mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
default menu height dynamically
This commit is contained in:
parent
0fd67731b5
commit
c9a207ebfd
40
menu.go
40
menu.go
@ -27,22 +27,23 @@ func NewMenu(items []string) *Menu {
|
|||||||
Selectable: false,
|
Selectable: false,
|
||||||
cursorPos: 0,
|
cursorPos: 0,
|
||||||
}
|
}
|
||||||
m.Width = calcWidth(items)
|
m.Width, m.Height = calcSize(items)
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// return dynamic width based on string len in items
|
// return width and height based on menu items
|
||||||
func calcWidth(items []string) int {
|
func calcSize(items []string) (w, h int) {
|
||||||
maxWidth := 0
|
h = len(items) + (padding * 2)
|
||||||
|
|
||||||
|
w = minWidth
|
||||||
for _, s := range items {
|
for _, s := range items {
|
||||||
if len(s) > maxWidth {
|
if len(s) > w {
|
||||||
maxWidth = len(s)
|
w = len(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if maxWidth < minWidth {
|
w += (padding * 2)
|
||||||
maxWidth = minWidth
|
|
||||||
}
|
return w, h
|
||||||
return maxWidth + (padding * 2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Menu) Buffer() ui.Buffer {
|
func (m *Menu) Buffer() ui.Buffer {
|
||||||
@ -51,7 +52,7 @@ func (m *Menu) Buffer() ui.Buffer {
|
|||||||
buf := m.Block.Buffer()
|
buf := m.Block.Buffer()
|
||||||
|
|
||||||
for n, item := range m.Items {
|
for n, item := range m.Items {
|
||||||
x := 2 // initial offset
|
x := padding
|
||||||
for _, ch := range item {
|
for _, ch := range item {
|
||||||
// invert bg/fg colors on currently selected row
|
// invert bg/fg colors on currently selected row
|
||||||
if m.Selectable && n == m.cursorPos {
|
if m.Selectable && n == m.cursorPos {
|
||||||
@ -59,7 +60,7 @@ func (m *Menu) Buffer() ui.Buffer {
|
|||||||
} else {
|
} else {
|
||||||
cell = ui.Cell{Ch: ch, Fg: m.TextFgColor, Bg: m.TextBgColor}
|
cell = ui.Cell{Ch: ch, Fg: m.TextFgColor, Bg: m.TextBgColor}
|
||||||
}
|
}
|
||||||
buf.Set(x, n+2, cell)
|
buf.Set(x, n+padding, cell)
|
||||||
x++
|
x++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,14 +68,14 @@ func (m *Menu) Buffer() ui.Buffer {
|
|||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Menu) Up() {
|
func (m *Menu) Up(ui.Event) {
|
||||||
if m.cursorPos > 0 {
|
if m.cursorPos > 0 {
|
||||||
m.cursorPos--
|
m.cursorPos--
|
||||||
ui.Render(m)
|
ui.Render(m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Menu) Down() {
|
func (m *Menu) Down(ui.Event) {
|
||||||
if m.cursorPos < (len(m.Items) - 1) {
|
if m.cursorPos < (len(m.Items) - 1) {
|
||||||
m.cursorPos++
|
m.cursorPos++
|
||||||
ui.Render(m)
|
ui.Render(m)
|
||||||
@ -86,7 +87,6 @@ func HelpMenu(g *Grid) {
|
|||||||
"[h] - open this help dialog",
|
"[h] - open this help dialog",
|
||||||
"[q] - exit ctop",
|
"[q] - exit ctop",
|
||||||
})
|
})
|
||||||
m.Height = 10
|
|
||||||
m.TextFgColor = ui.ColorWhite
|
m.TextFgColor = ui.ColorWhite
|
||||||
m.BorderLabel = "Help"
|
m.BorderLabel = "Help"
|
||||||
m.BorderFg = ui.ColorCyan
|
m.BorderFg = ui.ColorCyan
|
||||||
@ -99,17 +99,15 @@ func HelpMenu(g *Grid) {
|
|||||||
|
|
||||||
func SortMenu(g *Grid) {
|
func SortMenu(g *Grid) {
|
||||||
m := NewMenu(SortFields)
|
m := NewMenu(SortFields)
|
||||||
m.Height = 10
|
|
||||||
m.Selectable = true
|
m.Selectable = true
|
||||||
m.TextFgColor = ui.ColorWhite
|
m.TextFgColor = ui.ColorWhite
|
||||||
m.BorderLabel = "Sort Field"
|
m.BorderLabel = "Sort Field"
|
||||||
m.BorderFg = ui.ColorCyan
|
m.BorderFg = ui.ColorCyan
|
||||||
ui.Render(m)
|
ui.Render(m)
|
||||||
ui.Handle("/sys/kbd/<up>", func(ui.Event) {
|
ui.Handle("/sys/kbd/<up>", m.Up)
|
||||||
m.Up()
|
ui.Handle("/sys/kbd/<down>", m.Down)
|
||||||
})
|
ui.Handle("/sys/kbd/q", func(ui.Event) {
|
||||||
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
|
ui.StopLoop()
|
||||||
m.Down()
|
|
||||||
})
|
})
|
||||||
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
||||||
g.containerMap.config.sortField = m.Items[m.cursorPos]
|
g.containerMap.config.sortField = m.Items[m.cursorPos]
|
||||||
|
Loading…
Reference in New Issue
Block a user