mirror of https://github.com/TaKO8Ki/gobang
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
4.8 KiB
Rust
142 lines
4.8 KiB
Rust
use crate::app::InputMode;
|
|
use crate::App;
|
|
use crate::StatefulTable;
|
|
use tui::{
|
|
backend::Backend,
|
|
layout::{Constraint, Direction, Layout, Rect},
|
|
style::{Color, Modifier, Style},
|
|
symbols,
|
|
text::{Span, Spans, Text},
|
|
widgets::canvas::{Canvas, Line, Map, MapResolution, Rectangle},
|
|
widgets::{
|
|
Axis, BarChart, Block, Borders, Cell, Chart, Dataset, Gauge, LineGauge, List, ListItem,
|
|
Paragraph, Row, Sparkline, Table, Tabs, Wrap,
|
|
},
|
|
Frame,
|
|
};
|
|
use unicode_width::UnicodeWidthStr;
|
|
|
|
pub fn draw<B: Backend>(
|
|
f: &mut Frame<'_, B>,
|
|
app: &mut App,
|
|
table: &mut StatefulTable<'_>,
|
|
) -> anyhow::Result<()> {
|
|
let chunks = Layout::default()
|
|
.direction(Direction::Vertical)
|
|
.margin(2)
|
|
.constraints([Constraint::Percentage(15), Constraint::Percentage(85)])
|
|
.direction(Direction::Horizontal)
|
|
.split(f.size());
|
|
|
|
let tables: Vec<ListItem> = app
|
|
.tables
|
|
.iter()
|
|
.map(|i| ListItem::new(vec![Spans::from(Span::raw(i))]))
|
|
.collect();
|
|
let tasks = List::new(tables)
|
|
.block(Block::default().borders(Borders::ALL).title("Tables"))
|
|
.highlight_style(Style::default().add_modifier(Modifier::BOLD))
|
|
.highlight_symbol("> ");
|
|
f.render_widget(tasks, chunks[0]);
|
|
|
|
let chunks = Layout::default()
|
|
.direction(Direction::Vertical)
|
|
.constraints(
|
|
[
|
|
Constraint::Length(1),
|
|
Constraint::Length(3),
|
|
Constraint::Min(1),
|
|
]
|
|
.as_ref(),
|
|
)
|
|
.split(chunks[1]);
|
|
|
|
let (msg, style) = match app.input_mode {
|
|
InputMode::Normal => (
|
|
vec![
|
|
Span::raw("Press "),
|
|
Span::styled("q", Style::default().add_modifier(Modifier::BOLD)),
|
|
Span::raw(" to exit, "),
|
|
Span::styled("e", Style::default().add_modifier(Modifier::BOLD)),
|
|
Span::raw(" to start editing."),
|
|
],
|
|
Style::default().add_modifier(Modifier::RAPID_BLINK),
|
|
),
|
|
InputMode::Editing => (
|
|
vec![
|
|
Span::raw("Press "),
|
|
Span::styled("Esc", Style::default().add_modifier(Modifier::BOLD)),
|
|
Span::raw(" to stop editing, "),
|
|
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 input = Paragraph::new(app.input.as_ref())
|
|
.style(match app.input_mode {
|
|
InputMode::Normal => Style::default(),
|
|
InputMode::Editing => Style::default().fg(Color::Yellow),
|
|
})
|
|
.block(Block::default().borders(Borders::ALL).title("Input"));
|
|
f.render_widget(input, chunks[1]);
|
|
match app.input_mode {
|
|
InputMode::Normal =>
|
|
// Hide the cursor. `Frame` does this by default, so we don't need to do anything here
|
|
{}
|
|
|
|
InputMode::Editing => {
|
|
// Make the cursor visible and ask tui-rs to put it at the specified coordinates after rendering
|
|
f.set_cursor(
|
|
// Put cursor past the end of the input text
|
|
chunks[1].x + app.input.width() as u16 + 1,
|
|
// Move one line down, from the border to the input line
|
|
chunks[1].y + 1,
|
|
)
|
|
}
|
|
}
|
|
|
|
let selected_style = Style::default().add_modifier(Modifier::REVERSED);
|
|
let normal_style = Style::default().bg(Color::Blue);
|
|
let header_cells = [
|
|
"Header1", "Header2", "Header3", "Header4", "Header5", "Header6",
|
|
]
|
|
.iter()
|
|
.map(|h| Cell::from(*h).style(Style::default().fg(Color::Red)));
|
|
let header = Row::new(header_cells)
|
|
.style(normal_style)
|
|
.height(1)
|
|
.bottom_margin(1);
|
|
let rows = app.messages.iter().map(|item| {
|
|
let height = item
|
|
.iter()
|
|
.map(|content| content.chars().filter(|c| *c == '\n').count())
|
|
.max()
|
|
.unwrap_or(0)
|
|
+ 1;
|
|
let cells = item.iter().map(|c| Cell::from(c.to_string()));
|
|
Row::new(cells).height(height as u16).bottom_margin(1)
|
|
});
|
|
let t = Table::new(rows)
|
|
.header(header)
|
|
.block(Block::default().borders(Borders::ALL).title("Table"))
|
|
.highlight_style(selected_style)
|
|
.highlight_symbol(">> ")
|
|
.widths(&[
|
|
Constraint::Percentage(10),
|
|
Constraint::Percentage(10),
|
|
Constraint::Percentage(10),
|
|
Constraint::Percentage(10),
|
|
Constraint::Percentage(10),
|
|
Constraint::Percentage(10),
|
|
]);
|
|
f.render_stateful_widget(t, chunks[2], &mut table.state);
|
|
|
|
Ok(())
|
|
}
|