Skip to content

Commit

Permalink
Add list, connect, remove args
Browse files Browse the repository at this point in the history
  • Loading branch information
weyh committed Jan 13, 2024
1 parent f00fe8e commit 732b7ea
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 19 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ If you find anything that isn't rusty enough feel free to correct me. 🙂

## Args
```
Usage: gcoma --user-config <USER_CONFIG>
Usage: gcoma [OPTIONS] --user-config <USER_CONFIG>
Options:
-u, --user-config <USER_CONFIG> Path to user config file
-h, --help Print help
-V, --version Print version
-u, --user-config <USER_CONFIG> Path to user config file
-l, --list List all sessions
-c, --connect <SESSION_INDEX> Connect to session by index
-r, --remove <SESSION_GROUP_NAME> Remove session group by name
-h, --help Print help
-V, --version Print version
```

## Screenshot
Expand Down
24 changes: 24 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
54 changes: 50 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::io;
use std::{fs, io};

use ui::config::Config;

#[cfg(test)]
mod tests;
Expand All @@ -8,6 +10,12 @@ mod reqs_check;
mod session_core;
mod ui;

fn load_cfg_from_file(cfg_path: &str) -> io::Result<Config> {
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!");
Expand All @@ -17,10 +25,48 @@ fn main() -> io::Result<()> {
}

let matches = args::get_args();
let user_config = matches.get_one::<String>("user_config");
let cfg_path = matches.get_one::<String>("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::<bool>("list").unwrap_or(&false).to_owned();
let connect_idx = matches.get_one::<String>("connect");
let rm_sg = matches.get_one::<String>("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!");
}
Expand Down
16 changes: 5 additions & 11 deletions src/ui/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<Config> {
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,
Expand Down Expand Up @@ -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<Config>) -> io::Result<()> {
let mut state = ViewState::new(cfg.unwrap_or(Config::new()));

enable_raw_mode()?;
execute!(stdout(), EnterAlternateScreen, EnableMouseCapture)?;
Expand Down

0 comments on commit 732b7ea

Please sign in to comment.