Skip to main content

tui/widgets/
hints.rs

1//! Completions a rows list offers to its tag name and value cells.
2
3use std::collections::HashMap;
4
5use forms::Suggest;
6
7use super::RowsWidget;
8
9/// A lookup the sub-list wants answered; a value lookup names its tag.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum Hint {
12    Names(Suggest),
13    Values(Suggest, String),
14}
15
16impl Hint {
17    /// The `form-options` source that serves the lookup.
18    #[must_use]
19    pub fn source(&self) -> String {
20        match self {
21            Self::Names(suggest) => suggest.name().to_string(),
22            Self::Values(suggest, tag) => format!("{}:{tag}", suggest.name()),
23        }
24    }
25}
26
27impl Hint {
28    /// The lookup the focused cell of `rows` needs, if it suggests anything.
29    #[must_use]
30    pub fn of_focused(rows: &RowsWidget) -> Option<Self> {
31        let suggest = rows.focused_suggest()?;
32        match suggest {
33            Suggest::TagNames(_) => Some(Self::Names(suggest)),
34            Suggest::TagValues(_) => match rows.focused_text("name") {
35                "" => None,
36                name => Some(Self::Values(suggest, name.to_string())),
37            },
38        }
39    }
40}
41
42/// The answers so far per source and the `seq` of each request; a reply of
43/// any other `seq` is stale.
44#[derive(Debug, Default)]
45pub struct Hints {
46    answers: HashMap<String, (u64, Vec<String>)>,
47}
48
49impl Hints {
50    /// Whether `hint` has not been asked yet.
51    #[must_use]
52    pub fn wants(&self, hint: &Hint) -> bool {
53        !self.answers.contains_key(&hint.source())
54    }
55
56    pub fn asked(&mut self, hint: &Hint, seq: u64) {
57        self.answers.insert(hint.source(), (seq, Vec::new()));
58    }
59
60    pub fn arrived(&mut self, hint: &Hint, seq: u64, strings: Vec<String>) {
61        if let Some(answer) = self.answers.get_mut(&hint.source())
62            && answer.0 == seq
63        {
64            answer.1 = strings;
65        }
66    }
67
68    /// The answer to `hint`, empty until it arrives.
69    #[must_use]
70    pub fn answer(&self, hint: &Hint) -> &[String] {
71        self.answers
72            .get(&hint.source())
73            .map_or(&[], |(_, strings)| strings)
74    }
75}
76
77/// The candidates that start with `typed`, ignoring case.
78pub fn matching<'a>(all: &'a [String], typed: &str) -> Vec<&'a str> {
79    let typed = typed.to_lowercase();
80    all.iter()
81        .map(String::as_str)
82        .filter(|candidate| candidate.to_lowercase().starts_with(&typed))
83        .collect()
84}
85
86/// What completing `typed` yields: the candidate after the one typed in full,
87/// else the first.
88pub fn completion<'a>(candidates: &[&'a str], typed: &str) -> Option<&'a str> {
89    match candidates.iter().position(|candidate| *candidate == typed) {
90        Some(at) => candidates.get((at + 1) % candidates.len()).copied(),
91        None => candidates.first().copied(),
92    }
93}