From f1882310c92cab7af2a2a69051e01c873a910ffa Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Thu, 26 Sep 2024 04:11:39 +0000 Subject: [PATCH] add window --- src/application.rs | 24 ++++++++++----------- src/command.rs | 23 ++++++++++---------- src/lib.rs | 1 + src/window.rs | 5 +++-- src/windows/editor.rs | 48 ++++++++++++++++++++++++++++++++++++++++++ src/windows/editor.rs~ | 48 ++++++++++++++++++++++++++++++++++++++++++ src/windows/mod.rs | 1 + 7 files changed, 125 insertions(+), 25 deletions(-) create mode 100644 src/windows/editor.rs create mode 100644 src/windows/editor.rs~ create mode 100644 src/windows/mod.rs diff --git a/src/application.rs b/src/application.rs index 31fc466..7ac3598 100644 --- a/src/application.rs +++ b/src/application.rs @@ -105,15 +105,15 @@ impl Application { } } let x = UnicodeWidthStr::width(show.as_str()) as u16; - let mut suggestion = String::new(); - for c in self.cmdprocessor.get_suggestion().chars() { - suggestion.push(c); - if UnicodeWidthStr::width(suggestion.as_str()) as u16 >= self.size.0 - 1 - x { - break; - } - } - let x = x + UnicodeWidthStr::width(suggestion.as_str()) as u16; - let mut prompt = String::from(if x == 0 {""}else{" - "}); + let mut suggestion = String::new(); + for c in self.cmdprocessor.get_suggestion().chars() { + suggestion.push(c); + if UnicodeWidthStr::width(suggestion.as_str()) as u16 >= self.size.0 - 1 - x { + break; + } + } + let x = x + UnicodeWidthStr::width(suggestion.as_str()) as u16; + let mut prompt = String::from(if x == 0 { "" } else { " - " }); for c in self.cmdprocessor.get_prompt().chars() { prompt.push(c); if UnicodeWidthStr::width(prompt.as_str()) as u16 >= self.size.0 - 1 - x { @@ -131,9 +131,9 @@ impl Application { self.colortheme.cmdbar_cmdexist }), style::Print(show), - cursor::SavePosition, - style::SetForegroundColor(self.colortheme.cmdbar_prompt), - style::Print(suggestion), + cursor::SavePosition, + style::SetForegroundColor(self.colortheme.cmdbar_prompt), + style::Print(suggestion), style::Print(prompt), style::Print(" ".repeat((self.size.0 - x) as usize)), cursor::RestorePosition, diff --git a/src/command.rs b/src/command.rs index 4f4db67..65a1da0 100644 --- a/src/command.rs +++ b/src/command.rs @@ -34,7 +34,7 @@ impl CommandProcessor { } pub fn get_suggestion(&self) -> &String { - &self.cmdbar_suggestion + &self.cmdbar_suggestion } pub fn unknown(&self) -> bool { @@ -53,17 +53,18 @@ impl CommandProcessor { for _ in self.command.chars() { s.remove(0); } - if self.command.is_empty() { - self.cmdbar_suggestion.clear(); - } else { + if self.command.is_empty() { + self.cmdbar_suggestion.clear(); + self.cmdbar_prompt.clear(); + } else { self.cmdbar_suggestion = s; - } + } } else { - self.cmdbar_suggestion.clear(); - } - self.unknown_cmd = false; - if full { - self.command.clear(); + self.cmdbar_suggestion.clear(); + } + self.unknown_cmd = false; + if full { + self.command.clear(); match name.as_str() { "quit" => return true, "new-window" => { @@ -77,7 +78,7 @@ impl CommandProcessor { } } else if !self.cmdbar_show.is_empty() { self.cmdbar_prompt = String::from("Unknown command"); - self.cmdbar_suggestion.clear(); + self.cmdbar_suggestion.clear(); self.unknown_cmd = true; } else { self.cmdbar_prompt.clear(); diff --git a/src/lib.rs b/src/lib.rs index 01f6579..7c50e7f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ pub mod colortheme; pub mod command; pub mod innerpipe; pub mod window; +pub mod windows; use std::io; diff --git a/src/window.rs b/src/window.rs index dfb2b0b..aad7a05 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,9 +1,10 @@ -use std::io; +use std::io::{self, Stdout}; pub trait Window { fn get_pos(&self) -> (u16, u16); fn get_size(&self) -> (u16, u16); + fn get_name(&self) -> &str; fn get_id(&self) -> usize; - fn render(&mut self, screen_size: (u16, u16)) -> io::Result<()>; + fn render(&mut self, stdout: &mut Stdout, screen_size: (u16, u16)) -> io::Result<()>; } diff --git a/src/windows/editor.rs b/src/windows/editor.rs new file mode 100644 index 0000000..90bc844 --- /dev/null +++ b/src/windows/editor.rs @@ -0,0 +1,48 @@ +use std::{ + hash::{DefaultHasher, Hash, Hasher}, + io::{self, Stdout}, +}; + +use crossterm::{cursor, queue}; + +use crate::window::Window; + +pub struct Editor { + name: String, + pos: (u16, u16), + size: (u16, u16), +} + +impl Window for Editor { + fn get_pos(&self) -> (u16, u16) { + self.pos + } + + fn get_size(&self) -> (u16, u16) { + self.size + } + + fn get_name(&self) -> &str { + &self.name + } + + fn get_id(&self) -> usize { + let mut state = DefaultHasher::new(); + self.name.hash(&mut state); + state.finish() as usize + } + + fn render(&mut self, stdout: &mut Stdout, screen_size: (u16, u16)) -> io::Result<()> { + queue!(stdout, cursor::SavePosition, cursor::RestorePosition)?; + Ok(()) + } +} + +impl Hash for Editor { + fn hash(&self, state: &mut H) + where + H: Hasher, + { + self.name.hash(state) + } +} diff --git a/src/windows/editor.rs~ b/src/windows/editor.rs~ new file mode 100644 index 0000000..2c673ae --- /dev/null +++ b/src/windows/editor.rs~ @@ -0,0 +1,48 @@ +use std::{ + hash::{DefaultHasher, Hash, Hasher}, + io::{self, Stdout}, +}; + +use crossterm::{cursor, queue}; + +use crate::window::Window; + +pub struct Editor { + name: String, + pos: (u16, u16), + size: (u16, u16), +} + +impl Window for Editor { + fn get_pos(&self) -> (u16, u16) { + self.pos + } + + fn get_size(&self) -> (u16, u16) { + self.size + } + + fn get_name(&self) -> &str { + &self.name + } + + fn get_id(&self) -> usize { + let mut state = DefaultHasher::new(); + self.name.hash(&mut state); + state.finish() as usize + } + + fn render(&mut self, stdout: &mut Stdout, screen_size: (u16, u16)) -> io::Result<()> { + queue!(stdout, cursor::SavePosition, cursor::StorePosition)?; + Ok(()) + } +} + +impl Hash for Editor { + fn hash(&self, state: &mut H) + where + H: Hasher, + { + self.name.hash(state) + } +} diff --git a/src/windows/mod.rs b/src/windows/mod.rs new file mode 100644 index 0000000..0769981 --- /dev/null +++ b/src/windows/mod.rs @@ -0,0 +1 @@ +pub mod editor;