/* filter --- Simple interactive data filter for POSIX pipelines Copyright (c) 2022 Sameer Rahmani This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package main import ( "fmt" "log" "strings" "github.com/gdamore/tcell/v2" ) // TODO: Use tcell/v2/views instead of a manual scrolling computation // TODO: Right now, filter does not handle terminal resize properly, //fix it! // Screen size var xmax int var ymax int // TODO: Make the highlight style configuratble. // The style to be used with the current line var highlight tcell.Style var selectedStyle tcell.Style var helpStyle tcell.Style type State struct { Current int InSelectionMode bool UpperBound int LowerBound int Screen *tcell.Screen } // Draws the given `text` to the screen `s` at the given coordinates. Remember that // the text will be wrapped if it overflow. func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) { row := y1 col := x1 for _, r := range text { s.SetContent(col, row, r, nil, style) col++ if col >= x2 { row++ col = x1 } if row > y2 { break } } } // Main render function that gets called during the event loop. func Render(s State, inputs Items) { y := 2 status := fmt.Sprintf("Navigation: [UP or C-p] [Down or C-n] | Select: SPACE | Cancel: [q ESC] | Done: [Enter] | Line: %d", s.Current+1) drawText(*s.Screen, 0, 0, xmax-1, 1, helpStyle, status) for i, item := range (*inputs)[s.LowerBound:s.UpperBound] { style := tcell.StyleDefault var txt string if item.Selected { txt = fmt.Sprintf(" X %s", item.Text) style = selectedStyle } else { txt = fmt.Sprintf(" %s", item.Text) } if s.Current == s.LowerBound+i { style = highlight } drawText(*s.Screen, 0, y, xmax-1, y+1, style, txt) // Clean up the rest of the line that might've been filled with a previous render. // This is a hack to NOT use `Clear` on each loop drawText(*s.Screen, len(txt), y, xmax-1, y+1, style, strings.Repeat(" ", xmax-1-len(txt))) if s.Current == s.LowerBound+i { drawText(*s.Screen, 0, y, 1, y+1, tcell.StyleDefault.Foreground(tcell.ColorGreen).Bold(true), ">") } y += 1 } } func main() { input, err := ReadFromStdin() if err != nil { log.Fatalf("%+v", err) } // TODO: Make the default style configurable defStyle := tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault) highlight = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorWhite).Bold(true) selectedStyle = tcell.StyleDefault.Foreground(tcell.ColorYellow) helpStyle = tcell.StyleDefault.Foreground(tcell.ColorDefault) done := false // Initialize screen tty, err := tcell.NewDevTty() if err != nil { log.Fatalf("%+v", err) } s, err := tcell.NewTerminfoScreenFromTty(tty) if err != nil { log.Fatalf("%+v", err) } if err := s.Init(); err != nil { log.Fatalf("%+v", err) } s.SetStyle(defStyle) s.Clear() xmax, ymax = s.Size() quit := func() { // You have to catch panics in a defer, clean up, and // re-raise them - otherwise your application can // die without leaving any diagnostic trace. maybePanic := recover() s.Fini() if maybePanic != nil { panic(maybePanic) } } defer quit() startOfSelection := 0 state := State{ Current: 0, Screen: &s, LowerBound: 0, UpperBound: min(ymax-3, len(*input)), InSelectionMode: false, } // Event loop for !done { Render(state, input) // Update screen s.Show() // Poll event ev := s.PollEvent() // Process event switch ev := ev.(type) { case *tcell.EventResize: s.Sync() case *tcell.EventKey: if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC || ev.Rune() == 'q' { return } else if ev.Key() == tcell.KeyEnter { done = true break } else if ev.Key() == tcell.KeyCtrlL { s.Sync() } else if ev.Rune() == ' ' { // Mark the current line as selected item := (*input)[state.Current] item.Selected = !item.Selected } else if ev.Key() == tcell.KeyUp || ev.Key() == tcell.KeyCtrlP { if state.Current > 0 { state.Current -= 1 } if state.Current <= state.LowerBound && state.Current != 0 { state.LowerBound -= 1 state.UpperBound -= 1 } } else if ev.Key() == tcell.KeyDown || ev.Key() == tcell.KeyCtrlN { if state.Current < len(*input)-1 { state.Current += 1 } if state.Current >= state.UpperBound && state.Current != len(*input)-1 { state.LowerBound += 1 state.UpperBound += 1 } } else if ev.Key() == tcell.KeyCtrlE || ev.Key() == tcell.KeyEnd { state.UpperBound = len(*input) state.LowerBound = state.UpperBound - ymax + 2 state.Current = len(*input) - 1 } else if ev.Key() == tcell.KeyCtrlA || ev.Key() == tcell.KeyHome { state.UpperBound = ymax - 2 state.LowerBound = 0 state.Current = 0 } else if ev.Key() == tcell.KeyCtrlSpace { if state.InSelectionMode { start := min(startOfSelection, state.Current) end := max(startOfSelection, state.Current) toggleItems(input, start, end) } else { startOfSelection = state.Current } state.InSelectionMode = !state.InSelectionMode } } } // Deinitialize the screen so we can use the stdout properly s.Fini() // Write what ever that got selected to the stdout for _, item := range *input { if item.Selected { fmt.Println(item.Text) } } }