Skip to content

Instantly share code, notes, and snippets.

@AWtnb
Last active July 23, 2023 11:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AWtnb/9a2ec3151ae44389e2b8c863b2d1564d to your computer and use it in GitHub Desktop.
Save AWtnb/9a2ec3151ae44389e2b8c863b2d1564d to your computer and use it in GitHub Desktop.
fuzzy-filter for PowerShell
package main
import (
"bufio"
"flag"
"fmt"
"os"
"github.com/ktr0731/go-fuzzyfinder"
)
func main() {
var (
bytearr bool
)
flag.BoolVar(&bytearr, "bytearr", false, "print as byte array")
flag.Parse()
os.Exit(run(bytearr))
}
func run(bytearr bool) int {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
return 1
}
lines := fromPipe()
if len(lines) == 0 {
return 1
}
idx, err := fuzzyfinder.Find(lines, func(i int) string {
return lines[i]
})
if err != nil {
return 1
}
l := lines[idx]
if bytearr {
printBytes(l)
} else {
fmt.Println(l)
}
return 0
}
func printBytes(s string) {
for _, b := range []byte(s) {
fmt.Println(b)
}
}
func fromPipe() []string {
ss := []string{}
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
ss = append(ss, sc.Text())
}
if sc.Err() != nil {
fmt.Println(sc.Err())
}
return ss
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment