feat(tui): support changing the values of parameters

This commit is contained in:
Orhun Parmaksız 2022-01-20 18:10:52 +03:00
parent 59001598e6
commit a90fa2768f
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
2 changed files with 39 additions and 2 deletions

View file

@ -136,8 +136,33 @@ impl<'a> App<'a> {
.and_then(|v| CopyOption::try_from(*v).ok())
{
self.copy_to_clipboard(copy_option)?;
self.options = None;
} else if let Some(parameter) = self.parameter_list.selected() {
self.search_mode = false;
self.input_time = None;
self.input = Some(format!("set {} {}", parameter.name, parameter.value));
}
}
Command::Set(param_name, new_value) => {
if let Some(parameter) = self
.parameter_list
.items
.iter_mut()
.find(|param| param.name == param_name)
{
match parameter.update_value(&new_value, &self.sysctl.config, &mut Vec::new()) {
Ok(()) => {
self.run_command(Command::Refresh)?;
}
Err(e) => {
self.input = Some(e.to_string());
self.input_time = Some(Instant::now());
}
}
} else {
self.input = Some(String::from("Unknown parameter"));
self.input_time = Some(Instant::now());
}
self.options = None;
}
Command::ScrollUp => {
if let Some(options) = self.options.as_mut() {

View file

@ -6,6 +6,8 @@ use termion::event::Key;
pub enum Command {
/// Perform an action based on the selected entry.
Select,
/// Set the value of a parameter.
Set(String, String),
/// Scroll up on the widget.
ScrollUp,
/// Scroll down on the widget.
@ -41,7 +43,17 @@ impl FromStr for Command {
"copy" => Ok(Command::Copy),
"refresh" => Ok(Command::Refresh),
"exit" | "quit" | "q" | "q!" => Ok(Command::Exit),
_ => Err(()),
_ => {
if s.starts_with("set") {
let mut values = s.trim_start_matches("set").trim().split_whitespace();
Ok(Command::Set(
values.next().ok_or(())?.to_string(),
values.next().ok_or(())?.to_string(),
))
} else {
Err(())
}
}
}
}
}