Skip to content

Commit 6b61d82

Browse files
committed
add order to images, containers
1 parent bb246a1 commit 6b61d82

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

docker/docker.go

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"sort"
89
"strings"
910
"time"
1011

@@ -590,3 +591,29 @@ func (d *Docker) GetAllContainersSize() string {
590591

591592
return utils.FormatSize(size)
592593
}
594+
595+
func (d *Docker) GetImagesOrderBySize(desc bool) []MyImage {
596+
sortedImages := make([]MyImage, len(d.Images))
597+
copy(sortedImages, d.Images)
598+
sort.Slice(sortedImages, func(i, j int) bool {
599+
if desc {
600+
return sortedImages[i].Summary.Size > sortedImages[j].Summary.Size
601+
} else {
602+
return sortedImages[i].Summary.Size < sortedImages[j].Summary.Size
603+
}
604+
})
605+
return sortedImages
606+
}
607+
608+
func (d *Docker) GetContainersOrderBySize(desc bool) []MyContainer {
609+
sortedContainers := make([]MyContainer, len(d.Containers))
610+
copy(sortedContainers, d.Containers)
611+
sort.Slice(sortedContainers, func(i, j int) bool {
612+
if desc {
613+
return sortedContainers[i].SizeOriginal > sortedContainers[j].SizeOriginal
614+
} else {
615+
return sortedContainers[i].SizeOriginal < sortedContainers[j].SizeOriginal
616+
}
617+
})
618+
return sortedContainers
619+
}

models/container_list.go

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type ContainerList struct {
2424
title string
2525
}
2626

27+
var orderDescContainer bool
28+
2729
func NewContainerList(rows []table.Row) ContainerList {
2830
columns := []table.Column{
2931
{Title: "ID", Width: 20},
@@ -139,6 +141,11 @@ func (cl ContainerList) Update(msg tea.Msg, m *model) (table.Model, tea.Cmd) {
139141
case "ctrl+e":
140142
m.currentModel = MContainerExecOptions
141143
m.containerExecOptions = NewContainerExecOptions(m.containerList.table.SelectedRow()[1])
144+
case "ctrl+a":
145+
orderDescContainer = !orderDescContainer
146+
containers := m.dockerClient.GetContainersOrderBySize(orderDescContainer)
147+
cl.table.SetRows(GetContainerRows(containers, ""))
148+
142149
}
143150
}
144151

models/image_list.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/charmbracelet/lipgloss"
1414
)
1515

16+
var orderDescImage bool
17+
1618
type ImageList struct {
1719
table table.Model
1820
title string
@@ -89,7 +91,10 @@ func (il ImageList) Update(msg tea.Msg, m *model) (table.Model, tea.Cmd) {
8991
ov := NewImageOptions(m.imageList.table.SelectedRow()[1])
9092
m.imageOptions = ov
9193
m.currentModel = MImageOptions
92-
94+
case "ctrl+a":
95+
orderDescImage = !orderDescImage
96+
images := m.dockerClient.GetImagesOrderBySize(orderDescImage)
97+
il.table.SetRows(GetImageRows(images, ""))
9398
}
9499
}
95100

0 commit comments

Comments
 (0)