Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add automation based on config file #836

Merged
merged 7 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ Note that crates installed using `cargo install` require manual updating with `c

</details>

## Configuration

Linutil supports configuration through a TOML config file. Path to the file can be specified with `--config` (or `-c`).

Available options:
- `auto_execute` - a list of commands to execute automatically (can be combined with `--skip-confirmation`)

Example config:
```toml
# example_config.toml

auto_execute = [
"Fastfetch",
"Alacritty",
"Kitty"
]
```

```bash
linutil --config /path/to/example_config.toml
```

## 💖 Support

If you find Linutil helpful, please consider giving it a ⭐️ to show your support!
Expand Down
28 changes: 28 additions & 0 deletions core/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_execute: 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);
}
}
}
}
15 changes: 15 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod config;
mod inner;

use std::rc::Rc;
Expand All @@ -6,6 +7,7 @@ pub use ego_tree;
use ego_tree::Tree;
use std::path::PathBuf;

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

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

impl Tab {
pub fn find_command(&self, name: &str) -> Option<Rc<ListNode>> {
self.tree.root().descendants().find_map(|node| {
let value = node.value();
if value.name == name && !node.has_children() {
Some(value.clone())
} else {
None
}
})
}
}
2 changes: 1 addition & 1 deletion docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
## Milestones
### Q4 2024
- [x] Finish the foundation of the project's CLI
- [ ] Implement CLI arguments and configuration support
- [x] Implement CLI arguments and configuration support
- [ ] Add an option for logging script executions

### Q1 2025
Expand Down
4 changes: 4 additions & 0 deletions man/linutil.1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ curl -fsSL https://christitus.com/linux | sh
curl -fsSL https://christitus.com/linuxdev | sh

.SH OPTIONS
.TP
\fB\-c\fR, \fB\-\-config\fR \fI<path>\fR
Path to the configuration file.

.TP
\fB\-t\fR, \fB\-\-theme\fR \fI<theme>\fR
Set the theme to use in the TUI.
Expand Down
4 changes: 4 additions & 0 deletions tui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod theme;

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

Expand All @@ -30,6 +31,8 @@ use state::AppState;
// Linux utility toolbox
#[derive(Debug, Parser)]
struct Args {
#[arg(short, long, help = "Path to the configuration file")]
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 @@ -52,6 +55,7 @@ fn main() -> io::Result<()> {
let args = Args::parse();

let mut state = AppState::new(
args.config,
args.theme,
args.override_validation,
args.size_bypass,
Expand Down
28 changes: 27 additions & 1 deletion tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
theme::Theme,
};

use linutil_core::{ego_tree::NodeId, ListNode, TabList};
use linutil_core::{ego_tree::NodeId, Config, ListNode, TabList};
#[cfg(feature = "tips")]
use rand::Rng;
use ratatui::{
Expand All @@ -19,6 +19,7 @@ use ratatui::{
widgets::{Block, Borders, List, ListState, Paragraph},
Frame,
};
use std::path::PathBuf;
use std::rc::Rc;

const MIN_WIDTH: u16 = 100;
Expand Down Expand Up @@ -87,6 +88,7 @@ enum SelectedItem {

impl AppState {
pub fn new(
config_path: Option<PathBuf>,
theme: Theme,
override_validation: bool,
size_bypass: bool,
Expand All @@ -95,6 +97,8 @@ impl AppState {
let tabs = linutil_core::get_tabs(!override_validation);
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 {
theme,
focus: Focus::List,
Expand All @@ -113,9 +117,31 @@ impl AppState {
};

state.update_items();
if let Some(auto_execute_commands) = auto_execute_commands {
state.handle_initial_auto_execute(&auto_execute_commands);
}

state
}

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

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

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