From 957d720a7a7f123d4f6ec1ac0b85c6de85367637 Mon Sep 17 00:00:00 2001 From: Hannes Kuchelmeister Date: Tue, 10 Sep 2024 21:32:57 +0200 Subject: [PATCH] move handling of terminal settings into separate file --- src/app.rs | 2 +- src/modals/settings.rs | 16 +++++++++----- src/util.rs | 2 ++ src/util/prerequisit.rs | 29 ------------------------ src/util/settings.rs | 16 ++++++++++++++ src/util/terminal.rs | 49 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 src/util/settings.rs create mode 100644 src/util/terminal.rs diff --git a/src/app.rs b/src/app.rs index 5a60d4a..7f75551 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,6 @@ use crate::gtk::Align; -use crate::util::prerequisit::get_installed_terminals; use crate::util::prerequisit::is_toolbox_installed; +use crate::util::terminal::get_installed_terminals; use crate::modals::settings::SettingsMsg; use crate::util::toolbox::ToolbxContainer; diff --git a/src/modals/settings.rs b/src/modals/settings.rs index 3f13fef..bc57382 100644 --- a/src/modals/settings.rs +++ b/src/modals/settings.rs @@ -1,7 +1,11 @@ -use crate::util::prerequisit::get_installed_terminals; +use crate::util::terminal::get_installed_terminals; +use crate::util::terminal::TerminalType; use relm4::adw::prelude::{ ComboRowExt, PreferencesGroupExt, PreferencesPageExt, PreferencesRowExt, PreferencesWindowExt, }; + +use crate::util::settings::get_terminal; +use crate::util::settings::set_terminal; use relm4::gtk::prelude::Cast; use relm4::gtk::prelude::GtkWindowExt; use relm4::gtk::prelude::ListModelExt; @@ -58,8 +62,7 @@ impl SimpleComponent for SettingsDialog { ); }); - let settings = gio::Settings::new(APP_ID); - let terminal = settings.string("terminal"); + let terminal: String = get_terminal().into(); println!("{}", terminal); let terminal_selection = terminal_selection_model @@ -109,8 +112,11 @@ impl SimpleComponent for SettingsDialog { match msg { SettingsMsg::OpenSettings => {} SettingsMsg::TerminalSelectionChanged(terminal) => { - let settings = gio::Settings::new(APP_ID); - settings.set_string("terminal", &terminal); + let terminal_type: Result = terminal.as_str().try_into(); + match terminal_type { + Ok(t) => set_terminal(t), + _ => println!("Found unknown terminal string: {}", terminal), + } } } } diff --git a/src/util.rs b/src/util.rs index ac9dcba..8f3a4e4 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,2 +1,4 @@ pub mod prerequisit; +pub mod settings; +pub mod terminal; pub mod toolbox; diff --git a/src/util/prerequisit.rs b/src/util/prerequisit.rs index 0df03da..a5af22a 100644 --- a/src/util/prerequisit.rs +++ b/src/util/prerequisit.rs @@ -1,35 +1,6 @@ use crate::util::toolbox::ToolbxError; use std::process::Command; -#[derive(Debug)] -pub enum TerminalType { - GnomeTerminal, - Konsole, -} - -pub fn get_installed_terminals() -> Result, ToolbxError> { - let output = Command::new("flatpak-spawn") - .arg("--host") - .arg("gnome-terminal") - .arg("--version") - .output(); - - if output.is_err() { - return Err(ToolbxError::CommandExecutionError( - output.unwrap_err().to_string(), - )); - } - let output = output.unwrap(); - - if output.status.code() == Some(0) { - Ok(vec![TerminalType::GnomeTerminal, TerminalType::Konsole]) - } else { - Err(ToolbxError::CommandUnsuccessfulError( - String::from_utf8_lossy(&output.stderr).into_owned(), - )) - } -} - pub fn is_toolbox_installed() -> Result { let output = Command::new("flatpak-spawn") .arg("--host") diff --git a/src/util/settings.rs b/src/util/settings.rs new file mode 100644 index 0000000..7126c97 --- /dev/null +++ b/src/util/settings.rs @@ -0,0 +1,16 @@ +use crate::gio; +use crate::util::terminal::TerminalType; +use crate::APP_ID; +use relm4::gtk::prelude::SettingsExt; + +pub fn get_terminal() -> TerminalType { + let settings = gio::Settings::new(APP_ID); + let terminal = settings.string("terminal"); + terminal.as_str().try_into().unwrap_or_default() +} + +pub fn set_terminal(terminal: TerminalType) { + let settings = gio::Settings::new(APP_ID); + let terminal: String = terminal.into(); + settings.set_string("terminal", &terminal); +} diff --git a/src/util/terminal.rs b/src/util/terminal.rs new file mode 100644 index 0000000..83411d3 --- /dev/null +++ b/src/util/terminal.rs @@ -0,0 +1,49 @@ +use crate::util::toolbox::ToolbxError; +use std::process::Command; + +#[derive(Debug, Default)] +pub enum TerminalType { + #[default] + GnomeTerminal, + Konsole, +} + +impl From for String { + fn from(value: TerminalType) -> Self { + match value { + TerminalType::GnomeTerminal => "GnomeTerminal".to_string(), + TerminalType::Konsole => "Konsole".to_string(), + } + } +} + +impl TryFrom<&str> for TerminalType { + type Error = (); + + fn try_from(value: &str) -> Result { + Ok(TerminalType::GnomeTerminal) + } +} + +pub fn get_installed_terminals() -> Result, ToolbxError> { + let output = Command::new("flatpak-spawn") + .arg("--host") + .arg("gnome-terminal") + .arg("--version") + .output(); + + if output.is_err() { + return Err(ToolbxError::CommandExecutionError( + output.unwrap_err().to_string(), + )); + } + let output = output.unwrap(); + + if output.status.code() == Some(0) { + Ok(vec![TerminalType::GnomeTerminal, TerminalType::Konsole]) + } else { + Err(ToolbxError::CommandUnsuccessfulError( + String::from_utf8_lossy(&output.stderr).into_owned(), + )) + } +}