Break out ui code
This commit is contained in:
parent
536086c4a0
commit
00b7620157
72
src/main.rs
72
src/main.rs
@ -3,6 +3,7 @@
|
|||||||
#![allow(clippy::module_name_repetitions, clippy::cast_possible_truncation)]
|
#![allow(clippy::module_name_repetitions, clippy::cast_possible_truncation)]
|
||||||
|
|
||||||
mod command;
|
mod command;
|
||||||
|
mod ui;
|
||||||
use ansi4tui::bytes_to_text;
|
use ansi4tui::bytes_to_text;
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use anyhow::bail;
|
use anyhow::bail;
|
||||||
@ -239,7 +240,7 @@ fn run_app<B: Backend>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
terminal.draw(|f| ui(f, &app))?;
|
terminal.draw(|f| ui::draw(f, &app))?;
|
||||||
|
|
||||||
match event::read()? {
|
match event::read()? {
|
||||||
Event::Key(key) => match key.code {
|
Event::Key(key) => match key.code {
|
||||||
@ -309,72 +310,3 @@ fn run_app<B: Backend>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ui<B: Backend>(f: &mut Frame<B>, 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<B: Backend>(f: &mut Frame<B>, 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],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
99
src/ui.rs
Normal file
99
src/ui.rs
Normal file
@ -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<B: Backend>(f: &mut Frame<B>, 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<B: Backend>(f: &mut Frame<B>, 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],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user