summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvan Jegen <s.jegen@gmail.com>2015-08-21 19:56:35 +0200
committerSilvan Jegen <s.jegen@gmail.com>2015-08-21 19:56:35 +0200
commitf1b5b9da5ae2a81eb7b4be95a2faca07beacfd78 (patch)
treecaf03bed967afa2040153a7ffb5cda971a5f96bd
Initial commit
-rw-r--r--gwic.go70
1 files changed, 70 insertions, 0 deletions
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()
+}