1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! KeyHint component for keyboard shortcut hints
//!
//! Displays keyboard shortcuts in a consistent format.
//!
//! # Example
//!
//! ```rust,ignore
//! use rnk::prelude::*;
//! use rnk::components::KeyHint;
//!
//! fn footer() -> Element {
//! Box::new()
//! .flex_direction(FlexDirection::Row)
//! .gap(2.0)
//! .children(vec![
//! KeyHint::new("q", "Quit").into_element(),
//! KeyHint::new("↑↓", "Navigate").into_element(),
//! KeyHint::new("Enter", "Select").into_element(),
//! ])
//! .into_element()
//! }
//! ```
use crate::components::{Box as RnkBox, Text};
use crate::core::{Color, Element, FlexDirection};
/// A key hint component for displaying keyboard shortcuts
#[derive(Debug, Clone)]
pub struct KeyHint {
key: String,
description: String,
key_color: Color,
desc_color: Color,
}
impl KeyHint {
/// Create a new key hint
pub fn new(key: impl Into<String>, description: impl Into<String>) -> Self {
Self {
key: key.into(),
description: description.into(),
key_color: Color::Yellow,
desc_color: Color::BrightBlack,
}
}
/// Set the key color
pub fn key_color(mut self, color: Color) -> Self {
self.key_color = color;
self
}
/// Set the description color
pub fn desc_color(mut self, color: Color) -> Self {
self.desc_color = color;
self
}
/// Convert to Element
pub fn into_element(self) -> Element {
RnkBox::new()
.flex_direction(FlexDirection::Row)
.gap(0.5)
.children(vec![
Text::new(&self.key)
.color(self.key_color)
.bold()
.into_element(),
Text::new(&self.description)
.color(self.desc_color)
.into_element(),
])
.into_element()
}
}
impl Default for KeyHint {
fn default() -> Self {
Self::new("", "")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_key_hint_creation() {
let kh = KeyHint::new("q", "Quit");
assert_eq!(kh.key, "q");
assert_eq!(kh.description, "Quit");
}
#[test]
fn test_key_hint_into_element() {
let _ = KeyHint::new("Enter", "Select").into_element();
}
}