diff options
Diffstat (limited to 'src/edit.rs')
-rw-r--r-- | src/edit.rs | 136 |
1 files changed, 82 insertions, 54 deletions
diff --git a/src/edit.rs b/src/edit.rs index a804def..f21cf73 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -1,6 +1,8 @@ use vtcol::{Palette, Rgb, Scheme}; -use std::{fmt, path::PathBuf, rc::Rc}; +use std::{fmt, + path::{Path, PathBuf}, + rc::Rc}; use anyhow::{anyhow, Result}; @@ -143,12 +145,8 @@ slint::slint! { input (k) => { t.text = Aux.handle-command-buffer (k, t.text, cursor); - if (t.text == "") { - /* User backspaced her way to the left. */ - return true; - } - - return false; + /* Buffer is empty if the user backspaced her way to the left. */ + return t.text == ""; } t := Text { @@ -161,11 +159,8 @@ slint::slint! { GuiEdit := Window { property scheme-name <=> name.text; - callback set-primary ([color]); - callback set-secondary ([color]); - - set-primary (colors) => { primary-colors .colors = colors; } - set-secondary (colors) => { secondary-colors.colors = colors; } + property <[color]> primary <=> primary-colors .colors; + property <[color]> secondary <=> secondary-colors.colors; callback get-palette-color(int) -> color; callback set-palette-color(int, color); @@ -476,54 +471,67 @@ impl Edit } }); - gui.global::<Aux>().on_handle_command_buffer(|ev, text, _pos| { - let text = match KeyInput::from(&ev) { - KeyInput::Printable(s) => { - let mut text = text.to_string(); - text.push_str(&s); - text - }, - KeyInput::Return => { - match Command::try_from(text.as_str()) { - Err(e) => { - eprintln!("bad command [{}]: {}", text, e); - String::new() - }, - Ok(cmd) => { - if let Err(e) = cmd.exec() { - eprintln!( - "error executing command [{}]: {}", - text, e - ); - } - String::new() + { + let guiw = gui.as_weak(); + gui.global::<Aux>().on_handle_command_buffer(move |ev, text, _pos| { + let text = match KeyInput::from(&ev) { + KeyInput::Printable(s) => { + let mut text = text.to_string(); + text.push_str(&s); + text + }, + KeyInput::Return => { + match Command::try_from(text.as_str()) { + Err(e) => { + eprintln!("bad command [{}]: {}", text, e); + String::new() + }, + Ok(cmd) => { + use slint::Model; + let scheme_name = + guiw.unwrap().get_scheme_name().to_string(); + let prim = guiw.unwrap().get_primary(); + let secn = guiw.unwrap().get_secondary(); + let pal = prim.iter().chain(secn.iter()) + .collect::<Vec<Color>>(); + let pal = Palette::from(pal.as_slice()); + if let Err(e) = cmd + .exec(scheme_name.as_str(), pal) + { + eprintln!( + "error executing command [{}]: {}", + text, e + ); + } + String::new() + }, + } + /* The empty string signals to leave command mode. */ + }, + KeyInput::Backspace => + match text.char_indices().next_back() { + Some((i, _)) => text[..i].into(), + None => text.to_string(), }, - } - /* The empty string signals to leave command mode. */ - }, - KeyInput::Backspace => - match text.char_indices().next_back() { - Some((i, _)) => text[..i].into(), - None => text.to_string(), + other => { + eprintln!( + "»»» command mode input: “{}”", + other.to_string() + ); + text.to_string() }, - other => { - eprintln!( - "»»» command mode input: “{}”", - other.to_string() - ); - text.to_string() - }, - }; - eprintln!("»»» command buffer: “{}”", text); - text.into() - }); + }; + eprintln!("»»» command buffer: “{}”", text); + text.into() + }); + } if let Some(name) = name { gui.set_scheme_name(name.into()); } - gui.invoke_set_primary(primary.into()); - gui.invoke_set_secondary(secondary.into()); + gui.set_primary(primary.into()); + gui.set_secondary(secondary.into()); gui.run(); @@ -568,5 +576,25 @@ impl TryFrom<&str> for Command impl Command { - fn exec(self) -> Result<()> { todo!("got command: {:?}", self) } + fn exec(self, name: &str, pal: Palette) -> Result<()> + { + match self { + Self::Noop => Ok(()), + Self::Save(None) => Self::save(&PathBuf::from(&name), pal), + Self::Save(Some(path)) => Self::save(&path, pal), + } + //todo!("got command: {:?}", self) + } + + fn save(path: &Path, pal: Palette) -> Result<()> + { + use std::io::Write; + + let mut f = + std::fs::OpenOptions::new().create(true).write(true).open(path)?; + let pal: String = pal.into(); + f.write_all(pal.as_bytes()).map_err(|e| { + anyhow!("error saving Palette to file [{}]: {}", path.display(), e) + }) + } } |