2022-11-23 21:13:14 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2022-11-23 21:09:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
)
|
|
|
|
|
2022-11-23 21:24:54 +00:00
|
|
|
// TODO: Right now, filter does not handle terminal resize properly,
|
|
|
|
//fix it!
|
|
|
|
|
|
|
|
// Screen size
|
2022-11-23 21:09:40 +00:00
|
|
|
var xmax int
|
|
|
|
var ymax int
|
|
|
|
|
2022-11-23 21:24:54 +00:00
|
|
|
// TODO: Make the highlight style configuratble.
|
|
|
|
// The style to be used with the current line
|
2022-11-23 21:09:40 +00:00
|
|
|
var highlight tcell.Style
|
|
|
|
|
2022-11-23 21:24:54 +00:00
|
|
|
// Draws the given `text` to the screen `s` at the given coordinates. Remember that
|
|
|
|
// the text will be wrapped if it overflow.
|
2022-11-23 21:09:40 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 21:24:54 +00:00
|
|
|
// Main render function that gets called during the event loop.
|
2022-11-23 21:09:40 +00:00
|
|
|
func Render(s tcell.Screen, inputs Items, lowerBound int, upperBound int, current int) {
|
|
|
|
y := 1
|
|
|
|
status := fmt.Sprintf("Current: %d | lower: %d | upper: %d | max: %d", current, lowerBound, upperBound, ymax)
|
|
|
|
drawText(s, 0, 0, xmax-1, 1, tcell.StyleDefault, status)
|
|
|
|
for i, item := range (*inputs)[lowerBound:upperBound] {
|
|
|
|
style := tcell.StyleDefault
|
|
|
|
var txt string
|
|
|
|
|
|
|
|
if current == lowerBound+i {
|
|
|
|
style = highlight
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.Selected {
|
|
|
|
txt = fmt.Sprintf("XXXX | %s", item.Text)
|
|
|
|
} else {
|
|
|
|
txt = fmt.Sprintf("%04d | %s", lowerBound+i, item.Text)
|
|
|
|
}
|
|
|
|
|
|
|
|
drawText(s, 0, y, xmax-1, y+1, style, txt)
|
|
|
|
y += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
input, err := ReadFromStdin()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
2022-11-23 21:24:54 +00:00
|
|
|
// TODO: Make the default style configurable
|
2022-11-23 21:09:40 +00:00
|
|
|
defStyle := tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault)
|
|
|
|
highlight = tcell.StyleDefault.Background(tcell.ColorDarkGray).Foreground(tcell.ColorWhite)
|
|
|
|
currentLine := 0
|
2022-11-23 21:24:54 +00:00
|
|
|
done := false
|
2022-11-23 21:09:40 +00:00
|
|
|
|
|
|
|
// 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))
|
2022-11-23 21:24:54 +00:00
|
|
|
|
2022-11-23 21:09:40 +00:00
|
|
|
// Event loop
|
2022-11-23 21:26:49 +00:00
|
|
|
for !done {
|
2022-11-23 21:09:40 +00:00
|
|
|
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() == ' ' {
|
|
|
|
item := (*input)[currentLine]
|
|
|
|
item.Selected = !item.Selected
|
2022-11-23 21:24:54 +00:00
|
|
|
} else if ev.Key() == tcell.KeyUp || ev.Key() == tcell.KeyCtrlP {
|
2022-11-23 21:09:40 +00:00
|
|
|
if currentLine > 0 {
|
|
|
|
currentLine -= 1
|
|
|
|
}
|
|
|
|
if currentLine <= lowerBound && currentLine != 0 {
|
|
|
|
lowerBound -= 1
|
|
|
|
upperBound -= 1
|
|
|
|
}
|
|
|
|
|
2022-11-23 21:24:54 +00:00
|
|
|
} else if ev.Key() == tcell.KeyDown || ev.Key() == tcell.KeyCtrlN {
|
2022-11-23 21:09:40 +00:00
|
|
|
if currentLine < len(*input)-1 {
|
|
|
|
currentLine += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentLine >= upperBound && currentLine != len(*input)-1 {
|
|
|
|
lowerBound += 1
|
|
|
|
upperBound += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 21:24:54 +00:00
|
|
|
|
|
|
|
// Deinitialize the screen so we can use the stdout properly
|
2022-11-23 21:09:40 +00:00
|
|
|
s.Fini()
|
2022-11-23 21:24:54 +00:00
|
|
|
|
|
|
|
// Write what ever that got selected to the stdout
|
2022-11-23 21:09:40 +00:00
|
|
|
for _, item := range *input {
|
|
|
|
if item.Selected {
|
|
|
|
fmt.Println(item.Text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|