From 5796a1e8fc3e9e1c48c8d1930bce253c69f70dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Macio=C5=82ek?= Date: Mon, 22 Apr 2024 21:05:43 +0200 Subject: [PATCH] json editor part 3 --- src/main.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index d198b53..a2ccc60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ -use crossterm::event::{DisableMouseCapture, EnableMouseCapture}; +use app::{CurrentScreen, CurrentlyEditing}; +use color_eyre::eyre::Ok; +use crossterm::event::{self, DisableMouseCapture, EnableMouseCapture, KeyCode, KeyEventKind}; use crossterm::execute; use crossterm::terminal::{ disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen, @@ -9,7 +11,7 @@ use std::error::Error; use std::io; mod app; -use app::App; +use crate::app::App; fn main() -> Result<(), Box> { enable_raw_mode()?; @@ -46,7 +48,79 @@ fn run_app(terminal: &mut Terminal, app: &mut App) -> io::Result< terminal.draw(|f| ui(f, app))?; if let Event::Key(key) = event::read()? { - dbg!(key.code) + if key.kind == event::KeyEventKind::Release { + continue; + } + + match app.current_screen { + CurrentScreen::Main => match key.code { + KeyCode::Char('e') => { + app.current_screen = CurrentScreen::Editing; + app.currently_editing = Some(CurrentlyEditing::Key); + } + KeyCode::Char('q') => { + app.current_screen = CurrentScreen::Exiting; + } + _ => {} + }, + CurrentScreen::Exiting => match key.code { + KeyCode::Char('y') => { + return Ok(true); + } + KeyCode::Char('n') | KeyCode::Char('q') => { + return Ok(false); + } + _ => {} + }, + CurrentScreen::Editing if key.kind == KeyEventKind::Press => match key.code { + KeyCode::Enter => { + if let Some(editing) = &app.currently_editing { + match editing { + CurrentlyEditing::Key => { + app.currently_editing = Some(CurrentlyEditing::Value); + } + CurrentlyEditing::Value => { + app.save_key_value(); + app.current_screen = CurrentScreen::Main; + } + } + } + } + KeyCode::Backspace => { + if let Some(editing) = &app.currently_editing { + match editing { + CurrentlyEditing::Key => { + app.key_input.pop(); + } + CurrentlyEditing::Value => { + app.value_input.pop(); + } + } + } + } + KeyCode::Esc => { + app.current_screen = CurrentScreen::Main; + app.currently_editing = None; + } + KeyCode::Tab => { + app.toggle_edititng(); + } + KeyCode::Char(value) => { + if let Some(editing) = &app.currently_editing { + match editing { + CurrentlyEditing::Key => { + app.key_input.push(value); + } + CurrentlyEditing::Value => { + app.value_input.push(value); + } + } + } + } + _ => {} + }, + _ => {} + } } } }