Skip to main content

rusticity_term/cw/
tag.rs

1use crate::common::{translate_column, ColumnId};
2use crate::ui::table::Column as TableColumn;
3
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub enum TagColumn {
6    Key,
7    Value,
8}
9
10impl TagColumn {
11    const ID_KEY: &'static str = "column.cw.tag.key";
12    const ID_VALUE: &'static str = "column.cw.tag.value";
13
14    pub const fn id(&self) -> ColumnId {
15        match self {
16            TagColumn::Key => Self::ID_KEY,
17            TagColumn::Value => Self::ID_VALUE,
18        }
19    }
20
21    pub const fn default_name(&self) -> &'static str {
22        match self {
23            TagColumn::Key => "Key",
24            TagColumn::Value => "Value",
25        }
26    }
27
28    pub fn name(&self) -> String {
29        translate_column(self.id(), self.default_name())
30    }
31
32    pub fn from_id(id: &str) -> Option<Self> {
33        match id {
34            Self::ID_KEY => Some(TagColumn::Key),
35            Self::ID_VALUE => Some(TagColumn::Value),
36            _ => None,
37        }
38    }
39
40    pub const fn all() -> [TagColumn; 2] {
41        [TagColumn::Key, TagColumn::Value]
42    }
43
44    pub fn ids() -> Vec<ColumnId> {
45        Self::all().iter().map(|c| c.id()).collect()
46    }
47}
48
49impl TableColumn<(String, String)> for TagColumn {
50    fn name(&self) -> &str {
51        Box::leak(translate_column(self.id(), self.default_name()).into_boxed_str())
52    }
53
54    fn width(&self) -> u16 {
55        let translated = translate_column(self.id(), self.default_name());
56        translated.len().max(20) as u16
57    }
58
59    fn render(&self, item: &(String, String)) -> (String, ratatui::style::Style) {
60        let text = match self {
61            TagColumn::Key => item.0.clone(),
62            TagColumn::Value => item.1.clone(),
63        };
64        (text, ratatui::style::Style::default())
65    }
66}