Add documentation to the source and and replace 'r' and 'n' with Emacs keybindings
This commit is contained in:
parent
022070ad11
commit
6a3af2a0ac
30
filter.go
30
filter.go
|
@ -22,11 +22,19 @@ import (
|
|||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
// 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
|
||||
|
||||
// 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
|
||||
|
@ -43,6 +51,7 @@ func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string
|
|||
}
|
||||
}
|
||||
|
||||
// Main render function that gets called during the event loop.
|
||||
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)
|
||||
|
@ -72,9 +81,11 @@ func main() {
|
|||
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)
|
||||
currentLine := 0
|
||||
done := false
|
||||
|
||||
// Initialize screen
|
||||
tty, err := tcell.NewDevTty()
|
||||
|
@ -105,22 +116,15 @@ func main() {
|
|||
}
|
||||
defer quit()
|
||||
|
||||
// Here's an example of how to inject a keystroke where it will
|
||||
// be picked up by the next PollEvent call. Note that the
|
||||
// queue is LIFO, it has a limited length, and PostEvent() can
|
||||
// return an error.
|
||||
// s.PostEvent(tcell.NewEventKey(tcell.KeyRune, rune('a'), 0))
|
||||
|
||||
done := false
|
||||
lowerBound := 0
|
||||
upperBound := min(ymax-3, len(*input))
|
||||
|
||||
// Event loop
|
||||
for {
|
||||
if done {
|
||||
break
|
||||
}
|
||||
s.Clear()
|
||||
//window := (*input)[lowerBound:upperBound]
|
||||
Render(s, input, lowerBound, upperBound, currentLine)
|
||||
|
||||
// Update screen
|
||||
|
@ -141,12 +145,10 @@ func main() {
|
|||
break
|
||||
} else if ev.Key() == tcell.KeyCtrlL {
|
||||
s.Sync()
|
||||
} else if ev.Rune() == 'C' || ev.Rune() == 'c' {
|
||||
s.Clear()
|
||||
} else if ev.Rune() == ' ' {
|
||||
item := (*input)[currentLine]
|
||||
item.Selected = !item.Selected
|
||||
} else if ev.Key() == tcell.KeyUp || ev.Rune() == 'r' {
|
||||
} else if ev.Key() == tcell.KeyUp || ev.Key() == tcell.KeyCtrlP {
|
||||
if currentLine > 0 {
|
||||
currentLine -= 1
|
||||
}
|
||||
|
@ -155,7 +157,7 @@ func main() {
|
|||
upperBound -= 1
|
||||
}
|
||||
|
||||
} else if ev.Key() == tcell.KeyDown || ev.Rune() == 'n' {
|
||||
} else if ev.Key() == tcell.KeyDown || ev.Key() == tcell.KeyCtrlN {
|
||||
if currentLine < len(*input)-1 {
|
||||
currentLine += 1
|
||||
}
|
||||
|
@ -167,7 +169,11 @@ func main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
|
6
utils.go
6
utils.go
|
@ -36,6 +36,7 @@ func NewItem(text string) *Item {
|
|||
}
|
||||
}
|
||||
|
||||
// Pffff Golang, right? :D
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
|
@ -43,6 +44,9 @@ func min(a, b int) int {
|
|||
return b
|
||||
}
|
||||
|
||||
// Reads the input from stdin and makes sure that the current
|
||||
// process is being run in a pipeline and converts the data
|
||||
// to an array of items.
|
||||
func ReadFromStdin() (Items, error) {
|
||||
info, err := os.Stdin.Stat()
|
||||
if err != nil {
|
||||
|
@ -50,7 +54,7 @@ func ReadFromStdin() (Items, error) {
|
|||
}
|
||||
|
||||
if info.Mode()&os.ModeCharDevice != 0 {
|
||||
return nil, fmt.Errorf("The command is intended to work with pipes. For example `git log | filter | xarg | ...`")
|
||||
return nil, fmt.Errorf("the command is intended to work with pipes. For example `git log | filter | xarg | ...`")
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
|
Loading…
Reference in New Issue