Skip to content

Commit

Permalink
feat: Add automation based on config file
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevithakannan2 committed Oct 15, 2024
1 parent 79eb752 commit 742ed1a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ ego-tree = { workspace = true }
oneshot = "0.1.8"
portable-pty = "0.8.1"
ratatui = "0.28.1"
serde = { version = "1.0.205", features = ["derive"], default-features = false }
toml = { version = "0.8.19", features = ["parse"], default-features = false }
tui-term = "0.1.12"
temp-dir = "0.1.14"
unicode-width = "0.2.0"
Expand Down
28 changes: 28 additions & 0 deletions tui/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use serde::Deserialize;
use std::path::Path;
use std::process;

#[derive(Deserialize)]
pub struct Config {
pub auto_select: Vec<String>,
}

impl Config {
pub fn from_file(path: &Path) -> Self {
let content = match std::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) {
Ok(config) => config,
Err(e) => {
eprintln!("Failed to parse config file: {}", e);
process::exit(1);
}
}
}
}
9 changes: 8 additions & 1 deletion tui/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod config;
mod confirmation;
mod filter;
mod float;
Expand All @@ -9,6 +10,7 @@ mod theme;

use std::{
io::{self, stdout},
path::PathBuf,
time::Duration,
};

Expand All @@ -26,6 +28,11 @@ use state::AppState;
// Linux utility toolbox
#[derive(Debug, Parser)]
struct Args {
#[arg(
long,
help = "Path to the configuration file for automatic command selection"
)]
config: Option<PathBuf>,
#[arg(short, long, value_enum)]
#[arg(default_value_t = Theme::Default)]
#[arg(help = "Set the theme to use in the application")]
Expand All @@ -38,7 +45,7 @@ struct Args {
fn main() -> io::Result<()> {
let args = Args::parse();

let mut state = AppState::new(args.theme, args.override_validation);
let mut state = AppState::new(args.theme, args.override_validation, args.config);

stdout().execute(EnterAlternateScreen)?;
enable_raw_mode()?;
Expand Down
37 changes: 36 additions & 1 deletion tui/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::Config;
use crate::{
confirmation::{ConfirmPrompt, ConfirmStatus},
filter::{Filter, SearchAction},
Expand All @@ -19,6 +20,7 @@ use ratatui::{
widgets::{Block, Borders, List, ListState, Paragraph},
Frame,
};
use std::path::PathBuf;
use std::rc::Rc;
use temp_dir::TempDir;

Expand Down Expand Up @@ -60,6 +62,7 @@ pub struct AppState {
multi_select: bool,
selected_commands: Vec<Rc<ListNode>>,
drawable: bool,
auto_select: Option<Vec<String>>,
#[cfg(feature = "tips")]
tip: &'static str,
}
Expand All @@ -79,10 +82,12 @@ pub struct ListEntry {
}

impl AppState {
pub fn new(theme: Theme, override_validation: bool) -> Self {
pub fn new(theme: Theme, override_validation: bool, config_path: Option<PathBuf>) -> Self {
let (temp_dir, tabs) = linutil_core::get_tabs(!override_validation);
let root_id = tabs[0].tree.root().id();

let auto_select = config_path.map(|path| Config::from_file(&path).auto_select);

let mut state = Self {
_temp_dir: temp_dir,
theme,
Expand All @@ -95,14 +100,44 @@ impl AppState {
multi_select: false,
selected_commands: Vec::new(),
drawable: false,
auto_select,
#[cfg(feature = "tips")]
tip: get_random_tip(),
};

state.update_items();

if let Some(auto_select) = state.auto_select.clone() {
state.handle_initial_auto_select(&auto_select);
}

state
}

fn handle_initial_auto_select(&mut self, auto_select: &[String]) {
let item_list = self.filter.item_list();
self.selected_commands = auto_select
.iter()
.filter_map(|name| {
item_list
.iter()
.find(|item| item.node.name == *name)
.map(|item| item.node.clone())
})
.collect();

if !self.selected_commands.is_empty() {
let cmd_names = self
.selected_commands
.iter()
.map(|node| node.name.as_str())
.collect::<Vec<_>>();

let prompt = ConfirmPrompt::new(&cmd_names);
self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40));
}
}

fn get_list_item_shortcut(&self) -> Box<[Shortcut]> {
if self.selected_item_is_dir() {
Box::new([Shortcut::new("Go to selected dir", ["l", "Right", "Enter"])])
Expand Down

0 comments on commit 742ed1a

Please sign in to comment.