Skip to content

Commit

Permalink
feat(images): toggle to show dangling images
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpsoane committed Jun 29, 2024
1 parent bb3c41e commit 991de4c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
34 changes: 26 additions & 8 deletions src/docker/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,39 @@ impl DockerImage {
.format("%Y-%m-%d %H:%M:%S");
let b = Byte::from_u64(bollard_image.size as u64).get_appropriate_unit(UnitType::Binary);

for repo_tag in bollard_image.repo_tags {
let split_tag = repo_tag.split(':').collect::<Vec<&str>>();
if !bollard_image.repo_tags.is_empty() {
for repo_tag in bollard_image.repo_tags {
let split_tag = repo_tag.split(':').collect::<Vec<&str>>();

response.push(Self {
id: bollard_image.id.clone(),
name: split_tag[0].to_string(),
tag: split_tag[1].to_string(),
created: datetime.to_string(),
size: format!("{b:.2}"),
})
}
} else {
response.push(Self {
id: bollard_image.id.clone(),
name: split_tag[0].to_string(),
tag: split_tag[1].to_string(),
name: "<none>".into(),
tag: "<none>".into(),
created: datetime.to_string(),
size: format!("{b:.2}"),
})
}
response
}

pub async fn list(docker: &bollard::Docker) -> Result<Vec<Self>> {
pub async fn list(docker: &bollard::Docker, dangling: bool) -> Result<Vec<Self>> {
let mut filters: HashMap<String, Vec<String>> = HashMap::new();
filters.insert("dangling".into(), vec!["false".into()]);
if !dangling {
filters.insert("dangling".into(), vec!["false".into()]);
}

let mut images = docker
.list_images(Some(ListImagesOptions::<String> {
all: false,
all: true,
digests: false,
filters,
}))
Expand Down Expand Up @@ -76,6 +88,12 @@ impl DockerImage {
}

pub fn get_full_name(&self) -> String {
format!("{}:{}", self.name, self.tag)
let image = format!("{}:{}", self.name, self.tag);

if image == "<none>:<none>" {
self.id.clone()
} else {
image
}
}
}
13 changes: 9 additions & 4 deletions src/pages/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const DOWN_KEY: Key = Key::Down;
const J_KEY: Key = Key::Char('j');
const K_KEY: Key = Key::Char('k');
const CTRL_D_KEY: Key = Key::Ctrl('d');
const D_KEY: Key = Key::Char('d');
const G_KEY: Key = Key::Char('g');
const SHIFT_G_KEY: Key = Key::Char('G');

Expand All @@ -52,6 +53,7 @@ pub struct Images {
images: Vec<DockerImage>,
list_state: TableState,
modal: Option<BooleanModal<ModalTypes>>,
show_dangling: bool,
}

#[async_trait::async_trait]
Expand All @@ -77,6 +79,10 @@ impl Page for Images {
self.increment_list();
MessageResponse::Consumed
}
D_KEY => {
self.show_dangling = !self.show_dangling;
MessageResponse::Consumed
}
G_KEY => {
self.list_state.select(Some(0));
MessageResponse::Consumed
Expand Down Expand Up @@ -124,12 +130,10 @@ impl Page for Images {
impl Images {
pub fn new(docker: Docker, config: Box<Config>) -> Self {
let page_help = PageHelpBuilder::new(NAME.into(), config.clone())
// .add_input(format!("{}", A_KEY), "attach".into())
.add_input(format!("{CTRL_D_KEY}"), "delete".into())
// .add_input(format!("{R_KEY}"), "run".into())
// .add_input(format!("{S_KEY}"), "stop".into())
.add_input(format!("{G_KEY}"), "top".into())
.add_input(format!("{SHIFT_G_KEY}"), "bottom".into())
.add_input(format!("{D_KEY}"), "dangling".into())
.build();

Self {
Expand All @@ -140,14 +144,15 @@ impl Images {
images: vec![],
list_state: TableState::default(),
modal: None,
show_dangling: false,
}
}

async fn refresh(&mut self) -> Result<(), color_eyre::eyre::Error> {
let mut filters: HashMap<String, Vec<String>> = HashMap::new();
filters.insert("dangling".into(), vec!["false".into()]);

self.images = DockerImage::list(&self.docker)
self.images = DockerImage::list(&self.docker, self.show_dangling)
.await
.context("unable to retrieve list of images")?;
Ok(())
Expand Down

0 comments on commit 991de4c

Please sign in to comment.