mirror of https://github.com/TaKO8Ki/gobang
Filter completion (#88)
* create completion component * add move_up/down * fix variable name * pass config * create debug component * remov set * add reserved words * remove equal * allow dead code * always reset offset * apply completion candidates correctly * implement selected_candidate, word * fix clippy warnings * complete * add tests for complete * fix variable name * fmt * add tests for `filterd_candidates` * add "IN" to reserved words * remove "IN" * add test cases * add debug_assertions * return complete directly * add s * make input field private * update gobang.gifpull/102/head
parent
7796947b76
commit
6615a235a7
Binary file not shown.
Before Width: | Height: | Size: 3.6 MiB After Width: | Height: | Size: 2.3 MiB |
@ -0,0 +1,178 @@
|
|||||||
|
use super::{Component, EventState, MovableComponent};
|
||||||
|
use crate::components::command::CommandInfo;
|
||||||
|
use crate::config::KeyConfig;
|
||||||
|
use crate::event::Key;
|
||||||
|
use anyhow::Result;
|
||||||
|
use tui::{
|
||||||
|
backend::Backend,
|
||||||
|
layout::Rect,
|
||||||
|
style::{Color, Style},
|
||||||
|
widgets::{Block, Borders, Clear, List, ListItem, ListState},
|
||||||
|
Frame,
|
||||||
|
};
|
||||||
|
|
||||||
|
const RESERVED_WORDS: &[&str] = &["IN", "AND", "OR", "NOT", "NULL", "IS"];
|
||||||
|
|
||||||
|
pub struct CompletionComponent {
|
||||||
|
key_config: KeyConfig,
|
||||||
|
state: ListState,
|
||||||
|
word: String,
|
||||||
|
candidates: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CompletionComponent {
|
||||||
|
pub fn new(key_config: KeyConfig, word: impl Into<String>) -> Self {
|
||||||
|
Self {
|
||||||
|
key_config,
|
||||||
|
state: ListState::default(),
|
||||||
|
word: word.into(),
|
||||||
|
candidates: RESERVED_WORDS.iter().map(|w| w.to_string()).collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self, word: impl Into<String>) {
|
||||||
|
self.word = word.into();
|
||||||
|
self.state.select(None);
|
||||||
|
self.state.select(Some(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next(&mut self) {
|
||||||
|
let i = match self.state.selected() {
|
||||||
|
Some(i) => {
|
||||||
|
if i >= self.filterd_candidates().count() - 1 {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
i + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => 0,
|
||||||
|
};
|
||||||
|
self.state.select(Some(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn previous(&mut self) {
|
||||||
|
let i = match self.state.selected() {
|
||||||
|
Some(i) => {
|
||||||
|
if i == 0 {
|
||||||
|
self.filterd_candidates().count() - 1
|
||||||
|
} else {
|
||||||
|
i - 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => 0,
|
||||||
|
};
|
||||||
|
self.state.select(Some(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn filterd_candidates(&self) -> impl Iterator<Item = &String> {
|
||||||
|
self.candidates.iter().filter(move |c| {
|
||||||
|
(c.starts_with(self.word.to_lowercase().as_str())
|
||||||
|
|| c.starts_with(self.word.to_uppercase().as_str()))
|
||||||
|
&& !self.word.is_empty()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn selected_candidate(&self) -> Option<String> {
|
||||||
|
self.filterd_candidates()
|
||||||
|
.collect::<Vec<&String>>()
|
||||||
|
.get(self.state.selected()?)
|
||||||
|
.map(|c| c.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn word(&self) -> String {
|
||||||
|
self.word.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MovableComponent for CompletionComponent {
|
||||||
|
fn draw<B: Backend>(
|
||||||
|
&mut self,
|
||||||
|
f: &mut Frame<B>,
|
||||||
|
area: Rect,
|
||||||
|
_focused: bool,
|
||||||
|
x: u16,
|
||||||
|
y: u16,
|
||||||
|
) -> Result<()> {
|
||||||
|
if !self.word.is_empty() {
|
||||||
|
let width = 30;
|
||||||
|
let candidates = self
|
||||||
|
.filterd_candidates()
|
||||||
|
.map(|c| ListItem::new(c.to_string()))
|
||||||
|
.collect::<Vec<ListItem>>();
|
||||||
|
if candidates.clone().is_empty() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let candidate_list = List::new(candidates.clone())
|
||||||
|
.block(Block::default().borders(Borders::ALL))
|
||||||
|
.highlight_style(Style::default().bg(Color::Blue))
|
||||||
|
.style(Style::default());
|
||||||
|
|
||||||
|
let area = Rect::new(
|
||||||
|
area.x + x,
|
||||||
|
area.y + y + 2,
|
||||||
|
width.min(f.size().width),
|
||||||
|
candidates.len().min(5) as u16 + 2,
|
||||||
|
);
|
||||||
|
f.render_widget(Clear, area);
|
||||||
|
f.render_stateful_widget(candidate_list, area, &mut self.state);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Component for CompletionComponent {
|
||||||
|
fn commands(&self, _out: &mut Vec<CommandInfo>) {}
|
||||||
|
|
||||||
|
fn event(&mut self, key: Key) -> Result<EventState> {
|
||||||
|
if key == self.key_config.move_down {
|
||||||
|
self.next();
|
||||||
|
return Ok(EventState::Consumed);
|
||||||
|
} else if key == self.key_config.move_up {
|
||||||
|
self.previous();
|
||||||
|
return Ok(EventState::Consumed);
|
||||||
|
}
|
||||||
|
Ok(EventState::NotConsumed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::{CompletionComponent, KeyConfig};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_filterd_candidates_lowercase() {
|
||||||
|
assert_eq!(
|
||||||
|
CompletionComponent::new(KeyConfig::default(), "an")
|
||||||
|
.filterd_candidates()
|
||||||
|
.collect::<Vec<&String>>(),
|
||||||
|
vec![&"AND".to_string()]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_filterd_candidates_uppercase() {
|
||||||
|
assert_eq!(
|
||||||
|
CompletionComponent::new(KeyConfig::default(), "AN")
|
||||||
|
.filterd_candidates()
|
||||||
|
.collect::<Vec<&String>>(),
|
||||||
|
vec![&"AND".to_string()]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_filterd_candidates_multiple_candidates() {
|
||||||
|
assert_eq!(
|
||||||
|
CompletionComponent::new(KeyConfig::default(), "n")
|
||||||
|
.filterd_candidates()
|
||||||
|
.collect::<Vec<&String>>(),
|
||||||
|
vec![&"NOT".to_string(), &"NULL".to_string()]
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
CompletionComponent::new(KeyConfig::default(), "N")
|
||||||
|
.filterd_candidates()
|
||||||
|
.collect::<Vec<&String>>(),
|
||||||
|
vec![&"NOT".to_string(), &"NULL".to_string()]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
use super::{Component, DrawableComponent, EventState};
|
||||||
|
use crate::components::command::CommandInfo;
|
||||||
|
use crate::config::KeyConfig;
|
||||||
|
use crate::event::Key;
|
||||||
|
use anyhow::Result;
|
||||||
|
use tui::{
|
||||||
|
backend::Backend,
|
||||||
|
layout::{Alignment, Rect},
|
||||||
|
widgets::{Block, Borders, Clear, Paragraph, Wrap},
|
||||||
|
Frame,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct DebugComponent {
|
||||||
|
msg: String,
|
||||||
|
visible: bool,
|
||||||
|
key_config: KeyConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DebugComponent {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn new(key_config: KeyConfig, msg: String) -> Self {
|
||||||
|
Self {
|
||||||
|
msg,
|
||||||
|
visible: false,
|
||||||
|
key_config,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DrawableComponent for DebugComponent {
|
||||||
|
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, _area: Rect, _focused: bool) -> Result<()> {
|
||||||
|
if true {
|
||||||
|
let width = 65;
|
||||||
|
let height = 10;
|
||||||
|
let error = Paragraph::new(self.msg.to_string())
|
||||||
|
.block(Block::default().title("Debug").borders(Borders::ALL))
|
||||||
|
.alignment(Alignment::Left)
|
||||||
|
.wrap(Wrap { trim: true });
|
||||||
|
let area = Rect::new(
|
||||||
|
(f.size().width.saturating_sub(width)) / 2,
|
||||||
|
(f.size().height.saturating_sub(height)) / 2,
|
||||||
|
width.min(f.size().width),
|
||||||
|
height.min(f.size().height),
|
||||||
|
);
|
||||||
|
f.render_widget(Clear, area);
|
||||||
|
f.render_widget(error, area);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Component for DebugComponent {
|
||||||
|
fn commands(&self, _out: &mut Vec<CommandInfo>) {}
|
||||||
|
|
||||||
|
fn event(&mut self, key: Key) -> Result<EventState> {
|
||||||
|
if self.visible {
|
||||||
|
if key == self.key_config.exit_popup {
|
||||||
|
self.msg = String::new();
|
||||||
|
self.hide();
|
||||||
|
return Ok(EventState::Consumed);
|
||||||
|
}
|
||||||
|
return Ok(EventState::NotConsumed);
|
||||||
|
}
|
||||||
|
Ok(EventState::NotConsumed)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue