Author: Adam <git@apiote.xyz>
add PageDown and Space for next honk
gonk.go | 19 +++++++++++--------
diff --git a/gonk.go b/gonk.go index 40e11d17986da2200162d90dd8de04a30aee587a..49307f1e084420d27aa86a1e8ed48d5c5bdfb01a 100644 --- a/gonk.go +++ b/gonk.go @@ -47,6 +47,10 @@ type HonkSet struct { Honks []Honk } +func isPgDn(b []byte) bool { + return len(b) == 4 && b[0] == 27 && b[1] == 91 && b[2] == 54 && b[3] == 126 +} + func dataHome() string { xdgDataHome, _ := os.LookupEnv("XDG_DATA_HOME") if xdgDataHome == "" { @@ -164,19 +168,19 @@ } return honks } -func readkey() rune { +func readkey() []byte { exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run() exec.Command("stty", "-F", "/dev/tty", "-echo").Run() defer exec.Command("stty", "-F", "/dev/tty", "echo").Run() - var b []byte = make([]byte, 1) + var b []byte = make([]byte, 4) os.Stdin.Read(b) - key := rune(b[0]) - return key + return b } func showHonks(honks HonkSet, after int, colour Colour) (int, bool) { broken := false honksNumber := len(honks.Honks) + fmt.Println(colour.Cyan + "## o to open in browser, n/Space/PgDn for next, d to show donks, q to exit " + colour.Reset) for i, honk := range honks.Honks { md := html2md.Convert(honk.Noise) md = html.UnescapeString(md) @@ -186,19 +190,18 @@ fmt.Printf("%s\n\n", md) for i, donk := range honk.Donks { fmt.Printf(colour.White+"Donk #%d: %s\n\n"+colour.Reset, i, donk.URL) } - fmt.Printf(colour.Cyan + "## Press o to open in browser, n to show next, or q to exit " + colour.Reset) for { key := readkey() - if key == 'n' { + if rune(key[0]) == 'n' || rune(key[0]) == ' ' || isPgDn(key) { after = honk.ID fmt.Printf("\n\n") break } - if key == 'o' { + if rune(key[0]) == 'o' { exec.Command("xdg-open", honk.XID).Run() continue } - if key == 'q' { + if rune(key[0]) == 'q' { broken = true return after, broken }