diff --git a/grid.go b/grid.go index 4e9ce1e..f611507 100644 --- a/grid.go +++ b/grid.go @@ -141,6 +141,10 @@ func Display(g *Grid) bool { expand = true ui.StopLoop() }) + ui.Handle("/sys/kbd/f", func(ui.Event) { + menu = FilterMenu + ui.StopLoop() + }) ui.Handle("/sys/kbd/h", func(ui.Event) { menu = HelpMenu ui.StopLoop() @@ -148,7 +152,7 @@ func Display(g *Grid) bool { ui.Handle("/sys/kbd/q", func(ui.Event) { ui.StopLoop() }) - ui.Handle("/sys/kbd/r", func(ui.Event) { + ui.Handle("/sys/kbd/r", func(e ui.Event) { GlobalConfig.toggle("sortReversed") }) ui.Handle("/sys/kbd/s", func(ui.Event) { diff --git a/menus.go b/menus.go index aa7f10f..527670a 100644 --- a/menus.go +++ b/menus.go @@ -24,6 +24,19 @@ func HelpMenu() { ui.Loop() } +func FilterMenu() { + i := widgets.NewInput() + i.TextFgColor = ui.ColorWhite + i.BorderLabel = "Filter" + i.BorderFg = ui.ColorCyan + ui.Render(i) + i.InputHandlers() + ui.Handle("/sys/kbd/", func(ui.Event) { + ui.StopLoop() + }) + ui.Loop() +} + func SortMenu() { m := widgets.NewMenu(SortFields()) m.Selectable = true diff --git a/widgets/input.go b/widgets/input.go new file mode 100644 index 0000000..6e9725a --- /dev/null +++ b/widgets/input.go @@ -0,0 +1,64 @@ +package widgets + +import ( + "strings" + + ui "github.com/gizak/termui" +) + +var ( + input_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_." +) + +type Input struct { + ui.Block + Label string + Data string + TextFgColor ui.Attribute + TextBgColor ui.Attribute +} + +func NewInput() *Input { + i := &Input{ + Block: *ui.NewBlock(), + Label: "input", + TextFgColor: ui.ThemeAttr("par.text.fg"), + TextBgColor: ui.ThemeAttr("par.text.bg"), + } + i.Width, i.Height = 30, 3 + return i +} + +func (i *Input) Buffer() ui.Buffer { + var cell ui.Cell + buf := i.Block.Buffer() + + x := x_padding + for _, ch := range i.Data { + cell = ui.Cell{Ch: ch, Fg: i.TextFgColor, Bg: i.TextBgColor} + buf.Set(x, 1, cell) + x++ + } + + return buf +} + +func (i *Input) KeyPress(e ui.Event) { + ch := strings.Replace(e.Path, "/sys/kbd/", "", -1) + if ch == "C-8" { + idx := len(i.Data) - 1 + if idx > -1 { + i.Data = i.Data[0:idx] + } + ui.Render(i) + } + if strings.Index(input_chars, ch) > -1 { + i.Data += ch + ui.Render(i) + } +} + +// Setup some default handlers for menu navigation +func (i *Input) InputHandlers() { + ui.Handle("/sys/kbd/", i.KeyPress) +}