Last active
July 23, 2023 11:38
-
-
Save AWtnb/9a2ec3151ae44389e2b8c863b2d1564d to your computer and use it in GitHub Desktop.
fuzzy-filter for PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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