gonk.git

commit 985d709bf4752260f0058c13e6e22b91f6216945

Author: Adam <git@apiote.tk>

add colours, open-in-browser, and simple links

 gonk.go | 121 +++++++++++++++++++++++++++++++++++++++++++++++++---------


diff --git a/gonk.go b/gonk.go
index 365d15638bd5c9776782889134e1cd817beb75b6..5ca078d3ae5ada0f41265fe6d08122abc4ed430c 100644
--- a/gonk.go
+++ b/gonk.go
@@ -10,10 +10,23 @@ 	"net/http"
 	"net/url"
 	"os"
 	"os/exec"
+	"regexp"
 	"sort"
 	"strings"
 )
 
+type Colour struct {
+	Reset  string
+	Red    string
+	Green  string
+	Yellow string
+	Blue   string
+	Purple string
+	Cyan   string
+	Gray   string
+	White  string
+}
+
 type Honk struct {
 	ID     int
 	What   string
@@ -29,6 +42,24 @@ }
 
 type HonkSet struct {
 	Honks []Honk
+}
+
+func setUpColour(colourful bool) Colour {
+	if colourful {
+		return Colour{
+			Reset:  "\033[0m",
+			Red:    "\033[31m",
+			Green:  "\033[32m",
+			Yellow: "\033[33m",
+			Blue:   "\033[34m",
+			Purple: "\033[35m",
+			Cyan:   "\033[36m",
+			Gray:   "\033[37m",
+			White:  "\033[97m",
+		}
+	} else {
+		return Colour{}
+	}
 }
 
 func gettoken(server, username, password string) string {
@@ -116,12 +147,81 @@ 	key := rune(b[0])
 	return key
 }
 
+func showHonks(honks HonkSet, after int, colour Colour) (int, bool) {
+	broken := false
+	honksNumber := len(honks.Honks)
+	for i, honk := range honks.Honks {
+		md := html2md.Convert(honk.Noise)
+		md = changeA(md, colour)
+		fmt.Printf(colour.White+"%d/%d\n%s %s %s (%s) \n%s\n\n"+colour.Reset, i+1, honksNumber, honk.Handle, honk.What, honk.Oondle, honk.Date, honk.XID)
+		fmt.Printf("%s\n\n", md)
+		for i, donk := range honk.Donks {
+			fmt.Printf(colour.White+"Donk #%d: %s\n\n"+colour.Reset, i, donk.URL)
+		}
+		fmt.Printf(colour.Cyan + "## Press o to open in browser, n to show next, or q to exit " + colour.Reset)
+		for {
+			key := readkey()
+			if key == 'n' {
+				after = honk.ID
+				fmt.Printf("\n\n")
+				break
+			}
+			if key == 'o' {
+				exec.Command("xdg-open", honk.XID).Run()
+				continue
+			}
+			if key == 'q' {
+				broken = true
+				return after, broken
+			}
+		}
+	}
+	return after, broken
+}
+
+func changeA(noise string, colour Colour) string {
+	reg := regexp.MustCompile("\\[([^]]+)\\]\\(([^)]+)\\)")
+	indexes := reg.FindAllStringSubmatchIndex(noise, -1)
+	last := 0
+	renoise := ""
+	for _, index := range indexes {
+		s, t, s1, t1, s2, t2 := index[0], index[1], index[2], index[3], index[4], index[5]
+		bf := noise[last:s]
+		label := noise[s1:t1]
+		href := noise[s2:t2]
+		last = t
+		if label == href {
+			renoise = renoise + bf + colour.Blue + href + colour.Reset
+		} else if label[0] == '#' {
+			renoise = renoise + bf + colour.Green + label + colour.Reset
+		} else if label[0] == '@' {
+			instance, err := url.Parse(href)
+			if err != nil {
+				renoise = renoise + bf + "[" + colour.Green + label + colour.Reset + "](" + colour.Blue + href + colour.Reset + ")"
+			} else {
+				renoise = renoise + bf + colour.Green + label + "@" + instance.Host + colour.Reset
+			}
+		} else {
+			renoise = renoise + bf + "[" + colour.Green + label + colour.Reset + "](" + colour.Blue + href + colour.Reset + ")"
+		}
+	}
+	renoise = renoise + noise[last:]
+	return renoise
+}
+
 func main() {
 	server := ""
 	username := ""
 	password := ""
 	after := 0
 	broken := false
+
+	colourful := true
+	if len(os.Args) > 1 && os.Args[1] == "-t" {
+		colourful = false
+	}
+	colour := setUpColour(colourful)
+
 	data, err := ioutil.ReadFile("account")
 	for _, line := range strings.Split(string(data), "\n") {
 		if !strings.Contains(line, "=") {
@@ -154,26 +254,9 @@ 	honks := gethonks(server, token, after)
 	sort.Slice(honks.Honks, func(i, j int) bool {
 		return honks.Honks[i].ID < honks.Honks[j].ID
 	})
-	for _, honk := range honks.Honks {
-		md := html2md.Convert(honk.Noise)
-		fmt.Printf("%s %s %s (%s) \n%s\n\n%s\n\n", honk.Handle, honk.What, honk.Oondle, honk.Date, honk.XID, md)
-		for i, donk := range honk.Donks {
-			fmt.Printf("Donk #%d: %s\n\n", i, donk.URL)
-		}
-		fmt.Printf("## Press n to show next, or q to exit ")
-		key := readkey()
-		if key == 'n' {
-			after = honk.ID
-			fmt.Printf("\n\n")
-			continue
-		}
-		if key == 'q' {
-			broken = true
-			break
-		}
-	}
+	after, broken = showHonks(honks, after, colour)
 	if !broken {
-		fmt.Println("No more honks")
+		fmt.Println(colour.Cyan + "## No more honks" + colour.Reset)
 	}
 	ioutil.WriteFile("after", []byte(fmt.Sprintf("%d", after)), 0644)
 	logout(server, token)