feat(editor): add file name to status bar

This commit is contained in:
Harsh Shandilya 2022-08-08 00:38:05 +05:30
parent 5ea82d6a71
commit 58e6618e6b
No known key found for this signature in database
GPG Key ID: 366D7BBAD1031E80
2 changed files with 25 additions and 3 deletions

View File

@ -7,6 +7,7 @@ use termion::event::Key;
const NAME: &str = env!("CARGO_PKG_NAME");
const VERSION: &str = env!("CARGO_PKG_VERSION");
const STATUS_BG_COLOR: Rgb = Rgb(26, 28, 30);
const STATUS_FG_COLOR: Rgb = Rgb(225, 226, 229);
pub struct Editor {
should_quit: bool,
@ -209,9 +210,22 @@ impl Editor {
}
fn draw_status_bar(&self) {
let spaces = " ".repeat(self.terminal.size().width as usize);
let mut status;
let width = self.terminal.size().width as usize;
let mut file_name = "[No name]".to_string();
if let Some(filename) = &self.document.file_name {
file_name = filename.to_string();
file_name.truncate(20);
}
status = format!("{}", file_name);
if width > status.len() {
status.push_str(&" ".repeat(width - status.len()));
}
status.truncate(width);
Terminal::set_bg_color(STATUS_BG_COLOR);
println!("{}\r", spaces);
Terminal::set_fg_color(STATUS_FG_COLOR);
println!("{}\r", status);
Terminal::reset_fg_color();
Terminal::reset_bg_color();
}

View File

@ -1,6 +1,6 @@
use crate::Position;
use std::io::{self, stdout, Error, Write};
use termion::color::{Bg, Reset, Rgb};
use termion::color::{Bg, Fg, Reset, Rgb};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::{IntoRawMode, RawTerminal};
@ -75,4 +75,12 @@ impl Terminal {
pub fn reset_bg_color() {
print!("{}", Bg(Reset));
}
pub fn set_fg_color(color: Rgb) {
print!("{}", Fg(color));
}
pub fn reset_fg_color() {
print!("{}", Fg(Reset));
}
}