From 732b7ea6a4045ebb1d1522f97b7d64f26923d7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bak=20Gergely=20J=C3=A1nos?= <16374863+weyh@users.noreply.github.com> Date: Sat, 13 Jan 2024 15:08:59 +0100 Subject: [PATCH] Add list, connect, remove args --- README.md | 11 ++++++---- src/args.rs | 24 ++++++++++++++++++++++ src/main.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++---- src/ui/view.rs | 16 +++++---------- 4 files changed, 86 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index afb59f0..b92b574 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,15 @@ If you find anything that isn't rusty enough feel free to correct me. 🙂 ## Args ``` -Usage: gcoma --user-config +Usage: gcoma [OPTIONS] --user-config Options: - -u, --user-config Path to user config file - -h, --help Print help - -V, --version Print version + -u, --user-config Path to user config file + -l, --list List all sessions + -c, --connect Connect to session by index + -r, --remove Remove session group by name + -h, --help Print help + -V, --version Print version ``` ## Screenshot diff --git a/src/args.rs b/src/args.rs index 5bcf880..3b8b2c8 100644 --- a/src/args.rs +++ b/src/args.rs @@ -11,5 +11,29 @@ pub fn get_args() -> ArgMatches { .value_name("USER_CONFIG") .required(true), ) + .arg( + Arg::new("list") + .short('l') + .long("list") + .num_args(0) + .conflicts_with_all(["connect", "remove"]) + .help("List all sessions"), + ) + .arg( + Arg::new("connect") + .short('c') + .long("connect") + .value_name("SESSION_INDEX") + .conflicts_with_all(["list", "remove"]) + .help("Connect to session by index"), + ) + .arg( + Arg::new("remove") + .short('r') + .long("remove") + .value_name("SESSION_GROUP_NAME") + .conflicts_with_all(["list", "connect"]) + .help("Remove session group by name"), + ) .get_matches() } diff --git a/src/main.rs b/src/main.rs index 351e782..0e81be9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ -use std::io; +use std::{fs, io}; + +use ui::config::Config; #[cfg(test)] mod tests; @@ -8,6 +10,12 @@ mod reqs_check; mod session_core; mod ui; +fn load_cfg_from_file(cfg_path: &str) -> io::Result { + let cfg_str = fs::read_to_string(cfg_path)?; + let config = serde_json::from_str(&cfg_str)?; + Ok(config) +} + fn main() -> io::Result<()> { if !reqs_check::is_in_env("ssh") { panic!("'ssh' is not found in PATH!"); @@ -17,10 +25,48 @@ fn main() -> io::Result<()> { } let matches = args::get_args(); - let user_config = matches.get_one::("user_config"); + let cfg_path = matches.get_one::("user_config"); + + if cfg_path.is_some() { + let user_config = load_cfg_from_file(cfg_path.unwrap().as_str()); + + let list_flag = matches.get_one::("list").unwrap_or(&false).to_owned(); + let connect_idx = matches.get_one::("connect"); + let rm_sg = matches.get_one::("remove"); + + if list_flag { + let mut i = 0; + + for sg in user_config?.session_groups.iter() { + println!("{}:", sg.name); + + for s in sg.sessions.iter() { + println!(" {}. {}", i, s.name); + i += 1; + } + } + } else if connect_idx.is_some() { + let mut idx: usize = connect_idx.unwrap().parse().unwrap(); + + for sg in user_config?.session_groups.iter() { + for s in sg.sessions.iter() { + if idx == 0 { + s.connect(); + break; + } + idx -= 1; + } + } + } else if rm_sg.is_some() { + let mut ucfg = user_config.unwrap_or(Config::new()); + let sg_name = rm_sg.unwrap().to_owned(); + + ucfg.session_groups.retain(|sg| sg.name != sg_name); - if user_config.is_some() { - ui::view::display(user_config.unwrap())?; + ucfg.save(cfg_path.unwrap().as_str()); + } else { + ui::view::display(cfg_path.unwrap().as_str(), user_config)?; + } } else { panic!("No user config file specified!"); } diff --git a/src/ui/view.rs b/src/ui/view.rs index bfea17f..e41132e 100644 --- a/src/ui/view.rs +++ b/src/ui/view.rs @@ -4,12 +4,12 @@ use crossterm::{ terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; use ratatui::{prelude::*, widgets::*}; -use serde_json; + use std::io::{self, stdout, Stdout}; -use std::{fs, vec}; +use std::vec; use tui_textarea::{Input, Key}; -use crate::session_core::session::Session; +use crate::{load_cfg_from_file, session_core::session::Session}; use super::view_state::ViewState; use super::{ @@ -50,12 +50,6 @@ fn create_centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { .split(popup_layout[1])[1] } -fn load_cfg_from_file(cfg_path: &str) -> io::Result { - let cfg_str = fs::read_to_string(cfg_path)?; - let config = serde_json::from_str(&cfg_str)?; - Ok(config) -} - fn remove_selected(state: &mut ViewState) { let mut selected_idx = match state.table_state.selected() { Some(i) => i, @@ -401,8 +395,8 @@ fn connect_selected_ui( Ok(()) } -pub fn display(cfg_path: &str) -> io::Result<()> { - let mut state = ViewState::new(load_cfg_from_file(cfg_path).unwrap_or(Config::new())); +pub fn display(cfg_path: &str, cfg: io::Result) -> io::Result<()> { + let mut state = ViewState::new(cfg.unwrap_or(Config::new())); enable_raw_mode()?; execute!(stdout(), EnterAlternateScreen, EnableMouseCapture)?;