diff options
-rw-r--r-- | gwic.go | 70 |
1 files changed, 70 insertions, 0 deletions
@@ -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() +} |