Skip to content

Commit

Permalink
init column config menu
Browse files Browse the repository at this point in the history
  • Loading branch information
bcicen committed Oct 25, 2020
1 parent ffb96f4 commit 7c6b5c5
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 15 deletions.
7 changes: 7 additions & 0 deletions config/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ func EnabledColumns() (a []string) {
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
func ColumnLeft(name string) {
idx := colIndex(name)
Expand Down
5 changes: 2 additions & 3 deletions config/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ func Toggle(k string) {
lock.Lock()
defer lock.Unlock()

newVal := !sw.Val
log.Noticef("config change [%s]: %t -> %t", k, sw.Val, newVal)
sw.Val = newVal
sw.Val = !sw.Val
log.Noticef("config change [%s]: %t -> %t", k, !sw.Val, sw.Val)
//log.Errorf("ignoring toggle for non-existant switch: %s", k)
}
6 changes: 6 additions & 0 deletions container/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
c.updater = u
c.updater.SetMeta(c.Meta)
Expand Down
21 changes: 13 additions & 8 deletions cwidgets/compact/grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type CompactGrid struct {

func NewCompactGrid() *CompactGrid {
cg := &CompactGrid{header: NewCompactHeader()}
cg.RebuildHeader()
cg.rebuildHeader()
return cg
}

Expand All @@ -30,22 +30,19 @@ func (cg *CompactGrid) Align() {

// update row ypos, width recursively
colWidths := cg.calcWidths()
cg.header.SetWidths(cg.Width, colWidths)
for _, r := range cg.pageRows() {
r.SetY(y)
y += r.GetHeight()
r.SetWidths(cg.Width, colWidths)
}
}

func (cg *CompactGrid) RebuildHeader() {
cg.cols = newRowWidgets()
cg.header.clearFieldPars()
for _, col := range cg.cols {
cg.header.addFieldPar(col.Header())
}
func (cg *CompactGrid) Clear() {
cg.Rows = []RowBufferer{}
cg.rebuildHeader()
}

func (cg *CompactGrid) Clear() { cg.Rows = []RowBufferer{} }
func (cg *CompactGrid) GetHeight() int { return len(cg.Rows) + cg.header.Height }
func (cg *CompactGrid) SetX(x int) { cg.X = x }
func (cg *CompactGrid) SetY(y int) { cg.Y = y }
Expand Down Expand Up @@ -93,3 +90,11 @@ func (cg *CompactGrid) Buffer() ui.Buffer {
func (cg *CompactGrid) AddRows(rows ...RowBufferer) {
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())
}
}
5 changes: 4 additions & 1 deletion cwidgets/compact/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ type CompactHeader struct {
}

func NewCompactHeader() *CompactHeader {
return &CompactHeader{Height: 2}
return &CompactHeader{
X: rowPadding,
Height: 2,
}
}

func (row *CompactHeader) GetHeight() int {
Expand Down
8 changes: 8 additions & 0 deletions cwidgets/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ type WidgetUpdater interface {
SetMeta(models.Meta)
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 changes: 4 additions & 0 deletions grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ func Display() bool {
menu = SortMenu
ui.StopLoop()
})
ui.Handle("/sys/kbd/c", func(ui.Event) {
menu = ColumnsMenu
ui.StopLoop()
})
ui.Handle("/sys/kbd/S", func(ui.Event) {
path, err := config.Write()
if err == nil {
Expand Down
67 changes: 64 additions & 3 deletions menus.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,68 @@ func SortMenu() MenuFn {
HandleKeys("exit", ui.StopLoop)

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()
})

Expand Down Expand Up @@ -202,7 +263,7 @@ func ContainerMenu() MenuFn {
})

ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
selected = m.SelectedItem().Val
selected = m.SelectedValue()
ui.StopLoop()
})
ui.Handle("/sys/kbd/", func(ui.Event) {
Expand Down Expand Up @@ -321,7 +382,7 @@ func Confirm(txt string, fn func()) MenuFn {
ui.Handle("/sys/kbd/y", func(ui.Event) { yes() })

ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
switch m.SelectedItem().Val {
switch m.SelectedValue() {
case "cancel":
no()
case "yes":
Expand Down
9 changes: 9 additions & 0 deletions widgets/menu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func (m *Menu) DelItem(s string) (success bool) {
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
func (m *Menu) SetCursor(s string) (success bool) {
for n, i := range m.items {
Expand All @@ -79,6 +84,10 @@ func (m *Menu) SelectedItem() Item {
return m.items[m.cursorPos]
}

func (m *Menu) SelectedValue() string {
return m.items[m.cursorPos].Val
}

func (m *Menu) Buffer() ui.Buffer {
var cell ui.Cell
buf := m.Block.Buffer()
Expand Down

0 comments on commit 7c6b5c5

Please sign in to comment.