From 00b7620157fd7663b1b720233082694990ad6930 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Sat, 17 Dec 2022 11:33:52 -0500 Subject: [PATCH] Break out ui code --- src/main.rs | 72 ++------------------------------------ src/ui.rs | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 70 deletions(-) create mode 100644 src/ui.rs diff --git a/src/main.rs b/src/main.rs index 97e7da6..43f9aa5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ #![allow(clippy::module_name_repetitions, clippy::cast_possible_truncation)] mod command; +mod ui; use ansi4tui::bytes_to_text; use anyhow::anyhow; use anyhow::bail; @@ -239,7 +240,7 @@ fn run_app( } loop { - terminal.draw(|f| ui(f, &app))?; + terminal.draw(|f| ui::draw(f, &app))?; match event::read()? { Event::Key(key) => match key.code { @@ -309,72 +310,3 @@ fn run_app( } } } - -fn ui(f: &mut Frame, app: &App) { - let vertical_chunks = Layout::default() - .direction(Direction::Vertical) - .constraints([Constraint::Min(3), Constraint::Length(3)].as_ref()) - .split(f.size()); - - let chunks = Layout::default() - .direction(Direction::Horizontal) - .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) - .split(vertical_chunks[0]); - - f.render_widget( - Paragraph::new(app.text_orig.as_str()) - .block(Block::default().title("Orig").borders(Borders::ALL)), - chunks[0], - ); - f.render_widget( - Paragraph::new(app.cmdline.as_ref()).block( - Block::default() - .title(format!( - "Cmdline ({}{}{})", - app.command, - if app.hidden_options.is_empty() { - "" - } else { - " " - }, - app.hidden_options.join(" ") - )) - .borders(Borders::ALL), - ), - vertical_chunks[1], - ); - - // Render the output in the outpout region - ui_output(f, chunks[1], app); - - f.set_cursor( - vertical_chunks[1].x + app.cmdline_position + 1, - vertical_chunks[1].y + 1, - ); -} - -fn ui_output(f: &mut Frame, output_region: Rect, app: &App) { - if app.command_result.status_success { - f.render_widget( - Paragraph::new(bytes_to_text(&app.command_result.stdout)) - .block(Block::default().title("New").borders(Borders::ALL)), - output_region, - ); - } else { - let chunks = Layout::default() - .direction(Direction::Vertical) - .constraints([Constraint::Percentage(50), Constraint::Percentage(50)]) - .split(output_region); - - f.render_widget( - Paragraph::new(bytes_to_text(&app.command_result.stdout)) - .block(Block::default().title("Output").borders(Borders::ALL)), - chunks[0], - ); - f.render_widget( - Paragraph::new(bytes_to_text(&app.command_result.stderr)) - .block(Block::default().title("Error").borders(Borders::ALL)), - chunks[1], - ); - } -} diff --git a/src/ui.rs b/src/ui.rs new file mode 100644 index 0000000..fc9d922 --- /dev/null +++ b/src/ui.rs @@ -0,0 +1,99 @@ +use crate::command; +use crate::App; +use ansi4tui::bytes_to_text; +use anyhow::anyhow; +use anyhow::bail; +use anyhow::Result; +use command::CommandResult; +use crossterm::event::DisableBracketedPaste; +use crossterm::event::EnableBracketedPaste; +use std::str::FromStr; +use std::{ + io::{self, Write}, + process::{Command, Stdio}, + sync::Arc, + thread, + time::Duration, +}; +use tui::text::Text; +use tui::{ + backend::{Backend, CrosstermBackend}, + layout::{Constraint, Direction, Layout, Rect}, + widgets::{Block, Borders, Paragraph, Widget}, + Frame, Terminal, +}; + +use crossterm::{ + event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, + execute, + terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, +}; + +pub fn draw(f: &mut Frame, app: &App) { + let vertical_chunks = Layout::default() + .direction(Direction::Vertical) + .constraints([Constraint::Min(3), Constraint::Length(3)].as_ref()) + .split(f.size()); + + let chunks = Layout::default() + .direction(Direction::Horizontal) + .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) + .split(vertical_chunks[0]); + + f.render_widget( + Paragraph::new(app.text_orig.as_str()) + .block(Block::default().title("Orig").borders(Borders::ALL)), + chunks[0], + ); + f.render_widget( + Paragraph::new(app.cmdline.as_ref()).block( + Block::default() + .title(format!( + "Cmdline ({}{}{})", + app.command, + if app.hidden_options.is_empty() { + "" + } else { + " " + }, + app.hidden_options.join(" ") + )) + .borders(Borders::ALL), + ), + vertical_chunks[1], + ); + + // Render the output in the outpout region + ui_output(f, chunks[1], app); + + f.set_cursor( + vertical_chunks[1].x + app.cmdline_position + 1, + vertical_chunks[1].y + 1, + ); +} + +fn ui_output(f: &mut Frame, output_region: Rect, app: &App) { + if app.command_result.status_success { + f.render_widget( + Paragraph::new(bytes_to_text(&app.command_result.stdout)) + .block(Block::default().title("New").borders(Borders::ALL)), + output_region, + ); + } else { + let chunks = Layout::default() + .direction(Direction::Vertical) + .constraints([Constraint::Percentage(50), Constraint::Percentage(50)]) + .split(output_region); + + f.render_widget( + Paragraph::new(bytes_to_text(&app.command_result.stdout)) + .block(Block::default().title("Output").borders(Borders::ALL)), + chunks[0], + ); + f.render_widget( + Paragraph::new(bytes_to_text(&app.command_result.stderr)) + .block(Block::default().title("Error").borders(Borders::ALL)), + chunks[1], + ); + } +}