Skip to content

Commit 4414f58

Browse files
Merge branch 'config-updates' into optimization
Revert handling find_command_name in state.rs Add options for skip_confirmation, size_bypass
2 parents 42a7836 + ed01e13 commit 4414f58

File tree

6 files changed

+68
-43
lines changed

6 files changed

+68
-43
lines changed

Cargo.lock

+2-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
skip_confirmation = true

core/src/config.rs

+37-5
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
1+
use crate::{ListNode, TabList};
12
use serde::Deserialize;
2-
use std::{path::Path, process};
3+
use std::{fs, path::Path, process, rc::Rc};
34

5+
// Struct that defines what values can be used in the toml file
46
#[derive(Deserialize)]
7+
#[serde(deny_unknown_fields)]
58
pub struct Config {
6-
pub auto_execute: Vec<String>,
9+
#[serde(default)]
10+
auto_execute: Option<Vec<String>>,
11+
#[serde(default)]
12+
skip_confirmation: Option<bool>,
13+
#[serde(default)]
14+
size_bypass: Option<bool>,
15+
}
16+
17+
// Struct that holds the parsed values from the toml so that it can be applied in the AppState
18+
pub struct ConfigValues {
19+
pub auto_execute_commands: Vec<Rc<ListNode>>,
20+
pub skip_confirmation: bool,
21+
pub size_bypass: bool,
722
}
823

924
impl Config {
10-
pub fn from_file(path: &Path) -> Self {
11-
let content = match std::fs::read_to_string(path) {
25+
pub fn new(path: &Path, tabs: &TabList) -> ConfigValues {
26+
let content = match fs::read_to_string(path) {
1227
Ok(content) => content,
1328
Err(e) => {
1429
eprintln!("Failed to read config file {}: {}", path.display(), e);
1530
process::exit(1);
1631
}
1732
};
1833

19-
match toml::from_str(&content) {
34+
let config: Config = match toml::from_str(&content) {
2035
Ok(config) => config,
2136
Err(e) => {
2237
eprintln!("Failed to parse config file: {}", e);
2338
process::exit(1);
2439
}
40+
};
41+
42+
ConfigValues {
43+
auto_execute_commands: config.auto_execute_commands(tabs),
44+
skip_confirmation: config.skip_confirmation.unwrap_or(false),
45+
size_bypass: config.size_bypass.unwrap_or(false),
2546
}
2647
}
48+
49+
fn auto_execute_commands(&self, tabs: &TabList) -> Vec<Rc<ListNode>> {
50+
self.auto_execute
51+
.as_ref()
52+
.map_or_else(Vec::new, |commands| {
53+
commands
54+
.iter()
55+
.filter_map(|name| tabs.iter().find_map(|tab| tab.find_command_by_name(name)))
56+
.collect()
57+
})
58+
}
2759
}

core/src/lib.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub use ego_tree;
77
use ego_tree::Tree;
88
use std::path::PathBuf;
99

10-
pub use config::Config;
10+
pub use config::{Config, ConfigValues};
1111
pub use inner::{get_tabs, TabList};
1212

1313
#[derive(Clone, Hash, Eq, PartialEq)]
@@ -36,3 +36,12 @@ pub struct ListNode {
3636
pub task_list: String,
3737
pub multi_select: bool,
3838
}
39+
40+
impl Tab {
41+
fn find_command_by_name(&self, name: &str) -> Option<Rc<ListNode>> {
42+
self.tree.root().descendants().find_map(|node| {
43+
let node_value = node.value();
44+
(node_value.name == name && !node.has_children()).then_some(node_value.clone())
45+
})
46+
}
47+
}

tui/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ tui-term = "0.2.0"
2222
time = { version = "0.3.36", features = ["formatting", "local-offset", "macros"], default-features = false }
2323
unicode-width = { version = "0.2.0", default-features = false }
2424
rand = { version = "0.8.5", optional = true }
25-
linutil_core = { version = "24.10.31" }
25+
linutil_core = { version = "24.10.31", path = "../core" }
2626
tree-sitter-highlight = "0.24.4"
2727
tree-sitter-bash = "0.23.3"
2828
nix = { version = "0.29.0", features = [ "user" ] }

tui/src/state.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
theme::Theme,
1010
Args,
1111
};
12-
use linutil_core::{ego_tree::NodeId, Command, Config, ListNode, TabList};
12+
use linutil_core::{ego_tree::NodeId, Command, Config, ConfigValues, ListNode, TabList};
1313
use ratatui::{
1414
crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseEvent, MouseEventKind},
1515
layout::Flex,
@@ -98,10 +98,6 @@ impl AppState {
9898
let tabs = linutil_core::get_tabs(!args.override_validation);
9999
let root_id = tabs[0].tree.root().id();
100100

101-
let auto_execute_commands = args
102-
.config
103-
.map(|path| Config::from_file(&path).auto_execute);
104-
105101
let longest_tab_display_len = tabs
106102
.iter()
107103
.map(|tab| tab.name.len() + args.theme.tab_icon().len())
@@ -133,28 +129,29 @@ impl AppState {
133129
}
134130

135131
state.update_items();
136-
if let Some(auto_execute_commands) = auto_execute_commands {
137-
state.handle_initial_auto_execute(&auto_execute_commands);
132+
133+
if let Some(config_path) = args.config {
134+
let config = Config::new(&config_path, &state.tabs);
135+
state.apply_config(config);
138136
}
139137

140138
state
141139
}
142140

143-
fn find_command_by_name(&self, name: &str) -> Option<Rc<ListNode>> {
144-
self.tabs.iter().find_map(|tab| {
145-
tab.tree.root().descendants().find_map(|node| {
146-
let node_value = node.value();
147-
(node_value.name == name && !node.has_children()).then_some(node_value.clone())
148-
})
149-
})
141+
fn apply_config(&mut self, config_values: ConfigValues) {
142+
if !self.skip_confirmation {
143+
self.skip_confirmation = config_values.skip_confirmation;
144+
}
145+
if !self.size_bypass {
146+
self.size_bypass = config_values.size_bypass;
147+
}
148+
if !config_values.auto_execute_commands.is_empty() {
149+
self.selected_commands = config_values.auto_execute_commands;
150+
self.handle_initial_auto_execute();
151+
}
150152
}
151153

152-
fn handle_initial_auto_execute(&mut self, auto_execute_commands: &[String]) {
153-
self.selected_commands = auto_execute_commands
154-
.iter()
155-
.filter_map(|name| self.find_command_by_name(name))
156-
.collect();
157-
154+
fn handle_initial_auto_execute(&mut self) {
158155
if !self.selected_commands.is_empty() {
159156
self.spawn_confirmprompt();
160157
}

0 commit comments

Comments
 (0)