mirror of
https://github.com/ChrisTitusTech/linutil.git
synced 2024-11-05 21:28:48 +00:00
Added AppState, made theme use AppState
This commit is contained in:
parent
8e8476cda5
commit
bfc8e5ea82
15
src/list.rs
15
src/list.rs
|
@ -1,4 +1,4 @@
|
|||
use crate::{float::floating_window, theme::*};
|
||||
use crate::{float::floating_window, state::AppState};
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind};
|
||||
use ego_tree::{tree, NodeId};
|
||||
use ratatui::{
|
||||
|
@ -130,9 +130,8 @@ impl CustomList {
|
|||
}
|
||||
|
||||
/// Draw our custom widget to the frame
|
||||
pub fn draw(&mut self, frame: &mut Frame, area: Rect) {
|
||||
pub fn draw(&mut self, frame: &mut Frame, area: Rect, state: &AppState) {
|
||||
// Get the last element in the `visit_stack` vec
|
||||
let theme = get_theme();
|
||||
let curr = self
|
||||
.inner_tree
|
||||
.get(*self.visit_stack.last().unwrap())
|
||||
|
@ -143,7 +142,7 @@ impl CustomList {
|
|||
// to go up the tree
|
||||
// icons:
|
||||
if !self.at_root() {
|
||||
items.push(Line::from(format!("{} ..", theme.dir_icon)).style(theme.dir_color));
|
||||
items.push(Line::from(format!("{} ..", state.theme.dir_icon)).style(state.theme.dir_color));
|
||||
}
|
||||
|
||||
// Iterate through all the children
|
||||
|
@ -152,13 +151,13 @@ impl CustomList {
|
|||
// it's a directory and will be handled as such
|
||||
if node.has_children() {
|
||||
items.push(
|
||||
Line::from(format!("{} {}", theme.dir_icon, node.value().name))
|
||||
.style(theme.dir_color),
|
||||
Line::from(format!("{} {}", state.theme.dir_icon, node.value().name))
|
||||
.style(state.theme.dir_color),
|
||||
);
|
||||
} else {
|
||||
items.push(
|
||||
Line::from(format!("{} {}", theme.cmd_icon, node.value().name))
|
||||
.style(theme.cmd_color),
|
||||
Line::from(format!("{} {}", state.theme.cmd_icon, node.value().name))
|
||||
.style(state.theme.cmd_color),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
25
src/main.rs
25
src/main.rs
|
@ -1,6 +1,7 @@
|
|||
mod float;
|
||||
mod list;
|
||||
mod running_command;
|
||||
pub mod state;
|
||||
mod theme;
|
||||
|
||||
use std::{
|
||||
|
@ -22,7 +23,8 @@ use ratatui::{
|
|||
Terminal,
|
||||
};
|
||||
use running_command::RunningCommand;
|
||||
use theme::set_theme;
|
||||
use state::AppState;
|
||||
use theme::THEMES;
|
||||
|
||||
/// This is a binary :), Chris, change this to update the documentation on -h
|
||||
#[derive(Debug, Parser)]
|
||||
|
@ -34,16 +36,23 @@ struct Args {
|
|||
|
||||
fn main() -> std::io::Result<()> {
|
||||
let args = Args::parse();
|
||||
if args.compat {
|
||||
set_theme(0);
|
||||
}
|
||||
|
||||
let theme = if args.compat {
|
||||
THEMES[0].clone()
|
||||
} else {
|
||||
THEMES[1].clone()
|
||||
};
|
||||
|
||||
let state = AppState {
|
||||
theme,
|
||||
};
|
||||
|
||||
stdout().execute(EnterAlternateScreen)?;
|
||||
enable_raw_mode()?;
|
||||
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
|
||||
terminal.clear()?;
|
||||
|
||||
run(&mut terminal)?;
|
||||
run(&mut terminal, &state)?;
|
||||
|
||||
// restore terminal
|
||||
disable_raw_mode()?;
|
||||
|
@ -55,7 +64,7 @@ fn main() -> std::io::Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn run<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
|
||||
fn run<B: Backend>(terminal: &mut Terminal<B>, state: &AppState) -> io::Result<()> {
|
||||
let mut command_opt: Option<RunningCommand> = None;
|
||||
|
||||
let mut custom_list = CustomList::new();
|
||||
|
@ -63,9 +72,9 @@ fn run<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
|
|||
// Always redraw
|
||||
terminal
|
||||
.draw(|frame| {
|
||||
custom_list.draw(frame, frame.size());
|
||||
custom_list.draw(frame, frame.size(), state);
|
||||
if let Some(ref mut command) = &mut command_opt {
|
||||
command.draw(frame);
|
||||
command.draw(frame, state);
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
|
|
|
@ -11,7 +11,7 @@ use portable_pty::{
|
|||
};
|
||||
use ratatui::{
|
||||
layout::Size,
|
||||
style::{Color, Style, Stylize},
|
||||
style::{Style, Stylize},
|
||||
text::{Line, Span},
|
||||
widgets::{Block, Borders},
|
||||
Frame,
|
||||
|
@ -21,7 +21,14 @@ use tui_term::{
|
|||
widget::PseudoTerminal,
|
||||
};
|
||||
|
||||
use crate::{float::floating_window, theme::get_theme};
|
||||
use crate::{float::floating_window, state::AppState};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Command {
|
||||
Raw(&'static str),
|
||||
LocalFile(&'static str),
|
||||
None, // Directory
|
||||
}
|
||||
|
||||
/// This is a struct for storing everything connected to a running command
|
||||
// Create a new instance on every new command you want to run
|
||||
|
@ -50,14 +57,22 @@ pub struct RunningCommand {
|
|||
}
|
||||
|
||||
impl RunningCommand {
|
||||
pub fn new(command: &str) -> Self {
|
||||
pub fn new(command: Command, state: &AppState) -> Self {
|
||||
let pty_system = NativePtySystem::default();
|
||||
let mut cmd = CommandBuilder::new("sh");
|
||||
cmd.arg("-c");
|
||||
cmd.arg(command);
|
||||
|
||||
let cwd = std::env::current_dir().unwrap();
|
||||
cmd.cwd(cwd);
|
||||
let mut cmd = CommandBuilder::new("sh");
|
||||
match command {
|
||||
Command::Raw(prompt) => {
|
||||
cmd.arg("-c");
|
||||
cmd.arg(prompt);
|
||||
}
|
||||
Command::LocalFile(file) => {
|
||||
cmd.arg(file);
|
||||
}
|
||||
Command::None => panic!("Command::None was treated as a command"),
|
||||
}
|
||||
|
||||
cmd.cwd(&state.temp_path);
|
||||
|
||||
let pair = pty_system
|
||||
.openpty(PtySize {
|
||||
|
@ -153,9 +168,7 @@ impl RunningCommand {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, frame: &mut Frame) {
|
||||
{
|
||||
let theme = get_theme();
|
||||
pub fn draw(&mut self, frame: &mut Frame, state: &AppState) {
|
||||
// Funny name
|
||||
let floater = floating_window(frame.size());
|
||||
|
||||
|
@ -180,13 +193,13 @@ impl RunningCommand {
|
|||
Line::from(
|
||||
Span::default()
|
||||
.content("SUCCESS!")
|
||||
.style(Style::default().fg(theme.success_color).reversed()),
|
||||
.style(Style::default().fg(state.theme.success_color).reversed()),
|
||||
)
|
||||
} else {
|
||||
Line::from(
|
||||
Span::default()
|
||||
.content("FAILED!")
|
||||
.style(Style::default().fg(theme.fail_color).reversed()),
|
||||
.style(Style::default().fg(state.theme.fail_color).reversed()),
|
||||
)
|
||||
};
|
||||
|
||||
|
@ -205,7 +218,7 @@ impl RunningCommand {
|
|||
let pseudo_term = PseudoTerminal::new(&screen).block(term_border);
|
||||
frame.render_widget(pseudo_term, floater);
|
||||
}
|
||||
}
|
||||
|
||||
/// Send SIGHUB signal, *not* SIGKILL or SIGTERM, to the child process
|
||||
pub fn kill_child(&mut self) {
|
||||
if !self.is_finished() {
|
||||
|
|
6
src/state.rs
Normal file
6
src/state.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
use crate::theme::Theme;
|
||||
|
||||
pub struct AppState {
|
||||
/// Selected theme
|
||||
pub theme: Theme,
|
||||
}
|
11
src/theme.rs
11
src/theme.rs
|
@ -1,7 +1,6 @@
|
|||
use ratatui::style::Color;
|
||||
|
||||
pub static mut THEME_IDX: usize = 1;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Theme {
|
||||
pub dir_color: Color,
|
||||
pub cmd_color: Color,
|
||||
|
@ -29,11 +28,3 @@ pub const THEMES: [Theme; 2] = [
|
|||
success_color: Color::Rgb(5, 255, 55),
|
||||
},
|
||||
];
|
||||
|
||||
pub fn get_theme() -> &'static Theme {
|
||||
&THEMES[unsafe { THEME_IDX }]
|
||||
}
|
||||
|
||||
pub fn set_theme(idx: usize) {
|
||||
unsafe { THEME_IDX = idx };
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user