mirror of
https://github.com/ChrisTitusTech/linutil.git
synced 2024-11-23 21:51:56 +00:00
Add fields to config files
Skip Confirmation, Bypass Size
This commit is contained in:
parent
f8c6ba1e59
commit
ed01e1302f
|
@ -1,14 +1,31 @@
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use crate::{ListNode, TabList};
|
||||||
|
|
||||||
|
// Struct that defines what values can be used in the toml file
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub auto_execute: Vec<String>,
|
#[serde(default)]
|
||||||
|
auto_execute: Option<Vec<String>>,
|
||||||
|
#[serde(default)]
|
||||||
|
skip_confirmation: Option<bool>,
|
||||||
|
#[serde(default)]
|
||||||
|
size_bypass: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Struct that holds the parsed values from the toml so that it can be applied in the AppState
|
||||||
|
pub struct ConfigValues {
|
||||||
|
pub auto_execute_commands: Vec<Rc<ListNode>>,
|
||||||
|
pub skip_confirmation: bool,
|
||||||
|
pub size_bypass: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn from_file(path: &Path) -> Self {
|
pub fn new(path: &Path, tabs: &TabList) -> ConfigValues {
|
||||||
let content = match std::fs::read_to_string(path) {
|
let content = match std::fs::read_to_string(path) {
|
||||||
Ok(content) => content,
|
Ok(content) => content,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -17,12 +34,29 @@ impl Config {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match toml::from_str(&content) {
|
let config: Config = match toml::from_str(&content) {
|
||||||
Ok(config) => config,
|
Ok(config) => config,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Failed to parse config file: {}", e);
|
eprintln!("Failed to parse config file: {}", e);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ConfigValues {
|
||||||
|
auto_execute_commands: config.auto_execute_commands(tabs),
|
||||||
|
skip_confirmation: config.skip_confirmation.unwrap_or(false),
|
||||||
|
size_bypass: config.size_bypass.unwrap_or(false),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn auto_execute_commands(&self, tabs: &TabList) -> Vec<Rc<ListNode>> {
|
||||||
|
self.auto_execute
|
||||||
|
.as_ref()
|
||||||
|
.map_or_else(Vec::new, |commands| {
|
||||||
|
commands
|
||||||
|
.iter()
|
||||||
|
.filter_map(|name| tabs.iter().find_map(|tab| tab.find_command(name)))
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ pub use ego_tree;
|
||||||
use ego_tree::Tree;
|
use ego_tree::Tree;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub use config::Config;
|
pub use config::{Config, ConfigValues};
|
||||||
pub use inner::{get_tabs, TabList};
|
pub use inner::{get_tabs, TabList};
|
||||||
|
|
||||||
#[derive(Clone, Hash, Eq, PartialEq)]
|
#[derive(Clone, Hash, Eq, PartialEq)]
|
||||||
|
|
|
@ -9,7 +9,7 @@ use crate::{
|
||||||
theme::Theme,
|
theme::Theme,
|
||||||
};
|
};
|
||||||
|
|
||||||
use linutil_core::{ego_tree::NodeId, Config, ListNode, TabList};
|
use linutil_core::{ego_tree::NodeId, Config, ConfigValues, ListNode, TabList};
|
||||||
#[cfg(feature = "tips")]
|
#[cfg(feature = "tips")]
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
|
@ -105,8 +105,6 @@ impl AppState {
|
||||||
let tabs = linutil_core::get_tabs(!override_validation);
|
let tabs = linutil_core::get_tabs(!override_validation);
|
||||||
let root_id = tabs[0].tree.root().id();
|
let root_id = tabs[0].tree.root().id();
|
||||||
|
|
||||||
let auto_execute_commands = config_path.map(|path| Config::from_file(&path).auto_execute);
|
|
||||||
|
|
||||||
let mut state = Self {
|
let mut state = Self {
|
||||||
areas: None,
|
areas: None,
|
||||||
theme,
|
theme,
|
||||||
|
@ -131,28 +129,38 @@ impl AppState {
|
||||||
}
|
}
|
||||||
|
|
||||||
state.update_items();
|
state.update_items();
|
||||||
if let Some(auto_execute_commands) = auto_execute_commands {
|
|
||||||
state.handle_initial_auto_execute(&auto_execute_commands);
|
if let Some(config_path) = config_path {
|
||||||
|
let config = Config::new(&config_path, &state.tabs);
|
||||||
|
state.apply_config(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
state
|
state
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_initial_auto_execute(&mut self, auto_execute_commands: &[String]) {
|
fn apply_config(&mut self, config_values: ConfigValues) {
|
||||||
self.selected_commands = auto_execute_commands
|
self.skip_confirmation = config_values.skip_confirmation;
|
||||||
.iter()
|
self.size_bypass = config_values.size_bypass;
|
||||||
.filter_map(|name| self.tabs.iter().find_map(|tab| tab.find_command(name)))
|
if !config_values.auto_execute_commands.is_empty() {
|
||||||
.collect();
|
self.selected_commands = config_values.auto_execute_commands;
|
||||||
|
self.handle_initial_auto_execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_initial_auto_execute(&mut self) {
|
||||||
if !self.selected_commands.is_empty() {
|
if !self.selected_commands.is_empty() {
|
||||||
let cmd_names: Vec<_> = self
|
if !self.skip_confirmation {
|
||||||
.selected_commands
|
let cmd_names: Vec<_> = self
|
||||||
.iter()
|
.selected_commands
|
||||||
.map(|node| node.name.as_str())
|
.iter()
|
||||||
.collect();
|
.map(|node| node.name.as_str())
|
||||||
|
.collect();
|
||||||
|
|
||||||
let prompt = ConfirmPrompt::new(&cmd_names);
|
let prompt = ConfirmPrompt::new(&cmd_names);
|
||||||
self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40));
|
self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40));
|
||||||
|
} else {
|
||||||
|
self.handle_confirm_command();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user