Skip to content

Commit

Permalink
json editor part 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed Apr 22, 2024
1 parent 1b030d8 commit 5796a1e
Showing 1 changed file with 77 additions and 3 deletions.
80 changes: 77 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -9,7 +11,7 @@ use std::error::Error;
use std::io;

mod app;
use app::App;
use crate::app::App;

fn main() -> Result<(), Box<dyn Error>> {
enable_raw_mode()?;
Expand Down Expand Up @@ -46,7 +48,79 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, 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);
}
}
}
}
_ => {}
},
_ => {}
}
}
}
}

0 comments on commit 5796a1e

Please sign in to comment.