2021-07-29 10:47:36 +00:00
|
|
|
use super::{Component, DrawableComponent, EventState};
|
|
|
|
use crate::components::command::CommandInfo;
|
2021-07-31 16:03:39 +00:00
|
|
|
use crate::config::KeyConfig;
|
2021-07-29 10:47:36 +00:00
|
|
|
use crate::event::Key;
|
2021-09-06 13:29:24 +00:00
|
|
|
use crate::version::Version;
|
2021-07-29 10:47:36 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use itertools::Itertools;
|
|
|
|
use std::convert::From;
|
|
|
|
use tui::{
|
|
|
|
backend::Backend,
|
|
|
|
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
|
|
|
style::{Color, Modifier, Style},
|
|
|
|
text::{Span, Spans},
|
|
|
|
widgets::{Block, BorderType, Borders, Clear, Paragraph},
|
|
|
|
Frame,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub struct HelpComponent {
|
|
|
|
cmds: Vec<CommandInfo>,
|
|
|
|
visible: bool,
|
|
|
|
selection: u16,
|
2021-07-31 16:03:39 +00:00
|
|
|
key_config: KeyConfig,
|
2021-07-29 10:47:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DrawableComponent for HelpComponent {
|
2021-09-17 06:16:31 +00:00
|
|
|
fn draw<B: Backend>(&self, f: &mut Frame<B>, _area: Rect, _focused: bool) -> Result<()> {
|
2021-07-29 10:47:36 +00:00
|
|
|
if self.visible {
|
|
|
|
const SIZE: (u16, u16) = (65, 24);
|
|
|
|
let scroll_threshold = SIZE.1 / 3;
|
|
|
|
let scroll = self.selection.saturating_sub(scroll_threshold);
|
|
|
|
|
|
|
|
let area = Rect::new(
|
|
|
|
(f.size().width.saturating_sub(SIZE.0)) / 2,
|
|
|
|
(f.size().height.saturating_sub(SIZE.1)) / 2,
|
|
|
|
SIZE.0.min(f.size().width),
|
|
|
|
SIZE.1.min(f.size().height),
|
|
|
|
);
|
|
|
|
|
|
|
|
f.render_widget(Clear, area);
|
|
|
|
f.render_widget(
|
|
|
|
Block::default()
|
|
|
|
.title("Help")
|
|
|
|
.borders(Borders::ALL)
|
|
|
|
.border_type(BorderType::Thick),
|
|
|
|
area,
|
|
|
|
);
|
|
|
|
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.vertical_margin(1)
|
|
|
|
.horizontal_margin(1)
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.constraints([Constraint::Min(1), Constraint::Length(1)].as_ref())
|
|
|
|
.split(area);
|
|
|
|
|
|
|
|
f.render_widget(
|
|
|
|
Paragraph::new(self.get_text(chunks[0].width as usize)).scroll((scroll, 0)),
|
|
|
|
chunks[0],
|
|
|
|
);
|
|
|
|
|
|
|
|
f.render_widget(
|
|
|
|
Paragraph::new(Spans::from(vec![Span::styled(
|
2021-09-06 13:29:24 +00:00
|
|
|
format!("gobang {}", Version::new()),
|
2021-07-29 10:47:36 +00:00
|
|
|
Style::default(),
|
|
|
|
)]))
|
|
|
|
.alignment(Alignment::Right),
|
|
|
|
chunks[1],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for HelpComponent {
|
2021-07-31 16:03:39 +00:00
|
|
|
fn commands(&self, _out: &mut Vec<CommandInfo>) {}
|
2021-07-29 10:47:36 +00:00
|
|
|
|
|
|
|
fn event(&mut self, key: Key) -> Result<EventState> {
|
|
|
|
if self.visible {
|
2021-07-31 16:03:39 +00:00
|
|
|
if key == self.key_config.exit_popup {
|
|
|
|
self.hide();
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
} else if key == self.key_config.scroll_down {
|
|
|
|
self.scroll_selection(true);
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
} else if key == self.key_config.scroll_up {
|
|
|
|
self.scroll_selection(false);
|
|
|
|
return Ok(EventState::Consumed);
|
2021-07-29 10:47:36 +00:00
|
|
|
}
|
|
|
|
return Ok(EventState::NotConsumed);
|
2021-07-31 16:03:39 +00:00
|
|
|
} else if key == self.key_config.open_help {
|
2021-07-29 10:47:36 +00:00
|
|
|
self.show()?;
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
}
|
|
|
|
Ok(EventState::NotConsumed)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hide(&mut self) {
|
|
|
|
self.visible = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show(&mut self) -> Result<()> {
|
|
|
|
self.visible = true;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HelpComponent {
|
2021-07-31 16:03:39 +00:00
|
|
|
pub const fn new(key_config: KeyConfig) -> Self {
|
2021-07-29 10:47:36 +00:00
|
|
|
Self {
|
|
|
|
cmds: vec![],
|
|
|
|
visible: false,
|
|
|
|
selection: 0,
|
2021-07-31 16:03:39 +00:00
|
|
|
key_config,
|
2021-07-29 10:47:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_cmds(&mut self, cmds: Vec<CommandInfo>) {
|
|
|
|
self.cmds = cmds
|
|
|
|
.into_iter()
|
|
|
|
.filter(|e| !e.text.hide_help)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
}
|
|
|
|
|
2021-07-31 16:03:39 +00:00
|
|
|
fn scroll_selection(&mut self, inc: bool) {
|
2021-07-29 10:47:36 +00:00
|
|
|
let mut new_selection = self.selection;
|
|
|
|
|
|
|
|
new_selection = if inc {
|
|
|
|
new_selection.saturating_add(1)
|
|
|
|
} else {
|
|
|
|
new_selection.saturating_sub(1)
|
|
|
|
};
|
|
|
|
new_selection = new_selection.max(0);
|
|
|
|
|
|
|
|
self.selection = new_selection.min(self.cmds.len().saturating_sub(1) as u16);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_text(&self, width: usize) -> Vec<Spans> {
|
|
|
|
let mut txt: Vec<Spans> = Vec::new();
|
|
|
|
|
|
|
|
let mut processed = 0;
|
|
|
|
|
|
|
|
for (key, group) in &self.cmds.iter().group_by(|e| e.text.group) {
|
|
|
|
txt.push(Spans::from(Span::styled(
|
|
|
|
key.to_string(),
|
|
|
|
Style::default().add_modifier(Modifier::REVERSED),
|
|
|
|
)));
|
|
|
|
|
|
|
|
for command_info in group {
|
|
|
|
let is_selected = self.selection == processed;
|
|
|
|
processed += 1;
|
|
|
|
|
|
|
|
txt.push(Spans::from(Span::styled(
|
2021-07-31 16:03:39 +00:00
|
|
|
format!(" {}{:w$}", command_info.text.name, w = width),
|
2021-07-29 10:47:36 +00:00
|
|
|
if is_selected {
|
|
|
|
Style::default().bg(Color::Blue)
|
|
|
|
} else {
|
|
|
|
Style::default()
|
|
|
|
},
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
txt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2021-07-31 16:03:39 +00:00
|
|
|
use super::{Color, CommandInfo, HelpComponent, KeyConfig, Modifier, Span, Spans, Style};
|
2021-07-29 10:47:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_text() {
|
|
|
|
let width = 3;
|
2021-07-31 16:03:39 +00:00
|
|
|
let key_config = KeyConfig::default();
|
|
|
|
let mut component = HelpComponent::new(key_config.clone());
|
2021-07-29 10:47:36 +00:00
|
|
|
component.set_cmds(vec![
|
2021-07-31 16:03:39 +00:00
|
|
|
CommandInfo::new(crate::components::command::scroll(&key_config)),
|
|
|
|
CommandInfo::new(crate::components::command::filter(&key_config)),
|
2021-07-29 10:47:36 +00:00
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
component.get_text(width),
|
|
|
|
vec![
|
|
|
|
Spans::from(Span::styled(
|
|
|
|
"-- General --",
|
|
|
|
Style::default().add_modifier(Modifier::REVERSED)
|
|
|
|
)),
|
|
|
|
Spans::from(Span::styled(
|
2021-07-31 16:03:39 +00:00
|
|
|
" Scroll up/down/left/right [k,j,h,l] 3",
|
2021-07-29 10:47:36 +00:00
|
|
|
Style::default().bg(Color::Blue)
|
|
|
|
)),
|
2021-07-31 16:03:39 +00:00
|
|
|
Spans::from(Span::styled(" Filter [/] 3", Style::default()))
|
2021-07-29 10:47:36 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|