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"
|
2022-11-24 15:34:17 +00:00
|
|
|
"strings"
|
2022-11-23 21:09:40 +00:00
|
|
|
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
)
|
|
|
|
|
2022-11-23 22:39:46 +00:00
|
|
|
// TODO: Use tcell/v2/views instead of a manual scrolling computation
|
|
|
|
|
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-25 15:18:06 +00:00
|
|
|
var highlight,
|
|
|
|
selectedStyle,
|
|
|
|
helpStyle,
|
|
|
|
selectionStyle,
|
|
|
|
selectionArrow,
|
2022-11-25 16:40:02 +00:00
|
|
|
cursorStyle,
|
2022-11-25 15:18:06 +00:00
|
|
|
normalArrow tcell.Style
|
2022-11-23 21:09:40 +00:00
|
|
|
|
2022-11-25 15:30:24 +00:00
|
|
|
type Scene struct {
|
2022-11-25 16:40:02 +00:00
|
|
|
RenderFn func(*State, Items)
|
|
|
|
KeyHandlerFn func(*State, tcell.EventKey, Items)
|
2022-11-25 15:30:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 13:51:30 +00:00
|
|
|
type State struct {
|
2022-11-25 15:18:06 +00:00
|
|
|
Current int
|
|
|
|
InSelectionMode bool
|
|
|
|
SelectionStartLine int
|
|
|
|
UpperBound int
|
|
|
|
LowerBound int
|
|
|
|
Screen *tcell.Screen
|
2022-11-25 16:40:02 +00:00
|
|
|
Scenes []*Scene
|
|
|
|
CurrentScene int
|
|
|
|
UserInput string
|
2022-11-25 13:51:30 +00:00
|
|
|
}
|
|
|
|
|
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
|
2022-11-24 15:34:17 +00:00
|
|
|
for _, r := range text {
|
2022-11-23 21:09:40 +00:00
|
|
|
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-25 15:30:24 +00:00
|
|
|
func Render(s *State, inputs Items) {
|
2022-11-25 16:40:02 +00:00
|
|
|
scene := s.Scenes[s.CurrentScene]
|
|
|
|
scene.RenderFn(s, inputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RenderHelp(s *State, inputs Items) {
|
|
|
|
drawText(*s.Screen, 0, 4, xmax-1, 4+1, normalArrow, "hnthnt")
|
|
|
|
}
|
|
|
|
|
|
|
|
func RenderMain(s *State, inputs Items) {
|
2022-11-23 21:34:30 +00:00
|
|
|
y := 2
|
2022-11-25 17:29:31 +00:00
|
|
|
//status := fmt.Sprintf("[Line: %d][? For help]: %s", s.Current+1, s.UserInput)
|
|
|
|
status := fmt.Sprintf("L: %d, U: %d, S: %d [Line: %d][? For help]: %s", s.LowerBound, s.UpperBound, s.SelectionStartLine, s.Current+1, s.UserInput)
|
2022-11-25 14:06:05 +00:00
|
|
|
drawText(*s.Screen, 0, 0, xmax-1, 1, helpStyle, status)
|
2022-11-25 16:40:02 +00:00
|
|
|
drawText(*s.Screen, len(status), 0, xmax-1, 1, cursorStyle, "⎥")
|
2022-11-25 17:29:31 +00:00
|
|
|
|
2022-11-25 14:06:05 +00:00
|
|
|
for i, item := range (*inputs)[s.LowerBound:s.UpperBound] {
|
2022-11-23 21:09:40 +00:00
|
|
|
style := tcell.StyleDefault
|
2022-11-25 15:18:06 +00:00
|
|
|
arrowStyle := normalArrow
|
2022-11-25 17:29:31 +00:00
|
|
|
index := s.LowerBound + i
|
2022-11-25 15:18:06 +00:00
|
|
|
|
2022-11-23 21:09:40 +00:00
|
|
|
var txt string
|
|
|
|
|
|
|
|
if item.Selected {
|
2022-11-25 16:40:02 +00:00
|
|
|
txt = fmt.Sprintf(" X %s", item.Text)
|
2022-11-23 22:31:30 +00:00
|
|
|
style = selectedStyle
|
2022-11-23 21:09:40 +00:00
|
|
|
} else {
|
2022-11-24 15:34:17 +00:00
|
|
|
txt = fmt.Sprintf(" %s", item.Text)
|
2022-11-23 22:31:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 14:06:05 +00:00
|
|
|
if s.Current == s.LowerBound+i {
|
2022-11-23 22:31:30 +00:00
|
|
|
style = highlight
|
2022-11-23 21:09:40 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 15:18:06 +00:00
|
|
|
if s.InSelectionMode {
|
2022-11-25 17:29:31 +00:00
|
|
|
if (s.SelectionStartLine < s.LowerBound && index <= s.Current) || (s.SelectionStartLine > s.UpperBound && index >= s.Current) {
|
2022-11-25 15:18:06 +00:00
|
|
|
style = selectionStyle
|
|
|
|
arrowStyle = selectionArrow
|
2022-11-25 16:40:02 +00:00
|
|
|
} else {
|
|
|
|
low := min(s.SelectionStartLine, s.Current)
|
|
|
|
high := max(s.SelectionStartLine, s.Current)
|
|
|
|
|
2022-11-25 17:29:31 +00:00
|
|
|
if index >= low && index <= high {
|
2022-11-25 16:40:02 +00:00
|
|
|
style = selectionStyle
|
|
|
|
arrowStyle = selectionArrow
|
|
|
|
}
|
2022-11-25 15:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 16:40:02 +00:00
|
|
|
drawText(*s.Screen, 0, y, len(txt), y+1, style, txt)
|
2022-11-24 15:34:17 +00:00
|
|
|
// 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
|
2022-11-25 16:40:02 +00:00
|
|
|
drawText(*s.Screen, len(txt), y, xmax-1, y+1, style, strings.Repeat(" ", xmax-len(txt)))
|
|
|
|
|
2022-11-25 14:06:05 +00:00
|
|
|
if s.Current == s.LowerBound+i {
|
2022-11-25 15:18:06 +00:00
|
|
|
drawText(*s.Screen, 0, y, 1, y+1, arrowStyle, ">")
|
2022-11-24 15:34:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 21:09:40 +00:00
|
|
|
y += 1
|
|
|
|
}
|
2022-11-24 15:34:17 +00:00
|
|
|
|
2022-11-23 21:09:40 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 16:40:02 +00:00
|
|
|
func HandleKeyEvents(s *State, ev tcell.EventKey, inputs Items) {
|
|
|
|
scene := s.Scenes[s.CurrentScene]
|
|
|
|
scene.KeyHandlerFn(s, ev, inputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleMain(state *State, ev tcell.EventKey, input Items) {
|
|
|
|
if ev.Key() == tcell.KeyTAB {
|
|
|
|
// Mark the current line as selected
|
2022-11-25 17:33:56 +00:00
|
|
|
if state.InSelectionMode {
|
|
|
|
start := min(state.SelectionStartLine, state.Current)
|
|
|
|
end := max(state.SelectionStartLine, state.Current)
|
|
|
|
toggleItems(input, start, end+1)
|
|
|
|
state.SelectionStartLine = -1
|
|
|
|
state.InSelectionMode = false
|
|
|
|
} else {
|
|
|
|
item := (*input)[state.Current]
|
|
|
|
item.Selected = !item.Selected
|
|
|
|
}
|
2022-11-25 16:40:02 +00:00
|
|
|
|
|
|
|
} 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 {
|
|
|
|
state.SelectionStartLine = -1
|
|
|
|
} else {
|
|
|
|
state.SelectionStartLine = state.Current
|
|
|
|
}
|
|
|
|
|
|
|
|
state.InSelectionMode = !state.InSelectionMode
|
|
|
|
|
|
|
|
} else if ev.Key() == tcell.KeyCtrlH {
|
|
|
|
state.CurrentScene = 1
|
2022-11-25 17:29:31 +00:00
|
|
|
} else if ev.Key() == tcell.KeyBackspace {
|
|
|
|
state.UserInput = string(state.UserInput[:len(state.UserInput)-2])
|
2022-11-25 16:40:02 +00:00
|
|
|
} else {
|
|
|
|
state.UserInput = state.UserInput + string(ev.Rune())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleHelp(state *State, ev tcell.EventKey, input Items) {
|
|
|
|
if ev.Rune() == 'q' {
|
|
|
|
state.CurrentScene = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 21:09:40 +00:00
|
|
|
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)
|
2022-11-25 15:18:06 +00:00
|
|
|
highlight = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault).Bold(true)
|
|
|
|
selectedStyle = tcell.StyleDefault.Foreground(tcell.ColorPlum)
|
2022-11-23 22:31:30 +00:00
|
|
|
helpStyle = tcell.StyleDefault.Foreground(tcell.ColorDefault)
|
2022-11-25 15:18:06 +00:00
|
|
|
selectionStyle = tcell.StyleDefault.Background(tcell.ColorRebeccaPurple).Foreground(tcell.ColorDefault)
|
|
|
|
selectionArrow = tcell.StyleDefault.Background(tcell.ColorRebeccaPurple).Foreground(tcell.ColorGreen).Bold(true)
|
|
|
|
normalArrow = tcell.StyleDefault.Foreground(tcell.ColorGreen).Bold(true)
|
2022-11-25 16:40:02 +00:00
|
|
|
cursorStyle = normalArrow.Blink(true)
|
2022-11-23 22:31:30 +00:00
|
|
|
|
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()
|
|
|
|
|
2022-11-25 16:40:02 +00:00
|
|
|
mainScene := Scene{
|
|
|
|
RenderMain,
|
|
|
|
HandleMain,
|
|
|
|
}
|
|
|
|
helpScene := Scene{
|
|
|
|
RenderHelp,
|
|
|
|
HandleHelp,
|
|
|
|
}
|
|
|
|
|
2022-11-25 14:06:05 +00:00
|
|
|
state := State{
|
2022-11-25 15:18:06 +00:00
|
|
|
Current: 0,
|
|
|
|
SelectionStartLine: 0,
|
|
|
|
Screen: &s,
|
|
|
|
LowerBound: 0,
|
|
|
|
UpperBound: min(ymax-3, len(*input)),
|
|
|
|
InSelectionMode: false,
|
2022-11-25 16:40:02 +00:00
|
|
|
CurrentScene: 0,
|
|
|
|
Scenes: []*Scene{&mainScene, &helpScene},
|
2022-11-25 14:06:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 16:40:02 +00:00
|
|
|
previousScene := 0
|
|
|
|
|
2022-11-23 21:09:40 +00:00
|
|
|
// Event loop
|
2022-11-23 21:26:49 +00:00
|
|
|
for !done {
|
2022-11-25 16:40:02 +00:00
|
|
|
// If we need to render a new scene let's clear the screen first
|
|
|
|
if state.CurrentScene != previousScene {
|
|
|
|
s := *state.Screen
|
|
|
|
s.Clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
Render(&state, input)
|
2022-11-23 21:09:40 +00:00
|
|
|
|
|
|
|
// Update screen
|
|
|
|
s.Show()
|
|
|
|
|
|
|
|
// Poll event
|
|
|
|
ev := s.PollEvent()
|
|
|
|
|
|
|
|
// Process event
|
|
|
|
switch ev := ev.(type) {
|
|
|
|
case *tcell.EventResize:
|
|
|
|
s.Sync()
|
|
|
|
case *tcell.EventKey:
|
2022-11-25 16:40:02 +00:00
|
|
|
if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
|
2022-11-23 21:09:40 +00:00
|
|
|
return
|
|
|
|
} else if ev.Key() == tcell.KeyEnter {
|
|
|
|
done = true
|
|
|
|
break
|
|
|
|
} else if ev.Key() == tcell.KeyCtrlL {
|
|
|
|
s.Sync()
|
2022-11-25 16:40:02 +00:00
|
|
|
} else {
|
|
|
|
// In case of no global binding hand the even to the scene
|
|
|
|
HandleKeyEvents(&state, *ev, input)
|
2022-11-23 21:09:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|