filter/filter.go

192 lines
4.6 KiB
Go

/*
filter --- Simple interactive data filter for POSIX pipelines
Copyright (c) 2022 Sameer Rahmani <lxsameer@gnu.org>
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 <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"log"
"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
// 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 []rune(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 tcell.Screen, inputs Items, lowerBound int, upperBound int, current int) {
y := 2
status := fmt.Sprintf("Navigation: [UP or C-p] [Down or C-n] | Select: SPACE | Cancel: [q ESC] | Done: [Enter] | Line: %d", current)
drawText(s, 0, 0, xmax-1, 1, helpStyle, status)
for i, item := range (*inputs)[lowerBound: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 current == lowerBound+i {
style = highlight
}
drawText(s, 0, y, xmax-1, y+1, style, txt)
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.ColorDarkGray).Foreground(tcell.ColorWhite)
selectedStyle = tcell.StyleDefault.Foreground(tcell.ColorYellow)
helpStyle = tcell.StyleDefault.Foreground(tcell.ColorDefault)
currentLine := 0
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()
lowerBound := 0
upperBound := min(ymax-3, len(*input))
// Event loop
for !done {
//s.Clear()
Render(s, input, lowerBound, upperBound, currentLine)
// 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)[currentLine]
item.Selected = !item.Selected
} else if ev.Key() == tcell.KeyUp || ev.Key() == tcell.KeyCtrlP {
if currentLine > 0 {
currentLine -= 1
}
if currentLine <= lowerBound && currentLine != 0 {
lowerBound -= 1
upperBound -= 1
}
} else if ev.Key() == tcell.KeyDown || ev.Key() == tcell.KeyCtrlN {
if currentLine < len(*input)-1 {
currentLine += 1
}
if currentLine >= upperBound && currentLine != len(*input)-1 {
lowerBound += 1
upperBound += 1
}
}
}
}
// 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)
}
}
}