From 3aa682fc5ca2abe49752717fc8117010582e7c4e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 8 Mar 2021 14:41:40 -0500 Subject: [PATCH] Add status bar --- src/document.rs | 4 +++- src/editor.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/terminal.rs | 19 ++++++++++++++++++- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/document.rs b/src/document.rs index 427e6d1..69cfde0 100644 --- a/src/document.rs +++ b/src/document.rs @@ -4,6 +4,7 @@ use std::fs; #[derive(Default)] pub struct Document { rows: Vec, + pub file_name: Option, } impl Document { @@ -17,7 +18,8 @@ impl Document { } Ok(Self { - rows + rows, + file_name: Some(filename.to_string()), }) } diff --git a/src/editor.rs b/src/editor.rs index 66c87d5..5176f3f 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -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 { diff --git a/src/terminal.rs b/src/terminal.rs index 8472685..a55a041 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -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); } -} \ No newline at end of file + + 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)); + } +}