kdash 2.0.2

A fast and simple dashboard for Kubernetes
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// from https://github.com/Rigellute/spotify-tui
use std::fmt;
use std::str::FromStr;

use crossterm::event;

/// Represents an key.
#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)]
pub enum Key {
  /// Both Enter (or Return) and numpad Enter
  Enter,
  Tab,
  /// Shift+Tab (back-tab)
  BackTab,
  Backspace,
  Esc,
  /// Left arrow
  Left,
  /// Right arrow
  Right,
  /// Up arrow
  Up,
  /// Down arrow
  Down,
  /// Insert key
  Ins,
  /// Delete key
  Delete,
  /// Home key
  Home,
  /// End key
  End,
  /// Page Up key
  PageUp,
  /// Page Down key
  PageDown,
  /// F0 key
  F0,
  /// F1 key
  F1,
  /// F2 key
  F2,
  /// F3 key
  F3,
  /// F4 key
  F4,
  /// F5 key
  F5,
  /// F6 key
  F6,
  /// F7 key
  F7,
  /// F8 key
  F8,
  /// F9 key
  F9,
  /// F10 key
  F10,
  /// F11 key
  F11,
  /// F12 key
  F12,
  Char(char),
  Ctrl(char),
  Alt(char),
  Shift(char),
  Unknown,
}

impl Key {
  /// Returns the function key corresponding to the given number
  ///
  /// 1 -> F1, etc...
  ///
  /// # Panics
  ///
  /// If `n == 0 || n > 12`
  pub fn from_f(n: u8) -> Key {
    match n {
      0 => Key::F0,
      1 => Key::F1,
      2 => Key::F2,
      3 => Key::F3,
      4 => Key::F4,
      5 => Key::F5,
      6 => Key::F6,
      7 => Key::F7,
      8 => Key::F8,
      9 => Key::F9,
      10 => Key::F10,
      11 => Key::F11,
      12 => Key::F12,
      _ => panic!("unknown function key: F{}", n),
    }
  }

  /// Compact glyph form for `key:label` hints — no angle brackets.
  /// Uses universal Unicode key glyphs (`↹`, `⇧↹`, `⏎`, `←→↑↓`) so the
  /// rendered hints match across platforms (fixtures stay stable).
  pub fn symbol(&self) -> String {
    match self {
      Key::Char(' ') => "Space".into(),
      Key::Char(c) => c.to_string(),
      Key::Ctrl(c) => format!("Ctrl+{}", c),
      Key::Alt(c) => format!("Alt+{}", c),
      Key::Shift(' ') => "Shift+Space".into(),
      Key::Shift(c) => c.to_ascii_uppercase().to_string(),
      Key::Enter => "".into(),
      Key::Tab => "".into(),
      Key::BackTab => "⇧↹".into(),
      Key::Esc => "Esc".into(),
      Key::Backspace => "Backspace".into(),
      Key::Left => "".into(),
      Key::Right => "".into(),
      Key::Up => "".into(),
      Key::Down => "".into(),
      Key::Ins => "Ins".into(),
      Key::Delete => "Del".into(),
      Key::Home => "Home".into(),
      Key::End => "End".into(),
      Key::PageUp => "PgUp".into(),
      Key::PageDown => "PgDn".into(),
      Key::Unknown => "?".into(),
      // F0–F12 — Debug renders as `F3`, etc.
      other => format!("{:?}", other),
    }
  }
}

impl fmt::Display for Key {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match *self {
      Key::Alt(' ') => write!(f, "<Alt+Space>"),
      Key::Ctrl(' ') => write!(f, "<Ctrl+Space>"),
      Key::Shift(' ') => write!(f, "<Shift+Space>"),
      Key::Char(' ') => write!(f, "<Space>"),
      Key::BackTab => write!(f, "<Shift+Tab>"),
      Key::Alt(c) => write!(f, "<Alt+{}>", c),
      Key::Ctrl(c) => write!(f, "<Ctrl+{}>", c),
      Key::Shift(c) if c.is_ascii_alphabetic() => write!(f, "<{}>", c.to_ascii_uppercase()),
      Key::Shift(c) => write!(f, "<Shift+{}>", c),
      Key::Char(c) => write!(f, "<{}>", c),
      Key::Left => write!(f, "<←>"),
      Key::Right => write!(f, "<→>"),
      Key::Up => write!(f, "<↑>"),
      Key::Down => write!(f, "<↓>"),
      _ => write!(f, "<{:?}>", self),
    }
  }
}

impl FromStr for Key {
  type Err = String;

  fn from_str(input: &str) -> Result<Self, Self::Err> {
    let normalized = input.trim().trim_matches(['<', '>']);
    if normalized.is_empty() {
      return Err("key cannot be empty".into());
    }

    if normalized.chars().count() == 1 {
      let c = normalized
        .chars()
        .next()
        .expect("single-character string must have one char");
      return if c.is_ascii_uppercase() {
        Ok(Key::Shift(c.to_ascii_lowercase()))
      } else {
        Ok(Key::Char(c))
      };
    }

    let lower = normalized.to_lowercase();
    let key = match lower.as_str() {
      "enter" | "return" => Key::Enter,
      "tab" => Key::Tab,
      "backtab" | "back-tab" | "shift+tab" => Key::BackTab,
      "backspace" => Key::Backspace,
      "esc" | "escape" => Key::Esc,
      "left" | "leftarrow" | "left-arrow" => Key::Left,
      "right" | "rightarrow" | "right-arrow" => Key::Right,
      "up" | "uparrow" | "up-arrow" => Key::Up,
      "down" | "downarrow" | "down-arrow" => Key::Down,
      "insert" | "ins" => Key::Ins,
      "delete" | "del" => Key::Delete,
      "home" => Key::Home,
      "end" => Key::End,
      "pageup" | "page-up" => Key::PageUp,
      "pagedown" | "page-down" => Key::PageDown,
      "space" => Key::Char(' '),
      _ => {
        if let Some(rest) = lower.strip_prefix("ctrl+") {
          return parse_modified_char(rest, Key::Ctrl);
        }
        if let Some(rest) = lower.strip_prefix("alt+") {
          return parse_modified_char(rest, Key::Alt);
        }
        if let Some(rest) = lower.strip_prefix("shift+") {
          return parse_modified_char(rest, Key::Shift);
        }
        if let Some(rest) = lower.strip_prefix('f') {
          let value = rest
            .parse::<u8>()
            .map_err(|_| format!("unsupported key '{}'", input))?;
          if value <= 12 {
            return Ok(Key::from_f(value));
          }
        }
        return Err(format!("unsupported key '{}'", input));
      }
    };

    Ok(key)
  }
}

fn parse_modified_char(input: &str, wrap: fn(char) -> Key) -> Result<Key, String> {
  let value = if input == "space" { " " } else { input };
  if value.chars().count() != 1 {
    return Err(format!(
      "modified key '{}' must target a single character",
      input
    ));
  }

  Ok(wrap(
    value
      .chars()
      .next()
      .expect("single-character string must have one char"),
  ))
}

impl From<event::KeyEvent> for Key {
  fn from(key_event: event::KeyEvent) -> Self {
    match key_event {
      event::KeyEvent {
        code: event::KeyCode::Esc,
        ..
      } => Key::Esc,
      event::KeyEvent {
        code: event::KeyCode::Backspace,
        ..
      } => Key::Backspace,
      event::KeyEvent {
        code: event::KeyCode::Left,
        ..
      } => Key::Left,
      event::KeyEvent {
        code: event::KeyCode::Right,
        ..
      } => Key::Right,
      event::KeyEvent {
        code: event::KeyCode::Up,
        ..
      } => Key::Up,
      event::KeyEvent {
        code: event::KeyCode::Down,
        ..
      } => Key::Down,
      event::KeyEvent {
        code: event::KeyCode::Home,
        ..
      } => Key::Home,
      event::KeyEvent {
        code: event::KeyCode::End,
        ..
      } => Key::End,
      event::KeyEvent {
        code: event::KeyCode::PageUp,
        ..
      } => Key::PageUp,
      event::KeyEvent {
        code: event::KeyCode::PageDown,
        ..
      } => Key::PageDown,
      event::KeyEvent {
        code: event::KeyCode::Delete,
        ..
      } => Key::Delete,
      event::KeyEvent {
        code: event::KeyCode::Insert,
        ..
      } => Key::Ins,
      event::KeyEvent {
        code: event::KeyCode::F(n),
        ..
      } => Key::from_f(n),
      event::KeyEvent {
        code: event::KeyCode::Enter,
        ..
      } => Key::Enter,
      event::KeyEvent {
        code: event::KeyCode::Tab,
        ..
      } => Key::Tab,
      event::KeyEvent {
        code: event::KeyCode::BackTab,
        ..
      } => Key::BackTab,
      event::KeyEvent {
        code: event::KeyCode::Char(c),
        modifiers,
        ..
      } => {
        let normalized = if c.is_ascii_uppercase() {
          c.to_ascii_lowercase()
        } else {
          c
        };

        if modifiers.contains(event::KeyModifiers::CONTROL) {
          Key::Ctrl(normalized)
        } else if modifiers.contains(event::KeyModifiers::ALT) {
          Key::Alt(normalized)
        } else if modifiers.contains(event::KeyModifiers::SHIFT) {
          Key::Shift(normalized)
        } else {
          Key::Char(c)
        }
      }

      _ => Key::Unknown,
    }
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_key_fmt() {
    assert_eq!(format!("{}", Key::Left), "<←>");
    assert_eq!(format!("{}", Key::Right), "<→>");
    assert_eq!(format!("{}", Key::Up), "<↑>");
    assert_eq!(format!("{}", Key::Down), "<↓>");
    assert_eq!(format!("{}", Key::Alt(' ')), "<Alt+Space>");
    assert_eq!(format!("{}", Key::Shift(' ')), "<Shift+Space>");
    assert_eq!(format!("{}", Key::Alt('c')), "<Alt+c>");
    assert_eq!(format!("{}", Key::Shift('d')), "<D>");
    assert_eq!(format!("{}", Key::Char('c')), "<c>");
    assert_eq!(format!("{}", Key::Enter), "<Enter>");
    assert_eq!(format!("{}", Key::F10), "<F10>");
  }
  #[test]
  fn test_key_from_event() {
    assert_eq!(
      Key::from(event::KeyEvent::from(event::KeyCode::Esc)),
      Key::Esc
    );
    assert_eq!(
      Key::from(event::KeyEvent::from(event::KeyCode::F(2))),
      Key::F2
    );
    assert_eq!(
      Key::from(event::KeyEvent::from(event::KeyCode::Char('J'))),
      Key::Char('J')
    );
    assert_eq!(
      Key::from(event::KeyEvent {
        code: event::KeyCode::Char('D'),
        modifiers: event::KeyModifiers::SHIFT,
        kind: event::KeyEventKind::Press,
        state: event::KeyEventState::NONE,
      }),
      Key::Shift('d')
    );
    assert_eq!(
      Key::from(event::KeyEvent {
        code: event::KeyCode::Char('c'),
        modifiers: event::KeyModifiers::ALT,
        kind: event::KeyEventKind::Press,
        state: event::KeyEventState::NONE,
      }),
      Key::Alt('c')
    );
    assert_eq!(
      Key::from(event::KeyEvent {
        code: event::KeyCode::Char('c'),
        modifiers: event::KeyModifiers::CONTROL,
        kind: event::KeyEventKind::Press,
        state: event::KeyEventState::NONE
      }),
      Key::Ctrl('c')
    );
  }

  #[test]
  fn test_key_from_str() {
    assert_eq!("q".parse::<Key>(), Ok(Key::Char('q')));
    assert_eq!("ctrl+q".parse::<Key>(), Ok(Key::Ctrl('q')));
    assert_eq!("alt+x".parse::<Key>(), Ok(Key::Alt('x')));
    assert_eq!("D".parse::<Key>(), Ok(Key::Shift('d')));
    assert_eq!("shift+d".parse::<Key>(), Ok(Key::Shift('d')));
    assert_eq!("space".parse::<Key>(), Ok(Key::Char(' ')));
    assert_eq!("page-down".parse::<Key>(), Ok(Key::PageDown));
    assert_eq!("F10".parse::<Key>(), Ok(Key::F10));
    assert_eq!("tab".parse::<Key>(), Ok(Key::Tab));
    assert_eq!("shift+tab".parse::<Key>(), Ok(Key::BackTab));
    assert_eq!("backtab".parse::<Key>(), Ok(Key::BackTab));
  }

  #[test]
  fn test_uppercase_and_shift_string_parse_equally() {
    assert_eq!("D".parse::<Key>(), "shift+d".parse::<Key>());
  }
}