[go: nahoru, domu]

Skip to content

Commit

Permalink
feat: refactor ui to table
Browse files Browse the repository at this point in the history
  • Loading branch information
dj95 committed Dec 30, 2023
1 parent bc2bd50 commit ba6560c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 32 deletions.
33 changes: 33 additions & 0 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,44 @@ use zellij_tile::prelude::*;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Container {
pub id: String,
pub name: String,
pub image: String,
pub running: bool,
pub status: String,
}

impl ToString for Container {
fn to_string(&self) -> String {
format!(
"{} {} {} ({})",
self.id, self.name, self.image, self.status
)
}
}

impl Container {
pub fn to_table_row(&self) -> Vec<Text> {
vec![
Text::new(self.id.clone()),
Text::new(" "),
Text::new(self.name.clone()),
Text::new(" "),
Text::new(self.image.clone()),
Text::new(" "),
Text::new(self.status.clone()),
]
}
}

impl Default for Container {
fn default() -> Self {
Self {
id: "".to_owned(),
name: "".to_owned(),
image: "".to_owned(),
running: false,
status: "".to_owned(),
}
}
}
Expand All @@ -32,8 +61,12 @@ pub fn parse_docker_containers(output: &str) -> Vec<Container> {
for line in output.lines() {
let container: HashMap<String, String> = serde_json::from_str(line).unwrap();
containers.push(Container {
id: container["ID"].to_owned(),
name: container["Names"].to_owned(),
image: container["Image"].to_owned(),
running: container["State"] == "running",
status: container["Status"].to_owned(),
..Default::default()
});
}

Expand Down
67 changes: 35 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,35 +250,39 @@ impl ZellijPlugin for State {

eprintln!("Selected container: {:?}", self.selected_container);

let running_items: Vec<NestedListItem> = self
.filtered_containers
.iter()
.filter_map(|c| {
if !c.running {
return None;
}
let mut running_items = Table::new();
let mut running_items_len = 0;
for container in &self.filtered_containers {
if !container.running {
continue;
}

if *c.name == selected_container {
return Some(NestedListItem::new(&c.name).selected());
}
Some(NestedListItem::new(&c.name))
})
.collect();
let mut row = container.to_table_row();

let stopped_items: Vec<NestedListItem> = self
.filtered_containers
.iter()
.filter_map(|c| {
if c.running {
return None;
}
if container.name == selected_container {
row = row.iter().map(|t| t.clone().selected()).collect();
}

if *c.name == selected_container {
return Some(NestedListItem::new(&c.name).selected());
}
Some(NestedListItem::new(&c.name))
})
.collect();
running_items = running_items.add_styled_row(row);
running_items_len += 1;
}

let mut stopped_items = Table::new();
let mut stopped_items_len = 0;
for container in &self.filtered_containers {
if container.running {
continue;
}

let mut row = container.to_table_row();

if container.name == selected_container {
row = row.iter().map(|t| t.clone().selected()).collect();
}

stopped_items = stopped_items.add_styled_row(row);
stopped_items_len += 1;
}

print_text_with_coordinates(
Text::new(format!("Search > {}", self.search_query)),
Expand All @@ -288,17 +292,16 @@ impl ZellijPlugin for State {
None,
);

let len = running_items.len();
print_text_with_coordinates(Text::new(format!("Containers ({})", len)), 0, 2, None, None);
print_nested_list_with_coordinates(running_items.clone(), 1, 3, None, None);
print_text_with_coordinates(Text::new(format!("Containers ({})", running_items_len)), 0, 2, None, None);
print_table_with_coordinates(running_items, 1, 3, None, None);
print_text_with_coordinates(
Text::new(format!("Stopped Containers ({})", stopped_items.len())),
Text::new(format!("Stopped Containers ({})", stopped_items_len)),
0,
4 + len,
4 + running_items_len,
None,
None,
);
print_nested_list_with_coordinates(stopped_items, 1, 5 + len, None, None);
print_table_with_coordinates(stopped_items, 1, 5 + running_items_len, None, None);
print_help(rows);
}
}
Expand Down

0 comments on commit ba6560c

Please sign in to comment.