From b765f894fc088cdb2d3fc0df637fd1e781fc5ee0 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 16 Mar 2021 10:28:41 -0400 Subject: [PATCH] Display file type --- src/document.rs | 8 ++++++++ src/editor.rs | 3 ++- src/filetype.rs | 35 +++++++++++++++++++++++++++++++++++ src/main.rs | 2 ++ src/row.rs | 5 +++-- 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/filetype.rs diff --git a/src/document.rs b/src/document.rs index 3390f8f..2ec4d1a 100644 --- a/src/document.rs +++ b/src/document.rs @@ -1,3 +1,4 @@ +use crate::FileType; use crate::Position; use crate::Row; use crate::SearchDirection; @@ -9,6 +10,7 @@ pub struct Document { rows: Vec, pub file_name: Option, dirty: bool, + file_type: FileType, } impl Document { @@ -27,9 +29,14 @@ impl Document { rows, file_name: Some(filename.to_string()), dirty: false, + file_type: FileType::from(filename), }) } + pub fn file_type(&self) -> String { + self.file_type.name() + } + pub fn row(&self, index: usize) -> Option<&Row> { self.rows.get(index) } @@ -100,6 +107,7 @@ impl Document { } // File has been cleaned! (Saved) + self.file_type = FileType::from(file_name); self.dirty = false; } diff --git a/src/editor.rs b/src/editor.rs index 6e55326..e47bf19 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -136,7 +136,8 @@ impl Editor { ); let line_indicator = format!( - "{}/{}", + "{} | {}/{}", + self.document.file_type(), self.cursor_position.y.saturating_add(1), self.document.len() ); diff --git a/src/filetype.rs b/src/filetype.rs new file mode 100644 index 0000000..b191cbf --- /dev/null +++ b/src/filetype.rs @@ -0,0 +1,35 @@ +pub struct FileType { + name: String, + hl_opts: HighlightingOptions, +} + +#[derive(Default)] +pub struct HighlightingOptions { + pub numbers: bool, +} + +impl Default for FileType { + fn default() -> Self { + Self { + name: String::from("No filetype"), + hl_opts: HighlightingOptions::default(), + } + } +} + +impl FileType { + pub fn name(&self) -> String { + self.name.clone() + } + + pub fn from(file_name: &str) -> Self { + if file_name.ends_with(".rs") { + return Self { + name: String::from("Rust"), + hl_opts: HighlightingOptions { numbers: true }, + }; + } + + Self::default() + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 80a8e98..3135e82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ #![allow(clippy::missing_errors_doc, clippy::must_use_candidate, clippy::panic)] mod document; mod editor; +mod filetype; mod highlighting; mod row; mod terminal; @@ -10,6 +11,7 @@ pub use document::Document; use editor::Editor; pub use editor::Position; pub use editor::SearchDirection; +pub use filetype::FileType; pub use row::Row; pub use terminal::Terminal; diff --git a/src/row.rs b/src/row.rs index 9f7e61c..a788de3 100644 --- a/src/row.rs +++ b/src/row.rs @@ -230,8 +230,9 @@ impl Row { &highlighting::Type::None }; - if c.is_ascii_digit() - && (prev_is_separator || previous_highlight == &highlighting::Type::Number) + if (c.is_ascii_digit() + && (prev_is_separator || previous_highlight == &highlighting::Type::Number)) + || (c == &'.' && previous_highlight == &highlighting::Type::Number) { highlighting.push(highlighting::Type::Number) } else {