command bar update

This commit is contained in:
pointer-to-bios 2024-09-25 01:36:18 +00:00
parent f11e2e3a64
commit e0ba4e0661
5 changed files with 93 additions and 50 deletions

16
Cargo.lock generated
View File

@ -189,6 +189,7 @@ dependencies = [
"serde", "serde",
"tokio", "tokio",
"toml", "toml",
"unicode-width",
] ]
[[package]] [[package]]
@ -262,6 +263,12 @@ version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]]
name = "rustc-std-workspace-std"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aba676a20abe46e5b0f1b0deae474aaaf31407e6c71147159890574599da04ef"
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.38.37" version = "0.38.37"
@ -436,6 +443,15 @@ version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]]
name = "unicode-width"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd"
dependencies = [
"rustc-std-workspace-std",
]
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"

View File

@ -8,3 +8,4 @@ crossterm = "0.28.1"
serde = { version = "1.0.210", features = ["derive", "serde_derive"] } serde = { version = "1.0.210", features = ["derive", "serde_derive"] }
tokio = { version = "1.40.0", features = ["full"] } tokio = { version = "1.40.0", features = ["full"] }
toml = "0.8.19" toml = "0.8.19"
unicode-width = { version = "0.2.0", features = ["std"] }

View File

@ -6,11 +6,15 @@ use std::{
}; };
use crossterm::{ use crossterm::{
cursor, event, queue, cursor,
event::{self, KeyEvent, KeyModifiers},
queue,
style::{self, Color}, style::{self, Color},
terminal::{self, disable_raw_mode, enable_raw_mode, ClearType}, terminal::{self, disable_raw_mode, enable_raw_mode, ClearType},
}; };
use unicode_width::UnicodeWidthStr;
use crate::{colortheme::ColorTheme, iterate_config, window::Window, Config}; use crate::{colortheme::ColorTheme, iterate_config, window::Window, Config};
pub struct Application { pub struct Application {
@ -22,6 +26,7 @@ pub struct Application {
command_mode: bool, command_mode: bool,
command: String, command: String,
cmdbar_show: String, cmdbar_show: String,
cmdbar_prompt: String,
} }
impl Application { impl Application {
@ -44,6 +49,7 @@ impl Application {
command_mode: true, command_mode: true,
command: String::new(), command: String::new(),
cmdbar_show: String::new(), cmdbar_show: String::new(),
cmdbar_prompt: String::from("New workspace"),
}) })
} }
@ -55,45 +61,66 @@ impl Application {
match event::read()? { match event::read()? {
event::Event::FocusGained => queue!(self.stdout, cursor::Show)?, event::Event::FocusGained => queue!(self.stdout, cursor::Show)?,
event::Event::FocusLost => queue!(self.stdout, cursor::Hide)?, event::Event::FocusLost => queue!(self.stdout, cursor::Hide)?,
event::Event::Key(key_event) => match key_event.code { event::Event::Key(key_event) => {
event::KeyCode::Backspace => { let KeyEvent {
if self.command_mode { code, modifiers, ..
self.command.pop(); } = key_event;
self.cmdbar_show.clone_from(&self.command); match code {
event::KeyCode::Backspace => {
if self.command_mode {
self.command.pop();
self.cmdbar_show.clone_from(&self.command);
}
} }
} event::KeyCode::Enter => {
event::KeyCode::Enter => (), if self.command_mode {
event::KeyCode::Left => (), self.command.clear();
event::KeyCode::Right => (), self.cmdbar_show.clear();
event::KeyCode::Up => (), }
event::KeyCode::Down => (), }
event::KeyCode::Home => (), event::KeyCode::Left => (),
event::KeyCode::End => (), event::KeyCode::Right => (),
event::KeyCode::PageUp => (), event::KeyCode::Up => (),
event::KeyCode::PageDown => (), event::KeyCode::Down => (),
event::KeyCode::Tab => (), event::KeyCode::Home => (),
event::KeyCode::BackTab => (), event::KeyCode::End => (),
event::KeyCode::Delete => (), event::KeyCode::PageUp => (),
event::KeyCode::Insert => (), event::KeyCode::PageDown => (),
event::KeyCode::F(_) => (), event::KeyCode::Tab => (),
event::KeyCode::Char(c) => { event::KeyCode::BackTab => (),
if self.command_mode { event::KeyCode::Delete => (),
self.command.push(c); event::KeyCode::Insert => (),
self.cmdbar_show.clone_from(&self.command); event::KeyCode::F(_) => (),
event::KeyCode::Char(c) => {
if self.command_mode {
let mut b = true;
if modifiers.contains(KeyModifiers::CONTROL) {
self.command += "C-";
} else if modifiers.contains(KeyModifiers::ALT) {
self.command += "A-";
} else {
b = false;
}
self.command.push(c);
if b {
self.command.push(' ');
}
self.cmdbar_show.clone_from(&self.command);
}
} }
event::KeyCode::Null => (),
event::KeyCode::Esc => (),
event::KeyCode::CapsLock => (),
event::KeyCode::ScrollLock => (),
event::KeyCode::NumLock => (),
event::KeyCode::PrintScreen => (),
event::KeyCode::Pause => (),
event::KeyCode::Menu => (),
event::KeyCode::KeypadBegin => (),
event::KeyCode::Media(media) => (),
event::KeyCode::Modifier(modifier) => (),
} }
event::KeyCode::Null => (), }
event::KeyCode::Esc => (),
event::KeyCode::CapsLock => (),
event::KeyCode::ScrollLock => (),
event::KeyCode::NumLock => (),
event::KeyCode::PrintScreen => (),
event::KeyCode::Pause => (),
event::KeyCode::Menu => (),
event::KeyCode::KeypadBegin => (),
event::KeyCode::Media(media_key_code) => (),
event::KeyCode::Modifier(modifier_key_code) => (),
},
event::Event::Mouse(mouse_event) => (), event::Event::Mouse(mouse_event) => (),
event::Event::Paste(_) => (), event::Event::Paste(_) => (),
event::Event::Resize(w, h) => self.size = (w, h), event::Event::Resize(w, h) => self.size = (w, h),
@ -110,15 +137,7 @@ impl Application {
if let Some((name, _)) = iterate_config(map, &f) { if let Some((name, _)) = iterate_config(map, &f) {
match name.as_str() { match name.as_str() {
"quit" => break, "quit" => break,
"print" => queue!( _ => self.cmdbar_prompt = String::from("Unknown command"),
self.stdout,
style::SetBackgroundColor(self.colortheme.background),
cursor::SavePosition,
cursor::MoveTo(0, 0),
style::Print("Hello"),
cursor::RestorePosition,
)?,
_ => (),
} }
self.command.clear(); self.command.clear();
} }
@ -137,15 +156,25 @@ impl Application {
let mut show = String::new(); let mut show = String::new();
for c in self.cmdbar_show.chars() { for c in self.cmdbar_show.chars() {
show.push(c); show.push(c);
if cursor::position().unwrap().0 >= self.size.0 - 1 { if UnicodeWidthStr::width(show.as_str()) as u16 >= self.size.0 - 1 {
break; break;
} }
} }
let x = cursor::position().unwrap().0;
let mut prompt = String::new();
for c in self.cmdbar_prompt.chars() {
show.push(c);
if UnicodeWidthStr::width(prompt.as_str()) as u16 >= self.size.0 - 1 - x {
break;
}
}
queue!( queue!(
self.stdout, self.stdout,
style::SetBackgroundColor(self.colortheme.command_bar), style::SetBackgroundColor(self.colortheme.command_bar),
cursor::MoveTo(0, self.size.1 - 1), cursor::MoveTo(0, self.size.1 - 1),
style::Print(show), style::Print(show),
style::SetForegroudColor(self.colortheme.cmdbar_prompt),
style::Print(prompt),
)?; )?;
let x = cursor::position().unwrap().0; let x = cursor::position().unwrap().0;
queue!( queue!(

View File

@ -1,7 +1,6 @@
use crossterm::style::Color; use crossterm::style::Color;
use toml::{Table, Value}; use toml::{Table, Value};
pub struct ColorTheme { pub struct ColorTheme {
pub background: Color, pub background: Color,
pub command_bar: Color, pub command_bar: Color,

View File

@ -2,5 +2,3 @@ pub struct Window {
pos: (u16, u16), pos: (u16, u16),
size: (u16, u16), size: (u16, u16),
} }