Skip to content

Commit

Permalink
help & keys
Browse files Browse the repository at this point in the history
  • Loading branch information
somnek committed Jan 16, 2024
1 parent 9d5da71 commit 4260071
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
data
Taskfile.yml
debug.log
.env
Expand Down
13 changes: 5 additions & 8 deletions keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ type keyMap struct {
Start key.Binding
Pause key.Binding
Unpause key.Binding

Select key.Binding
}

func (k keyMap) ShortHelp() []key.Binding {
return []key.Binding{
k.Help,
k.Quit,
k.Clear,
k.SelectAll,
k.Tab,
}
Expand All @@ -38,16 +35,16 @@ func (k keyMap) ShortHelp() []key.Binding {
func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{
k.Help,
k.Quit,
k.Clear,
k.SelectAll,
k.Tab,
k.Help,
},
{
k.Up,
k.Down,
k.Select,
k.Toggle,
k.Clear,
k.SelectAll,
},
{
k.Remove,
Expand Down Expand Up @@ -85,7 +82,7 @@ var keys = keyMap{
),
SelectAll: key.NewBinding(
key.WithKeys("A"),
key.WithHelp("shift+a", "select all"),
key.WithHelp("shift+a", "all"),
),
Tab: key.NewBinding(
key.WithKeys("tab"),
Expand Down
7 changes: 7 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"time"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
)
Expand Down Expand Up @@ -36,6 +37,7 @@ type model struct {
// TODO: merge process into Container struct
processes map[string]string // map[containerID]desiredState
keys keyMap
help help.Model
logs string
page int
width int
Expand Down Expand Up @@ -74,6 +76,10 @@ func initialModel() model {
s.Spinner = spinner.Jump
s.Style = spinnerStyle

// help
h := help.New()
h.Width = fixedWidth

// processes
processes := make(map[string]string)
return model{
Expand All @@ -84,5 +90,6 @@ func initialModel() model {
spinner: s,
page: pageContainer,
keys: keys,
help: h,
}
}
3 changes: 3 additions & 0 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ var (
itemCountStyle = lipgloss.NewStyle().
Foreground(frenchBlue).
Bold(true)

spinnerStyle = lipgloss.NewStyle().
Foreground(celesBlue).
Bold(true)

helpStyle = lipgloss.NewStyle()
)
6 changes: 1 addition & 5 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.logs = ""

case key.Matches(msg, m.keys.Help): // toggle help
if m.page != 3 {
m.page = 3
} else {
m.page = 0
}
m.help.ShowAll = !m.help.ShowAll
return m, nil

case key.Matches(msg, m.keys.Tab): // switch tab
Expand Down
53 changes: 44 additions & 9 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ func buildContainerDescFull(id string) string {
if err != nil {
log.Fatal(err)
}
desc := fmt.Sprintf("ID : %v\n", runewidth.Truncate(container.ID, fixedBodyRWidth-8, "..."))
desc := fmt.Sprintf(
"ID : %v\n",
runewidth.Truncate(container.ID, fixedBodyRWidth-8, "..."),
)
desc += fmt.Sprintf("Image : %s\n", container.Config.Image)
desc += fmt.Sprintf("Cmd : %s\n", strings.Join(container.Config.Cmd, " "))
desc += fmt.Sprintf("Created : %s\n", container.Created.Format("2006-01-02 15:04:05"))
desc += fmt.Sprintf("State : %s\n", container.State.String())
desc += fmt.Sprintf("Ports : %v\n", formatPortsMapping(container.NetworkSettings.Ports))
desc += fmt.Sprintf(
"Ports : %v\n",
formatPortsMapping(container.NetworkSettings.Ports),
)
desc += fmt.Sprintf("Mounts : %v\n", formatMounts(container.Mounts))
desc += fmt.Sprintf("Labels : %v\n", container.Config.Labels)
desc += fmt.Sprintf("Env : %v\n", container.Config.Env)
Expand Down Expand Up @@ -143,7 +149,7 @@ func buildImageView(m model) (string, string) {
func buildLogView(m model) string {
var s string
s += m.logs
logStyle.MarginLeft((fixedWidth - lipgloss.Width(s)) / 2)
logStyle.MarginLeft(((fixedWidth - 4) - lipgloss.Width(s)) / 2)
logStyle.AlignHorizontal(lipgloss.Center)
return logStyle.Render(s)
}
Expand All @@ -170,7 +176,7 @@ func buildContainerView(m model) (string, string) {
check = checkStyle.Render("✔")
}
row := fmt.Sprintf("%s %s %s %s", cursor, check, state, name)
padRowWidth(&row)
padItemWidth(&row, fixedBodyLWidth-10)
bodyL += row
}

Expand All @@ -193,7 +199,7 @@ func (m model) View() string {
}

// title
title := "🐳 Docker" + " " + m.spinner.View()
title := "🐳 Docker" + " "
titleStyle.MarginLeft((m.width / 2) - (lipgloss.Width(title) / 2))
title = titleStyle.Render(title)

Expand All @@ -204,11 +210,15 @@ func (m model) View() string {
// bottom
bottom = buildLogView(m)

// help
help := m.help.View(m.keys)
padHelpWidth(&help, m.width, fixedWidth)

// joing title + body + log + help
final += lipgloss.JoinVertical(lipgloss.Top, body, bottom)
appStyle.MarginLeft((m.width - fixedWidth) / 2)

return title + "\n" + appStyle.Render(final) + "\n"
return title + "\n" + appStyle.Render(final) + "\n" + help
}

func padBodyHeight(s *string, itemCount int) {
Expand All @@ -217,10 +227,35 @@ func padBodyHeight(s *string, itemCount int) {
}
}

func padRowWidth(s *string) {
func padHelpWidth(s *string, windowWidth, maxAppWidth int) {
var outerPad, innerPad, longest int

// get width of longer help string (fullHelp)
split := strings.Split(*s, "\n")
for _, line := range split {
if lipgloss.Width(line) > longest {
longest = lipgloss.Width(line)
}
}
sWidth := longest
logToFile(fmt.Sprintf("sWidth: %d", sWidth))

if windowWidth > 0 && longest < maxAppWidth-4 {
outerPad = (windowWidth - maxAppWidth) / 2
innerPad = ((maxAppWidth - 4) - sWidth) / 2
}

var newS string
for _, line := range split {
newS += strings.Repeat(" ", outerPad+innerPad) + line + "\n"
}
*s = newS
}

func padItemWidth(s *string, maxWidth int) {
sWidth := lipgloss.Width(*s)
if sWidth < fixedBodyLWidth-10 {
*s = *s + strings.Repeat(" ", (fixedBodyLWidth-10)-sWidth)
if sWidth < maxWidth-10 {
*s = *s + strings.Repeat(" ", maxWidth-sWidth)
}
*s += "\n"
}

0 comments on commit 4260071

Please sign in to comment.