Add status bar

This commit is contained in:
Timothy Warren 2021-03-08 14:41:40 -05:00
parent a67f78680f
commit 3aa682fc5c
3 changed files with 61 additions and 2 deletions

View File

@ -4,6 +4,7 @@ use std::fs;
#[derive(Default)]
pub struct Document {
rows: Vec<Row>,
pub file_name: Option<String>,
}
impl Document {
@ -17,7 +18,8 @@ impl Document {
}
Ok(Self {
rows
rows,
file_name: Some(filename.to_string()),
})
}

View File

@ -2,8 +2,11 @@ use crate::Document;
use crate::Row;
use crate::Terminal;
use std::env;
use termion::color;
use termion::event::Key;
const STATUS_FG_COLOR: color::Rgb = color::Rgb(63, 63, 63);
const STATUS_BG_COLOR: color::Rgb = color::Rgb(239, 239, 239);
const VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(Default)]
@ -61,6 +64,8 @@ impl Editor {
println!("Goodbye.\r");
} else {
self.draw_rows();
self.draw_status_bar();
self.draw_message_bar();
Terminal::cursor_position(&Position {
x: self.cursor_position.x.saturating_sub(self.offset.x),
y: self.cursor_position.y.saturating_sub(self.offset.y),
@ -70,6 +75,41 @@ impl Editor {
Terminal::flush()
}
fn draw_status_bar(&self) {
let mut status;
let width = self.terminal.size().width as usize;
let mut file_name = "[No Name]".to_string();
if let Some(name) = &self.document.file_name {
file_name = name.clone();
file_name.truncate(20);
}
status = format!("{} - {} lines", file_name, self.document.len());
let line_indicator = format!(
"{}/{}",
self.cursor_position.y.saturating_add(1),
self.document.len()
);
let len = status.len() + line_indicator.len();
if width > len {
status.push_str(&" ".repeat(width - len));
}
status = format!("{}{}", status, line_indicator);
status.truncate(width);
Terminal::set_bg_color(STATUS_BG_COLOR);
Terminal::set_fg_color(STATUS_FG_COLOR);
println!("{}\r", status);
Terminal::reset_fg_color();
Terminal::reset_bg_color();
}
fn draw_message_bar(&self) {
Terminal::clear_current_line();
}
fn process_keypress(&mut self) -> Result<(), std::io::Error> {
let pressed_key = Terminal::read_key()?;
match pressed_key {

View File

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