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], slice...) } else { slice = append(all[ind-ctxlen:ind], slice...) } if len(all[ind:]) < ctxlen { slice = append(slice, all[ind+1:]...) } else { slice = append(slice, all[ind+1:ind+ctxlen]...) } 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, 1, 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() }