Skip to content

Commit

Permalink
json editor part 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed Apr 25, 2024
1 parent c3a7fc4 commit 6f1e684
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 13 deletions.
26 changes: 15 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
enable_raw_mode()?;
Expand Down
96 changes: 94 additions & 2 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use color_eyre::owo_colors::OwoColorize;
use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Style},
Expand All @@ -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::<ListItem>::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()
Expand All @@ -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]
}

0 comments on commit 6f1e684

Please sign in to comment.