Skip to content

Commit

Permalink
Merge pull request #27 from ajayd-san/improved_dynamic_sizing
Browse files Browse the repository at this point in the history
Improved dynamic sizing
  • Loading branch information
ajayd-san authored Jul 20, 2024
2 parents 16662a6 + df4a4c3 commit cf7566f
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 53 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
Use: "gmd",
Short: "TUI to manage docker objects",
Long: `The Definitive TUI to manage docker objects with ease.`,
Version: "1.3.1-hotfix",
Version: "1.3.2",
RunE: func(cmd *cobra.Command, args []string) error {
return tui.StartTUI(debug)
},
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if [ $? -ne 0 ]; then
fi

package=gomanagedocker
version=1.3.1
version=1.3.2
arch=$(uname -m)
os=$(uname -s)

Expand Down
16 changes: 13 additions & 3 deletions tui/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (

func populateImageInfoBox(imageinfo imageItem) string {
var res strings.Builder
addEntry(&res, "id: ", strings.TrimPrefix(imageinfo.ID, "sha256:"))
id := strings.TrimPrefix(imageinfo.ID, "sha256:")
id = trimToLength(id, moreInfoStyle.GetWidth())
addEntry(&res, "id: ", id)
addEntry(&res, "Name: ", imageinfo.getName())
sizeInGb := float64(imageinfo.getSize())
addEntry(&res, "Size: ", strconv.FormatFloat(sizeInGb, 'f', 2, 64)+"GB")
Expand All @@ -30,7 +32,9 @@ func populateVolumeInfoBox(volumeInfo VolumeItem) string {
addEntry(&res, "Name: ", volumeInfo.getName())
addEntry(&res, "Created: ", volumeInfo.CreatedAt)
addEntry(&res, "Driver: ", volumeInfo.Driver)
addEntry(&res, "Mount Point: ", volumeInfo.Mountpoint)

mntPt := trimToLength(volumeInfo.Mountpoint, moreInfoStyle.GetWidth())
addEntry(&res, "Mount Point: ", mntPt)

if size := volumeInfo.getSize(); size != -1 {
addEntry(&res, "Size: ", fmt.Sprintf("%f", size))
Expand All @@ -43,7 +47,9 @@ func populateVolumeInfoBox(volumeInfo VolumeItem) string {

func populateContainerInfoBox(containerInfo containerItem, containerSizeTracker *ContainerSizeManager, imageIdToNameMap map[string]string) string {
var res strings.Builder
addEntry(&res, "ID: ", containerInfo.ID)

id := trimToLength(containerInfo.ID, moreInfoStyle.GetWidth())
addEntry(&res, "ID: ", id)
addEntry(&res, "Name: ", containerInfo.getName())
addEntry(&res, "Image: ", imageIdToNameMap[containerInfo.ImageID])
addEntry(&res, "Created: ", time.Unix(containerInfo.Created, 0).Format(time.UnixDate))
Expand Down Expand Up @@ -111,3 +117,7 @@ func mapToString(m map[string]string) string {
}
return res.String()
}

func trimToLength(id string, availableWidth int) string {
return id[:min(availableWidth-10, len(id))]
}
95 changes: 65 additions & 30 deletions tui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/charmbracelet/bubbles/key"
)

var KeymapAvailableWidth int

type navigationKeymap struct {
Enter key.Binding
Back key.Binding
Expand All @@ -17,10 +19,9 @@ type navigationKeymap struct {
}

type imgKeymap struct {
Run key.Binding
Rename key.Binding
Scout key.Binding
// Pull key.Binding
Run key.Binding
Rename key.Binding
Scout key.Binding
Prune key.Binding
Delete key.Binding
DeleteForce key.Binding
Expand Down Expand Up @@ -65,33 +66,35 @@ var ImageKeymap = imgKeymap{
),
Scout: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "Scout"),
key.WithHelp("s", "scout"),
),
// Pull: key.NewBinding(
// key.WithKeys("o"),
// key.WithHelp("o", "Pull new Image"),
// ),
Prune: key.NewBinding(
key.WithKeys("p"),
key.WithHelp("p", "Prune images"),
key.WithHelp("p", "prune images"),
),
CopyId: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "Copy Image ID"),
key.WithHelp("c", "copy Image ID"),
),
RunAndExec: key.NewBinding(
key.WithKeys("x"),
key.WithHelp("x", "Run and exec"),
key.WithHelp("x", "run and exec"),
),
}

func (m imgKeymap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{m.Run,
m.Delete,
m.DeleteForce,
m.Prune},
allBindings := []key.Binding{
m.Run,
m.Delete,
m.DeleteForce,
m.Prune,
m.Scout,
m.CopyId,
m.RunAndExec,
}

return packKeybindings(allBindings, KeymapAvailableWidth)

}

func (m imgKeymap) ShortHelp() []key.Binding {
Expand All @@ -110,19 +113,19 @@ func (m imgKeymap) ShortHelp() []key.Binding {
var ContainerKeymap = contKeymap{
ToggleListAll: key.NewBinding(
key.WithKeys("a"),
key.WithHelp("a", "Toggle list all"),
key.WithHelp("a", "toggle list all"),
),
ToggleStartStop: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "Toggle Start/Stop"),
key.WithHelp("s", "toggle Start/Stop"),
),
TogglePause: key.NewBinding(
key.WithKeys("t"),
key.WithHelp("t", "Toggle Pause/unPause"),
key.WithHelp("t", "toggle Pause/unPause"),
),
Restart: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "Restart"),
key.WithHelp("r", "restart"),
),
Delete: key.NewBinding(
key.WithKeys("d"),
Expand All @@ -142,12 +145,24 @@ var ContainerKeymap = contKeymap{
),
CopyId: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "Copy Container ID"),
key.WithHelp("c", "copy ID"),
),
}

func (m contKeymap) FullHelp() [][]key.Binding {
return [][]key.Binding{}
allBindings := []key.Binding{
m.ToggleListAll,
m.ToggleStartStop,
m.Restart,
m.TogglePause,
m.Delete,
m.DeleteForce,
m.Prune,
m.Exec,
m.CopyId,
}

return packKeybindings(allBindings, KeymapAvailableWidth)
}

func (m contKeymap) ShortHelp() []key.Binding {
Expand Down Expand Up @@ -175,16 +190,16 @@ var VolumeKeymap = volKeymap{
),
CopyId: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "Copy Name"),
key.WithHelp("c", "copy Name"),
),
}

func (m volKeymap) FullHelp() [][]key.Binding {
return [][]key.Binding{}
return [][]key.Binding{{m.Delete}, {m.Prune}, {m.CopyId}}
}

func (m volKeymap) ShortHelp() []key.Binding {
return []key.Binding{m.Delete, m.Prune, m.CopyId}
return []key.Binding{}
}

var NavKeymap = navigationKeymap{
Expand All @@ -198,15 +213,15 @@ var NavKeymap = navigationKeymap{
),
Quit: key.NewBinding(
key.WithKeys("ctrl+c", "q"),
key.WithHelp("ctrl+c/q", "quit"),
key.WithHelp("q", "quit"),
),
NextTab: key.NewBinding(
key.WithKeys("right", "l", "tab"),
key.WithHelp("->/l/tab", "next"),
),
PrevTab: key.NewBinding(
key.WithKeys("left", "h", "shift+tab"),
key.WithHelp("<-/h/shift+tab", "prev"),
key.WithHelp("<-/h/S-tab", "prev"),
),
NextItem: key.NewBinding(
key.WithKeys("down", "j"),
Expand All @@ -227,9 +242,29 @@ var NavKeymap = navigationKeymap{
}

func (m navigationKeymap) FullHelp() [][]key.Binding {
return [][]key.Binding{}
allBindings := []key.Binding{m.NextItem, m.PrevItem, m.NextTab, m.PrevTab, m.PrevPage, m.NextPage, m.Quit}
return packKeybindings(allBindings, KeymapAvailableWidth)
}

func (m navigationKeymap) ShortHelp() []key.Binding {
return []key.Binding{m.NextItem, m.PrevItem, m.NextTab, m.PrevTab, m.PrevPage, m.NextPage, m.Enter, m.Quit}
return []key.Binding{}
}

func packKeybindings(keybindings []key.Binding, width int) [][]key.Binding {
res := make([][]key.Binding, len(keybindings))

i := 0
curWidth := width
for _, binding := range keybindings {
if curWidth < 20 {
i = 0
curWidth = width
}

res[i] = append(res[i], binding)
curWidth -= len(binding.Help().Desc) + len(binding.Help().Key) + 3
i += 1
}

return res
}
13 changes: 8 additions & 5 deletions tui/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

const (
// list always takes up 30% of the screen
listWidthRatio float32 = 0.3
const listWidthRatioWithInfoBox = 0.3
const listWidthRatioWithOutInfoBox = 0.85

var (
// list always takes up 30% of the screen by default
listWidthRatio float32 = listWidthRatioWithInfoBox
// duration of the list status message on screen, default: 2s
statusMessageDuration time.Duration = 2 * time.Second
)
Expand All @@ -30,8 +33,8 @@ func (m listModel) Init() tea.Cmd {
func (m listModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.list.SetSize(int(listWidthRatio*float32(msg.Width)), msg.Height-10)
listContainer = listContainer.Width(int(listWidthRatio * float32(msg.Width))).Height(msg.Height - 9)
m.list.SetSize(int(listWidthRatio*float32(msg.Width)), msg.Height-12)
listContainer = listContainer.Width(int(listWidthRatio * float32(msg.Width))).Height(msg.Height - 12)
case []dockerRes:
m.updateTab(msg)

Expand Down
Loading

0 comments on commit cf7566f

Please # to comment.