From f1b5b9da5ae2a81eb7b4be95a2faca07beacfd78 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Fri, 21 Aug 2015 19:56:35 +0200 Subject: Initial commit --- gwic.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 gwic.go diff --git a/gwic.go b/gwic.go new file mode 100644 index 0000000..07f9d1a --- /dev/null +++ b/gwic.go @@ -0,0 +1,70 @@ +package main + +import ( + "bufio" + "flag" + "fmt" + "io/ioutil" + "os" + "regexp" + "strings" + "text/tabwriter" +) + +type queue []string + +func (q *queue) insert(s string) { +} + +func printWithContext(all []string, ind, ctxlen int) { + var slice []string + slice = append(slice, "\""+all[ind]+"\"") + + if len(all[:ind]) < ctxlen { + slice = append(all[:ind-1], slice...) + } else { + slice = append(all[ind-ctxlen:ind-1], slice...) + } + + if len(all[ind:]) < ctxlen { + slice = append(slice, all[ind+1:]...) + } else { + slice = append(slice, all[ind+1:ind+ctxlen]...) + } + + //for _, w := range slice { + //fmt.Printf("%q ", w) + //} + fstr := strings.Join(slice, "\t") + fmt.Fprintf(tw, "%s\n", fstr) +} + +var tw *tabwriter.Writer + +func main() { + flag.Parse() + kw := flag.Arg(0) + if len(kw) == 0 { + fmt.Printf("Need string to search for. Exiting...\n") + os.Exit(1) + } + tw = tabwriter.NewWriter(os.Stdout, 1, 2, 1, ' ', 0) + + reader := bufio.NewReader(os.Stdin) + all, err := ioutil.ReadAll(reader) + if err != nil { + fmt.Printf("Could not read everything because there was an error: %q. Exiting\n", err) + os.Exit(1) + } + + splitreg := regexp.MustCompile("[ \t\n]") + splline := splitreg.Split(string(all), -1) + for i, w := range splline { + index := strings.Index(w, kw) + if index < 0 { + continue + } + printWithContext(splline, i, 5) + } + tw.Flush() +} -- cgit v1.2.3