Skip to content

Commit

Permalink
refact: rust fixes and optimizations (ChrisTitusTech#933)
Browse files Browse the repository at this point in the history
* fix: getting locked out when running script

* Use success and fail colors and reorder imports

Use theme color instead of using ratatui::Color for running_command success and fail + search preview text color + min tui warning color, add colors for confirmation prompt, fix inverted success and fail colors

* Remove redundant code in themes

Removed redundant match statement with a function

* Fix scroll beyond list, color bleeding and refact in confirmation.rs

Remove unnecessary usage of pub in ConfirmPropmt struct fields, simplify numbering, prevent scrolling beyond list, fix color bleeding

* Implement case insensitive, fix word disappearing bug

Use regex for case insesitive finding, implement String instead of char<Vec>, fix word disappearing by recalculating the render x for preview text

* Revert "Remove redundant code in themes"

This reverts commit 3b7e859.

* Reference instead of passing the vector

* Revert regex and String implementation

Use Vec<char> for search_input to prevent panics when using multi-byte characters, use lowercase conversion instead of regex, Added comments for clarity

* Replace ansi and text wrapping code with ratatui

Replaced ansi related code for tree sitter highlight with direct ratatui::text. Cache the processed text in appstate to remove processing of text for every frame render.Create paragraph instead of list so that scroll and wrapping can be done without external crates. Add caps keys for handle_key_event.

* Fix conflicts

* Reference instead of borrowing commands, refact mut variables

Reference instead of borrowing commands from state, Refactor draw function variables to immutable, calculate innersize from block instead of manual definition

* Update tui/src/filter.rs

Co-authored-by: Liam <[email protected]>

* Rendering optimizations and function refactors

Handle `find_command` inside state itself -> `get_command_by_name`. Move tips to a seperate file for modularity. Pass the whole args to state instead of seperate args. Use const for float and confirmation prompt float sizes. Add the `longest_tab_length` to appstate struct so that it will not be calculated for each frame render use static str instead String for tips. Use function for spawning confirmprompt. Merge command list and task items list rendering a single widget instead of two. Remove redundant keys in handle_key. Optimize scrolling logic. Rename `toggle_task_list_guide` -> `enable_task_list_guide`

* Cleanup

Use prelude for ratatui imports. Use const for theme functions, add
missing hints

* Update deps, remove unused temp-dir

* Add accidentally deleted preview.tape

Add labels + Wait 2sec after program ends

* Add fields to config files

Skip Confirmation, Bypass Size

* Remove accidentally commited config file

---------

Co-authored-by: Liam <[email protected]>
  • Loading branch information
jeevithakannan2 and lj3954 authored Nov 17, 2024
1 parent fa69885 commit ab7a670
Show file tree
Hide file tree
Showing 17 changed files with 596 additions and 819 deletions.
109 changes: 24 additions & 85 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include = ["src/*.rs", "Cargo.toml", "tabs/**"]
[dependencies]
include_dir = "0.7.4"
temp-dir = "0.1.14"
serde = { version = "1.0.205", features = ["derive"], default-features = false }
serde = { version = "1.0.215", features = ["derive"], default-features = false }
toml = { version = "0.8.19", features = ["parse"], default-features = false }
which = "6.0.3"
which = "7.0.0"
ego-tree = "0.9.0"
43 changes: 37 additions & 6 deletions core/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
use crate::{ListNode, TabList};
use serde::Deserialize;
use std::path::Path;
use std::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 read_config(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()
})
}
}
9 changes: 4 additions & 5 deletions core/src/inner.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::{Command, ListNode, Tab};
use ego_tree::{NodeMut, Tree};
use include_dir::{include_dir, Dir};
use serde::Deserialize;
use std::{
fs::File,
io::{BufRead, BufReader, Read},
Expand All @@ -6,11 +10,6 @@ use std::{
path::{Path, PathBuf},
rc::Rc,
};

use crate::{Command, ListNode, Tab};
use ego_tree::{NodeMut, Tree};
use include_dir::{include_dir, Dir};
use serde::Deserialize;
use temp_dir::TempDir;

const TAB_DATA: Dir = include_dir!("$CARGO_MANIFEST_DIR/tabs");
Expand Down
12 changes: 4 additions & 8 deletions 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 @@ -38,14 +38,10 @@ pub struct ListNode {
}

impl Tab {
pub fn find_command(&self, name: &str) -> Option<Rc<ListNode>> {
fn find_command_by_name(&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
}
let node_value = node.value();
(node_value.name == name && !node.has_children()).then_some(node_value.clone())
})
}
}
13 changes: 4 additions & 9 deletions tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@ oneshot = { version = "0.1.8", features = ["std"], default-features = false }
portable-pty = "0.8.1"
ratatui = { version = "0.29.0", features = ["crossterm"], default-features = false }
tui-term = { version = "0.2.0", default-features = false }
temp-dir = "0.1.14"
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" }
tree-sitter-highlight = "0.24.3"
tree-sitter-bash = "0.23.1"
textwrap = { version = "0.16.1", default-features = false }
anstyle = { version = "1.0.8", default-features = false }
ansi-to-tui = { version = "7.0.0", default-features = false }
zips = "0.1.7"
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" ] }
vt100-ctt = { git = "https://github.com/ChrisTitusTech/vt100-rust" }
vt100-ctt = "0.16.0"

[[bin]]
name = "linutil"
Expand Down
Loading

0 comments on commit ab7a670

Please sign in to comment.