From 6f1e684e7e58cf59501ee81a86c3caa69bc3cc0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Macio=C5=82ek?= Date: Thu, 25 Apr 2024 21:39:45 +0200 Subject: [PATCH] json editor part 5 --- src/main.rs | 26 +++++++++------ src/ui.rs | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 109 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index a2ccc60..cc45b76 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,21 @@ -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, +use std::{error::Error, io}; + +use crossterm::{ + event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind}, + execute, + terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, +}; +use ratatui::{ + backend::{Backend, CrosstermBackend}, + Terminal, }; -use ratatui::backend::CrosstermBackend; -use ratatui::Terminal; -use std::error::Error; -use std::io; mod app; -use crate::app::App; +mod ui; +use crate::{ + app::{App, CurrentScreen, CurrentlyEditing}, + ui::ui, +}; fn main() -> Result<(), Box> { enable_raw_mode()?; diff --git a/src/ui.rs b/src/ui.rs index 8b06335..de63737 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,3 +1,4 @@ +use color_eyre::owo_colors::OwoColorize; use ratatui::{ layout::{Constraint, Direction, Layout, Rect}, style::{Color, Style}, @@ -8,7 +9,98 @@ use ratatui::{ use crate::app::{App, CurrentScreen, CurrentlyEditing}; -pub fn ui(f: &mut Frame, app: &App) {} +pub fn ui(f: &mut Frame, app: &App) { + let chunks = Layout::default() + .direction(Direction::Vertical) + .constraints([ + Constraint::Length(3), + Constraint::Min(1), + Constraint::Length(3), + ]) + .split(f.size()); + + let title_block = Block::default() + .borders(Borders::ALL) + .style(Style::default()); + + let title = Paragraph::new(Text::styled( + "Create New Json", + Style::default().fg(Color::Green), + )) + .block(title_block); + + f.render_widget(title, chunks[0]); + + let mut list_items = Vec::::new(); + + for key in app.pairs.keys() { + list_items.push(ListItem::new(Line::from(Span::styled( + format!("{: <25} : {}", key, app.pairs.get(key).unwrap()), + Style::default().fg(Color::Yellow), + )))); + } + + let list = List::new(list_items); + + f.render_widget(list, chunks[1]); + + let current_navigation_text = vec![ + match app.current_screen { + CurrentScreen::Main => Span::styled("Normal Mode", Style::default().fg(Color::Green)), + CurrentScreen::Editing => { + Span::styled("Editing Mode", Style::default().fg(Color::Yellow)) + } + CurrentScreen::Exiting => Span::styled("Exiting", Style::default().fg(Color::LightRed)), + } + .to_owned(), + Span::styled(" | ", Style::default().fg(Color::White)), + { + if let Some(editing) = &app.currently_editing { + match editing { + CurrentlyEditing::Key => { + Span::styled("Editing Json Key", Style::default().fg(Color::Green)) + } + CurrentlyEditing::Value => { + Span::styled("Editing Json Value", Style::default().fg(Color::LightGreen)) + } + } + } else { + Span::styled("Not Editing Anything", Style::default().fg(Color::DarkGray)) + } + }, + ]; + + let mode_footer = Paragraph::new(Line::from(current_navigation_text)) + .block(Block::default().borders(Borders::ALL)); + + let current_keys_hint = { + match app.current_screen { + CurrentScreen::Main => Span::styled( + "(q) to quit / (e) to make new pair", + Style::default().fg(Color::Red), + ), + CurrentScreen::Editing => Span::styled( + "(ESC) to cancel/(Tab) to switch boxes/enter to complete", + Style::default().fg(Color::Red), + ), + CurrentScreen::Exiting => Span::styled( + "(q) to quit / (e) to make new pair", + Style::default().fg(Color::Red), + ), + } + }; + + let key_notes_footer = + Paragraph::new(Line::from(current_keys_hint)).block(Block::default().borders(Borders::ALL)); + + let footer_chunks = Layout::default() + .direction(Direction::Horizontal) + .constraints([Constraint::Percentage(50), Constraint::Percentage(50)]) + .split(chunks[2]); + + f.render_widget(mode_footer, footer_chunks[0]); + f.render_widget(key_notes_footer, footer_chunks[1]); +} fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { let popup_layout = Layout::default() @@ -27,5 +119,5 @@ fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { Constraint::Percentage(percent_x), Constraint::Percentage((100 - percent_x) / 2), ]) - .split(popup_layout[1])[1] // Return the middle chunk + .split(popup_layout[1])[1] }