add window

This commit is contained in:
pointer-to-bios 2024-09-26 04:11:39 +00:00
parent 934452beda
commit f1882310c9
7 changed files with 125 additions and 25 deletions

View File

@ -105,15 +105,15 @@ impl Application {
} }
} }
let x = UnicodeWidthStr::width(show.as_str()) as u16; let x = UnicodeWidthStr::width(show.as_str()) as u16;
let mut suggestion = String::new(); let mut suggestion = String::new();
for c in self.cmdprocessor.get_suggestion().chars() { for c in self.cmdprocessor.get_suggestion().chars() {
suggestion.push(c); suggestion.push(c);
if UnicodeWidthStr::width(suggestion.as_str()) as u16 >= self.size.0 - 1 - x { if UnicodeWidthStr::width(suggestion.as_str()) as u16 >= self.size.0 - 1 - x {
break; break;
} }
} }
let x = x + UnicodeWidthStr::width(suggestion.as_str()) as u16; let x = x + UnicodeWidthStr::width(suggestion.as_str()) as u16;
let mut prompt = String::from(if x == 0 {""}else{" - "}); let mut prompt = String::from(if x == 0 { "" } else { " - " });
for c in self.cmdprocessor.get_prompt().chars() { for c in self.cmdprocessor.get_prompt().chars() {
prompt.push(c); prompt.push(c);
if UnicodeWidthStr::width(prompt.as_str()) as u16 >= self.size.0 - 1 - x { if UnicodeWidthStr::width(prompt.as_str()) as u16 >= self.size.0 - 1 - x {
@ -131,9 +131,9 @@ impl Application {
self.colortheme.cmdbar_cmdexist self.colortheme.cmdbar_cmdexist
}), }),
style::Print(show), style::Print(show),
cursor::SavePosition, cursor::SavePosition,
style::SetForegroundColor(self.colortheme.cmdbar_prompt), style::SetForegroundColor(self.colortheme.cmdbar_prompt),
style::Print(suggestion), style::Print(suggestion),
style::Print(prompt), style::Print(prompt),
style::Print(" ".repeat((self.size.0 - x) as usize)), style::Print(" ".repeat((self.size.0 - x) as usize)),
cursor::RestorePosition, cursor::RestorePosition,

View File

@ -34,7 +34,7 @@ impl CommandProcessor {
} }
pub fn get_suggestion(&self) -> &String { pub fn get_suggestion(&self) -> &String {
&self.cmdbar_suggestion &self.cmdbar_suggestion
} }
pub fn unknown(&self) -> bool { pub fn unknown(&self) -> bool {
@ -53,17 +53,18 @@ impl CommandProcessor {
for _ in self.command.chars() { for _ in self.command.chars() {
s.remove(0); s.remove(0);
} }
if self.command.is_empty() { if self.command.is_empty() {
self.cmdbar_suggestion.clear(); self.cmdbar_suggestion.clear();
} else { self.cmdbar_prompt.clear();
} else {
self.cmdbar_suggestion = s; self.cmdbar_suggestion = s;
} }
} else { } else {
self.cmdbar_suggestion.clear(); self.cmdbar_suggestion.clear();
} }
self.unknown_cmd = false; self.unknown_cmd = false;
if full { if full {
self.command.clear(); self.command.clear();
match name.as_str() { match name.as_str() {
"quit" => return true, "quit" => return true,
"new-window" => { "new-window" => {
@ -77,7 +78,7 @@ impl CommandProcessor {
} }
} else if !self.cmdbar_show.is_empty() { } else if !self.cmdbar_show.is_empty() {
self.cmdbar_prompt = String::from("Unknown command"); self.cmdbar_prompt = String::from("Unknown command");
self.cmdbar_suggestion.clear(); self.cmdbar_suggestion.clear();
self.unknown_cmd = true; self.unknown_cmd = true;
} else { } else {
self.cmdbar_prompt.clear(); self.cmdbar_prompt.clear();

View File

@ -4,6 +4,7 @@ pub mod colortheme;
pub mod command; pub mod command;
pub mod innerpipe; pub mod innerpipe;
pub mod window; pub mod window;
pub mod windows;
use std::io; use std::io;

View File

@ -1,9 +1,10 @@
use std::io; use std::io::{self, Stdout};
pub trait Window { pub trait Window {
fn get_pos(&self) -> (u16, u16); fn get_pos(&self) -> (u16, u16);
fn get_size(&self) -> (u16, u16); fn get_size(&self) -> (u16, u16);
fn get_name(&self) -> &str;
fn get_id(&self) -> usize; 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<()>;
} }

48
src/windows/editor.rs Normal file
View File

@ -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<H>(&self, state: &mut H)
where
H: Hasher,
{
self.name.hash(state)
}
}

48
src/windows/editor.rs~ Normal file
View File

@ -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<H>(&self, state: &mut H)
where
H: Hasher,
{
self.name.hash(state)
}
}

1
src/windows/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod editor;