1use ratatui::layout::Constraint;
4use scripting::nomiscript::{Value, format_value};
5
6use crate::tabs::plist::plist_text;
7use crate::tabs::select_list::{SelectList, SelectRow};
8
9#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct SshKeyRow {
12 pub id: String,
13 pub key_type: String,
14 pub fingerprint: String,
15 pub comment: String,
16 pub added: String,
18}
19
20pub type SshKeysTab = SelectList<SshKeyRow>;
22
23impl SelectRow for SshKeyRow {
24 const HEADERS: &'static [&'static str] = &["Type", "Fingerprint", "Comment", "Added"];
25 const WIDTHS: &'static [Constraint] = &[
26 Constraint::Length(12),
27 Constraint::Length(52),
28 Constraint::Fill(1),
29 Constraint::Length(10),
30 ];
31
32 fn parse(plist: &Value) -> Result<Self, String> {
33 let malformed = || format!("malformed ssh key: {}", format_value(plist));
34 let text = |name| plist_text(plist, name).ok_or_else(malformed);
35 let created = text(":created-at")?;
36 Ok(Self {
37 id: text(":id")?,
38 key_type: text(":key-type")?,
39 fingerprint: text(":fingerprint")?,
40 comment: text(":name")?,
41 added: created.get(..10).unwrap_or(&created).to_string(),
42 })
43 }
44
45 fn key(&self) -> &str {
46 &self.fingerprint
47 }
48
49 fn has_key(&self, key: &str) -> bool {
50 self.fingerprint == key || self.id == key
51 }
52
53 fn cells(&self) -> Vec<String> {
54 vec![
55 self.key_type.clone(),
56 self.fingerprint.clone(),
57 self.comment.clone(),
58 self.added.clone(),
59 ]
60 }
61}
62
63#[cfg(test)]
64mod tests;