Skip to content

Commit

Permalink
Merge 3b718df into 6884074
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliot00 committed Dec 6, 2021
2 parents 6884074 + 3b718df commit 28a5c3a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 50 deletions.
7 changes: 3 additions & 4 deletions quake_tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(

if let Event::Key(key) = event::read()? {
match app.mode {
Mode::Normal => match key.code {
KeyCode::Char(':') => {
Mode::Normal => {
if let KeyCode::Char(':') = key.code {
app.mode = Mode::Command;
}
_ => {}
},
}
Mode::Command => match key.code {
KeyCode::Enter => {
let command: String = app.command.drain(..).collect();
Expand Down
87 changes: 41 additions & 46 deletions quake_tui/src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::app::{App, MainWidget, Mode};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::{fs, io};
use tui::backend::Backend;
use tui::layout::{Constraint, Corner, Direction, Layout, Rect};
use tui::layout::{Alignment, Constraint, Corner, Direction, Layout, Rect};
use tui::style::{Color, Modifier, Style};
use tui::text::{Span, Spans, Text};
use tui::text::{Span, Spans};
use tui::widgets::{Block, Borders, List, ListItem, Paragraph};
use tui::Frame;
use unicode_width::UnicodeWidthStr;
Expand All @@ -13,66 +13,61 @@ pub fn draw<B: Backend>(f: &mut Frame<B>, app: &mut App) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(2)
.constraints(
[
Constraint::Length(1),
Constraint::Length(3),
Constraint::Min(1),
]
.as_ref(),
)
.constraints([Constraint::Length(3), Constraint::Min(1)].as_ref())
.split(f.size());
let (msg, style) = match app.mode {
Mode::Normal => (
vec![
Span::raw("Press "),
Span::styled(":", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to start command mode."),
],
Style::default().add_modifier(Modifier::RAPID_BLINK),
),
Mode::Command => (
vec![
Span::raw("Press "),
Span::styled("Esc", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to quit command mode, "),
Span::styled("Enter", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to record the message"),
],
Style::default(),
),
};
let mut text = Text::from(Spans::from(msg));
text.patch_style(style);
let help_message = Paragraph::new(text);
f.render_widget(help_message, chunks[0]);
let action = Paragraph::new(app.command.as_ref())
let command_bar = Paragraph::new(app.command.as_ref())
.style(match app.mode {
Mode::Normal => Style::default(),
Mode::Command => Style::default().fg(Color::Yellow),
})
.block(Block::default().borders(Borders::ALL).title("Action"));
f.render_widget(action, chunks[1]);
f.render_widget(command_bar, chunks[0]);
match app.mode {
Mode::Normal => {}
Mode::Command => f.set_cursor(
chunks[1].x + app.command.width() as u16 + 1,
chunks[1].y + 1,
chunks[0].x + app.command.width() as u16 + 1,
chunks[0].y + 1,
),
}

draw_main(app, f, chunks[2]);
draw_main(app, f, chunks[1]);
}

fn draw_main<B>(app: &App, frame: &mut Frame<B>, area: Rect)
where
B: Backend,
{
match app.main_widget {
MainWidget::Home => frame.render_widget(Block::default(), area),
MainWidget::Home => {
let help_messages = vec![
Spans::from(vec![
Span::raw("Press "),
Span::styled(":", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" into command mode, "),
Span::styled("Esc", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" back to normal mode."),
]),
Spans::from(vec![
Span::raw("Command "),
Span::styled("listAll", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" list all workspace."),
]),
Spans::from(vec![
Span::raw("Command "),
Span::styled("quit", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" quit quake app."),
]),
];
frame.render_widget(
Paragraph::new(help_messages)
.block(Block::default().title("Main").borders(Borders::ALL))
.alignment(Alignment::Center),
area,
)
}
MainWidget::Dirs => {
let entry_dirs: Vec<ListItem> = list_entries_dirs()
.unwrap_or(vec![])
let entry_dirs: Vec<ListItem> = list_workspaces()
.unwrap_or_default()
.iter()
.rev()
.map(|dir| {
Expand All @@ -92,17 +87,17 @@ where
}
}

fn list_entries_dirs() -> io::Result<Vec<PathBuf>> {
fn list_workspaces() -> io::Result<Vec<PathBuf>> {
let mut entries = fs::read_dir(".")?
.map(|res| res.map(|e| e.path()))
.filter(|path| path.as_ref().map(is_entries_dir).unwrap_or(false))
.filter(|path| path.as_deref().map(is_workspace).unwrap_or(false))
.collect::<Result<Vec<_>, io::Error>>()?;

entries.sort();

Ok(entries)
}

fn is_entries_dir(path: &PathBuf) -> bool {
fn is_workspace(path: &Path) -> bool {
path.is_dir() && path.join("entries.csv").exists()
}

0 comments on commit 28a5c3a

Please sign in to comment.