Add edit and delete instructions

main
kirbylife 2026-07-24 21:32:31 -06:00
parent 0a8a831f95
commit 39bbd8192c
1 changed files with 61 additions and 16 deletions

View File

@ -70,18 +70,35 @@ enum Field {
Command,
}
struct AddForm {
#[derive(Clone, Copy)]
enum CommandFormTarget {
New,
Existing(usize),
}
struct CommandForm {
alias: Input,
command: Input,
field: Field,
target: CommandFormTarget,
}
impl AddForm {
impl CommandForm {
fn new() -> Self {
Self {
alias: Input::default(),
command: Input::new(String::from("ssh ")),
field: Field::Alias,
target: CommandFormTarget::New,
}
}
fn edit(index: usize, command: &Command) -> Self {
Self {
alias: Input::new(command.alias.clone()),
command: Input::new(command.command.clone()),
field: Field::Alias,
target: CommandFormTarget::Existing(index),
}
}
@ -102,7 +119,7 @@ impl AddForm {
enum Mode {
Normal,
Adding(AddForm),
Editing(CommandForm),
}
struct App {
@ -143,43 +160,70 @@ impl App {
fn on_key(&mut self, key: KeyEvent) {
match self.mode {
Mode::Normal => self.on_key_normal(key.code),
Mode::Adding(_) => self.on_key_adding(key),
Mode::Editing(_) => self.on_key_editing(key),
}
}
fn on_key_normal(&mut self, code: KeyCode) {
match code {
KeyCode::Char('q') | KeyCode::Esc => self.should_quit = true,
KeyCode::Char('a') => self.mode = Mode::Adding(AddForm::new()),
KeyCode::Char('a') => self.mode = Mode::Editing(CommandForm::new()),
KeyCode::Char('e') => self.start_editing(),
KeyCode::Char('d') => self.delete_selected(),
KeyCode::Up | KeyCode::Char('k') => self.select_previous(),
KeyCode::Down | KeyCode::Char('j') => self.select_next(),
_ => {}
}
}
fn on_key_adding(&mut self, key: KeyEvent) {
fn start_editing(&mut self) {
if let Some(command) = self.config.commands.get(self.selected) {
self.mode = Mode::Editing(CommandForm::edit(self.selected, command));
}
}
fn delete_selected(&mut self) {
if self.selected < self.config.commands.len() {
self.config.commands.remove(self.selected);
self.selected = self
.selected
.min(self.config.commands.len().saturating_sub(1));
let _ = self.config.save();
}
}
fn on_key_editing(&mut self, key: KeyEvent) {
match key.code {
KeyCode::Esc => self.mode = Mode::Normal,
KeyCode::Enter => self.confirm_adding(),
KeyCode::Enter => self.confirm_editing(),
KeyCode::Tab | KeyCode::Up | KeyCode::Down => {
if let Mode::Adding(form) = &mut self.mode {
if let Mode::Editing(form) = &mut self.mode {
form.toggle_field();
}
}
_ => {
if let Mode::Adding(form) = &mut self.mode {
if let Mode::Editing(form) = &mut self.mode {
form.current_mut().handle_event(&Event::Key(key));
}
}
}
}
fn confirm_adding(&mut self) {
if let Mode::Adding(form) = &self.mode {
fn confirm_editing(&mut self) {
if let Mode::Editing(form) = &self.mode {
let alias = form.alias.value().trim().to_string();
let command = form.command.value().trim().to_string();
let target = form.target;
if !command.is_empty() {
self.config.commands.push(Command { alias, command });
match target {
CommandFormTarget::New => self.config.commands.push(Command { alias, command }),
CommandFormTarget::Existing(index) => {
if let Some(existing) = self.config.commands.get_mut(index) {
existing.alias = alias;
existing.command = command;
}
}
}
let _ = self.config.save();
}
}
@ -216,13 +260,14 @@ fn run(mut terminal: DefaultTerminal) -> std::io::Result<()> {
fn draw(frame: &mut Frame, app: &App) {
draw_main(frame, app, frame.area());
if let Mode::Adding(form) = &app.mode {
draw_add_popup(frame, form);
match &app.mode {
Mode::Normal => {}
Mode::Editing(form) => draw_edit_popup(frame, form),
}
}
fn draw_main(frame: &mut Frame, app: &App, area: Rect) {
let instructions = " [a] Add connection [q/Esc] Quit ";
let instructions = " [a] Add [e] Edit [d] Delete [q/Esc] Quit ";
let block = Block::default()
.title(" sshmngrs ")
@ -252,7 +297,7 @@ fn draw_main(frame: &mut Frame, app: &App, area: Rect) {
frame.render_stateful_widget(list, area, &mut state);
}
fn draw_add_popup(frame: &mut Frame, form: &AddForm) {
fn draw_edit_popup(frame: &mut Frame, form: &CommandForm) {
let area = centered_rect(frame.area(), 60, 6);
frame.render_widget(Clear, area);