mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
init column config menu
This commit is contained in:
parent
6e60fc905e
commit
22a5607012
@ -69,6 +69,13 @@ func EnabledColumns() (a []string) {
|
|||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ColumnToggle toggles the enabled status of a given column name
|
||||||
|
func ColumnToggle(name string) {
|
||||||
|
col := GlobalColumns[colIndex(name)]
|
||||||
|
col.Enabled = !col.Enabled
|
||||||
|
log.Noticef("config change [column-%s]: %t -> %t", col.Name, !col.Enabled, col.Enabled)
|
||||||
|
}
|
||||||
|
|
||||||
// ColumnLeft moves the column with given name up one position, if possible
|
// ColumnLeft moves the column with given name up one position, if possible
|
||||||
func ColumnLeft(name string) {
|
func ColumnLeft(name string) {
|
||||||
idx := colIndex(name)
|
idx := colIndex(name)
|
||||||
|
@ -72,8 +72,7 @@ func Toggle(k string) {
|
|||||||
lock.Lock()
|
lock.Lock()
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
|
||||||
newVal := !sw.Val
|
sw.Val = !sw.Val
|
||||||
log.Noticef("config change [%s]: %t -> %t", k, sw.Val, newVal)
|
log.Noticef("config change [%s]: %t -> %t", k, !sw.Val, sw.Val)
|
||||||
sw.Val = newVal
|
|
||||||
//log.Errorf("ignoring toggle for non-existant switch: %s", k)
|
//log.Errorf("ignoring toggle for non-existant switch: %s", k)
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,12 @@ func New(id string, collector collector.Collector, manager manager.Manager) *Con
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Container) RecreateWidgets() {
|
||||||
|
c.SetUpdater(cwidgets.NullWidgetUpdater{})
|
||||||
|
c.Widgets = compact.NewCompactRow()
|
||||||
|
c.SetUpdater(c.Widgets)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Container) SetUpdater(u cwidgets.WidgetUpdater) {
|
func (c *Container) SetUpdater(u cwidgets.WidgetUpdater) {
|
||||||
c.updater = u
|
c.updater = u
|
||||||
c.updater.SetMeta(c.Meta)
|
c.updater.SetMeta(c.Meta)
|
||||||
|
@ -17,7 +17,7 @@ type CompactGrid struct {
|
|||||||
|
|
||||||
func NewCompactGrid() *CompactGrid {
|
func NewCompactGrid() *CompactGrid {
|
||||||
cg := &CompactGrid{header: NewCompactHeader()}
|
cg := &CompactGrid{header: NewCompactHeader()}
|
||||||
cg.RebuildHeader()
|
cg.rebuildHeader()
|
||||||
return cg
|
return cg
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,6 +30,7 @@ func (cg *CompactGrid) Align() {
|
|||||||
|
|
||||||
// update row ypos, width recursively
|
// update row ypos, width recursively
|
||||||
colWidths := cg.calcWidths()
|
colWidths := cg.calcWidths()
|
||||||
|
cg.header.SetWidths(cg.Width, colWidths)
|
||||||
for _, r := range cg.pageRows() {
|
for _, r := range cg.pageRows() {
|
||||||
r.SetY(y)
|
r.SetY(y)
|
||||||
y += r.GetHeight()
|
y += r.GetHeight()
|
||||||
@ -37,15 +38,11 @@ func (cg *CompactGrid) Align() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cg *CompactGrid) RebuildHeader() {
|
func (cg *CompactGrid) Clear() {
|
||||||
cg.cols = newRowWidgets()
|
cg.Rows = []RowBufferer{}
|
||||||
cg.header.clearFieldPars()
|
cg.rebuildHeader()
|
||||||
for _, col := range cg.cols {
|
|
||||||
cg.header.addFieldPar(col.Header())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cg *CompactGrid) Clear() { cg.Rows = []RowBufferer{} }
|
|
||||||
func (cg *CompactGrid) GetHeight() int { return len(cg.Rows) + cg.header.Height }
|
func (cg *CompactGrid) GetHeight() int { return len(cg.Rows) + cg.header.Height }
|
||||||
func (cg *CompactGrid) SetX(x int) { cg.X = x }
|
func (cg *CompactGrid) SetX(x int) { cg.X = x }
|
||||||
func (cg *CompactGrid) SetY(y int) { cg.Y = y }
|
func (cg *CompactGrid) SetY(y int) { cg.Y = y }
|
||||||
@ -93,3 +90,11 @@ func (cg *CompactGrid) Buffer() ui.Buffer {
|
|||||||
func (cg *CompactGrid) AddRows(rows ...RowBufferer) {
|
func (cg *CompactGrid) AddRows(rows ...RowBufferer) {
|
||||||
cg.Rows = append(cg.Rows, rows...)
|
cg.Rows = append(cg.Rows, rows...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cg *CompactGrid) rebuildHeader() {
|
||||||
|
cg.cols = newRowWidgets()
|
||||||
|
cg.header.clearFieldPars()
|
||||||
|
for _, col := range cg.cols {
|
||||||
|
cg.header.addFieldPar(col.Header())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -14,7 +14,10 @@ type CompactHeader struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewCompactHeader() *CompactHeader {
|
func NewCompactHeader() *CompactHeader {
|
||||||
return &CompactHeader{Height: 2}
|
return &CompactHeader{
|
||||||
|
X: rowPadding,
|
||||||
|
Height: 2,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (row *CompactHeader) GetHeight() int {
|
func (row *CompactHeader) GetHeight() int {
|
||||||
|
@ -11,3 +11,11 @@ type WidgetUpdater interface {
|
|||||||
SetMeta(models.Meta)
|
SetMeta(models.Meta)
|
||||||
SetMetrics(models.Metrics)
|
SetMetrics(models.Metrics)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NullWidgetUpdater struct{}
|
||||||
|
|
||||||
|
// NullWidgetUpdater implements WidgetUpdater
|
||||||
|
func (wu NullWidgetUpdater) SetMeta(models.Meta) {}
|
||||||
|
|
||||||
|
// NullWidgetUpdater implements WidgetUpdater
|
||||||
|
func (wu NullWidgetUpdater) SetMetrics(models.Metrics) {}
|
||||||
|
4
grid.go
4
grid.go
@ -191,6 +191,10 @@ func Display() bool {
|
|||||||
menu = SortMenu
|
menu = SortMenu
|
||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
})
|
})
|
||||||
|
ui.Handle("/sys/kbd/c", func(ui.Event) {
|
||||||
|
menu = ColumnsMenu
|
||||||
|
ui.StopLoop()
|
||||||
|
})
|
||||||
ui.Handle("/sys/kbd/S", func(ui.Event) {
|
ui.Handle("/sys/kbd/S", func(ui.Event) {
|
||||||
path, err := config.Write()
|
path, err := config.Write()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
67
menus.go
67
menus.go
@ -104,7 +104,68 @@ func SortMenu() MenuFn {
|
|||||||
HandleKeys("exit", ui.StopLoop)
|
HandleKeys("exit", ui.StopLoop)
|
||||||
|
|
||||||
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
||||||
config.Update("sortField", m.SelectedItem().Val)
|
config.Update("sortField", m.SelectedValue())
|
||||||
|
ui.StopLoop()
|
||||||
|
})
|
||||||
|
|
||||||
|
ui.Render(m)
|
||||||
|
ui.Loop()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ColumnsMenu() MenuFn {
|
||||||
|
ui.Clear()
|
||||||
|
ui.DefaultEvtStream.ResetHandlers()
|
||||||
|
defer ui.DefaultEvtStream.ResetHandlers()
|
||||||
|
|
||||||
|
m := menu.NewMenu()
|
||||||
|
m.Selectable = true
|
||||||
|
m.SortItems = false
|
||||||
|
m.BorderLabel = "Columns"
|
||||||
|
|
||||||
|
rebuild := func() {
|
||||||
|
m.ClearItems()
|
||||||
|
for _, col := range config.GlobalColumns {
|
||||||
|
txt := fmt.Sprintf("%s [%t]", col.Label, col.Enabled)
|
||||||
|
m.AddItems(menu.Item{col.Name, txt})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
upFn := func() {
|
||||||
|
config.ColumnLeft(m.SelectedValue())
|
||||||
|
m.Up()
|
||||||
|
rebuild()
|
||||||
|
}
|
||||||
|
|
||||||
|
downFn := func() {
|
||||||
|
config.ColumnRight(m.SelectedValue())
|
||||||
|
m.Down()
|
||||||
|
rebuild()
|
||||||
|
}
|
||||||
|
|
||||||
|
toggleFn := func() {
|
||||||
|
config.ColumnToggle(m.SelectedValue())
|
||||||
|
rebuild()
|
||||||
|
}
|
||||||
|
|
||||||
|
rebuild()
|
||||||
|
|
||||||
|
HandleKeys("up", m.Up)
|
||||||
|
HandleKeys("down", m.Down)
|
||||||
|
HandleKeys("enter", toggleFn)
|
||||||
|
HandleKeys("pgup", upFn)
|
||||||
|
HandleKeys("pgdown", downFn)
|
||||||
|
|
||||||
|
ui.Handle("/sys/kbd/x", func(ui.Event) { toggleFn() })
|
||||||
|
ui.Handle("/sys/kbd/<enter>", func(ui.Event) { toggleFn() })
|
||||||
|
|
||||||
|
HandleKeys("exit", func() {
|
||||||
|
cSource, err := cursor.cSuper.Get()
|
||||||
|
if err == nil {
|
||||||
|
for _, c := range cSource.All() {
|
||||||
|
c.RecreateWidgets()
|
||||||
|
}
|
||||||
|
}
|
||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -202,7 +263,7 @@ func ContainerMenu() MenuFn {
|
|||||||
})
|
})
|
||||||
|
|
||||||
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
||||||
selected = m.SelectedItem().Val
|
selected = m.SelectedValue()
|
||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
})
|
})
|
||||||
ui.Handle("/sys/kbd/", func(ui.Event) {
|
ui.Handle("/sys/kbd/", func(ui.Event) {
|
||||||
@ -321,7 +382,7 @@ func Confirm(txt string, fn func()) MenuFn {
|
|||||||
ui.Handle("/sys/kbd/y", func(ui.Event) { yes() })
|
ui.Handle("/sys/kbd/y", func(ui.Event) { yes() })
|
||||||
|
|
||||||
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
||||||
switch m.SelectedItem().Val {
|
switch m.SelectedValue() {
|
||||||
case "cancel":
|
case "cancel":
|
||||||
no()
|
no()
|
||||||
case "yes":
|
case "yes":
|
||||||
|
@ -55,6 +55,11 @@ func (m *Menu) DelItem(s string) (success bool) {
|
|||||||
return success
|
return success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClearItems removes all current menu items
|
||||||
|
func (m *Menu) ClearItems() {
|
||||||
|
m.items = m.items[:0]
|
||||||
|
}
|
||||||
|
|
||||||
// Move cursor to an position by Item value or label
|
// Move cursor to an position by Item value or label
|
||||||
func (m *Menu) SetCursor(s string) (success bool) {
|
func (m *Menu) SetCursor(s string) (success bool) {
|
||||||
for n, i := range m.items {
|
for n, i := range m.items {
|
||||||
@ -79,6 +84,10 @@ func (m *Menu) SelectedItem() Item {
|
|||||||
return m.items[m.cursorPos]
|
return m.items[m.cursorPos]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Menu) SelectedValue() string {
|
||||||
|
return m.items[m.cursorPos].Val
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Menu) Buffer() ui.Buffer {
|
func (m *Menu) Buffer() ui.Buffer {
|
||||||
var cell ui.Cell
|
var cell ui.Cell
|
||||||
buf := m.Block.Buffer()
|
buf := m.Block.Buffer()
|
||||||
|
Loading…
Reference in New Issue
Block a user