Skip to content

Commit

Permalink
Merge branch 'config-updates' into optimization
Browse files Browse the repository at this point in the history
Revert handling find_command_name in state.rs
Add options for skip_confirmation, size_bypass
  • Loading branch information
jeevithakannan2 committed Nov 15, 2024
2 parents 42a7836 + ed01e13 commit 4414f58
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 43 deletions.
18 changes: 2 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
skip_confirmation = true
42 changes: 37 additions & 5 deletions core/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
use crate::{ListNode, TabList};
use serde::Deserialize;
use std::{path::Path, process};
use std::{fs, path::Path, process, rc::Rc};

// Struct that defines what values can be used in the toml file
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
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 {
pub fn from_file(path: &Path) -> Self {
let content = match std::fs::read_to_string(path) {
pub fn new(path: &Path, tabs: &TabList) -> ConfigValues {
let content = match fs::read_to_string(path) {
Ok(content) => content,
Err(e) => {
eprintln!("Failed to read config file {}: {}", path.display(), e);
process::exit(1);
}
};

match toml::from_str(&content) {
let config: Config = match toml::from_str(&content) {
Ok(config) => config,
Err(e) => {
eprintln!("Failed to parse config file: {}", e);
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_by_name(name)))
.collect()
})
}
}
11 changes: 10 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use ego_tree;
use ego_tree::Tree;
use std::path::PathBuf;

pub use config::Config;
pub use config::{Config, ConfigValues};
pub use inner::{get_tabs, TabList};

#[derive(Clone, Hash, Eq, PartialEq)]
Expand Down Expand Up @@ -36,3 +36,12 @@ pub struct ListNode {
pub task_list: String,
pub multi_select: bool,
}

impl Tab {
fn find_command_by_name(&self, name: &str) -> Option<Rc<ListNode>> {
self.tree.root().descendants().find_map(|node| {
let node_value = node.value();
(node_value.name == name && !node.has_children()).then_some(node_value.clone())
})
}
}
2 changes: 1 addition & 1 deletion tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ tui-term = "0.2.0"
time = { version = "0.3.36", features = ["formatting", "local-offset", "macros"], default-features = false }
unicode-width = { version = "0.2.0", default-features = false }
rand = { version = "0.8.5", optional = true }
linutil_core = { version = "24.10.31" }
linutil_core = { version = "24.10.31", path = "../core" }
tree-sitter-highlight = "0.24.4"
tree-sitter-bash = "0.23.3"
nix = { version = "0.29.0", features = [ "user" ] }
Expand Down
37 changes: 17 additions & 20 deletions tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
theme::Theme,
Args,
};
use linutil_core::{ego_tree::NodeId, Command, Config, ListNode, TabList};
use linutil_core::{ego_tree::NodeId, Command, Config, ConfigValues, ListNode, TabList};
use ratatui::{
crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseEvent, MouseEventKind},
layout::Flex,
Expand Down Expand Up @@ -98,10 +98,6 @@ impl AppState {
let tabs = linutil_core::get_tabs(!args.override_validation);
let root_id = tabs[0].tree.root().id();

let auto_execute_commands = args
.config
.map(|path| Config::from_file(&path).auto_execute);

let longest_tab_display_len = tabs
.iter()
.map(|tab| tab.name.len() + args.theme.tab_icon().len())
Expand Down Expand Up @@ -133,28 +129,29 @@ impl AppState {
}

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) = args.config {
let config = Config::new(&config_path, &state.tabs);
state.apply_config(config);
}

state
}

fn find_command_by_name(&self, name: &str) -> Option<Rc<ListNode>> {
self.tabs.iter().find_map(|tab| {
tab.tree.root().descendants().find_map(|node| {
let node_value = node.value();
(node_value.name == name && !node.has_children()).then_some(node_value.clone())
})
})
fn apply_config(&mut self, config_values: ConfigValues) {
if !self.skip_confirmation {
self.skip_confirmation = config_values.skip_confirmation;
}
if !self.size_bypass {
self.size_bypass = config_values.size_bypass;
}
if !config_values.auto_execute_commands.is_empty() {
self.selected_commands = config_values.auto_execute_commands;
self.handle_initial_auto_execute();
}
}

fn handle_initial_auto_execute(&mut self, auto_execute_commands: &[String]) {
self.selected_commands = auto_execute_commands
.iter()
.filter_map(|name| self.find_command_by_name(name))
.collect();

fn handle_initial_auto_execute(&mut self) {
if !self.selected_commands.is_empty() {
self.spawn_confirmprompt();
}
Expand Down

0 comments on commit 4414f58

Please sign in to comment.