Blame


1 4f7fe71a 2020-08-02 op // acmetag is a tool to programmatically interact with acme(1) tag
2 4f7fe71a 2020-08-02 op // bar. It provides two flag:
3 4f7fe71a 2020-08-02 op //
4 4f7fe71a 2020-08-02 op // * -g prints the tag content (mnemonic: get)
5 4f7fe71a 2020-08-02 op // * -c clears the tag content (mnemonic: clear)
6 4f7fe71a 2020-08-02 op //
7 4f7fe71a 2020-08-02 op // Any other argument (if passed) will be appended to the tag bar.
8 4f7fe71a 2020-08-02 op //
9 4f7fe71a 2020-08-02 op // Of course, you can combine the flags:
10 4f7fe71a 2020-08-02 op //
11 4f7fe71a 2020-08-02 op // acmetag -g -c fmt
12 4f7fe71a 2020-08-02 op //
13 4f7fe71a 2020-08-02 op // BUG(op) it cannot change the text before the | character.
14 4f7fe71a 2020-08-02 op // AFAIK that's not possible
15 14b3f96d 2019-10-26 op package main
16 14b3f96d 2019-10-26 op
17 14b3f96d 2019-10-26 op import (
18 14b3f96d 2019-10-26 op "flag"
19 14b3f96d 2019-10-26 op "fmt"
20 6c2d529b 2019-10-26 op "log"
21 14b3f96d 2019-10-26 op "os"
22 14b3f96d 2019-10-26 op "strconv"
23 14b3f96d 2019-10-26 op
24 14b3f96d 2019-10-26 op "9fans.net/go/acme"
25 14b3f96d 2019-10-26 op )
26 14b3f96d 2019-10-26 op
27 14b3f96d 2019-10-26 op var (
28 14b3f96d 2019-10-26 op cl = flag.Bool("c", false, `Clear the tag`)
29 6c2d529b 2019-10-26 op gt = flag.Bool("g", false, `Get the content of the tag`)
30 14b3f96d 2019-10-26 op )
31 14b3f96d 2019-10-26 op
32 14b3f96d 2019-10-26 op func open() (*acme.Win, error) {
33 14b3f96d 2019-10-26 op winid := os.Getenv("winid")
34 14b3f96d 2019-10-26 op id, err := strconv.Atoi(winid)
35 14b3f96d 2019-10-26 op if err != nil {
36 14b3f96d 2019-10-26 op return nil, err
37 14b3f96d 2019-10-26 op }
38 14b3f96d 2019-10-26 op win, err := acme.Open(id, nil)
39 14b3f96d 2019-10-26 op return win, err
40 14b3f96d 2019-10-26 op }
41 14b3f96d 2019-10-26 op
42 14b3f96d 2019-10-26 op func usage() {
43 14b3f96d 2019-10-26 op me := os.Args[0]
44 14b3f96d 2019-10-26 op fmt.Println(me, "- manage acme(1) tag")
45 8e6a76fc 2020-06-10 op fmt.Println("Usage:", me, " [-cg] [entries...]")
46 8e6a76fc 2020-06-10 op fmt.Println(" where entries are words to be added to the acme tag bar")
47 14b3f96d 2019-10-26 op flag.PrintDefaults()
48 14b3f96d 2019-10-26 op os.Exit(1)
49 14b3f96d 2019-10-26 op }
50 14b3f96d 2019-10-26 op
51 14b3f96d 2019-10-26 op func main() {
52 14b3f96d 2019-10-26 op flag.Usage = usage
53 14b3f96d 2019-10-26 op flag.Parse()
54 14b3f96d 2019-10-26 op
55 14b3f96d 2019-10-26 op win, err := open()
56 14b3f96d 2019-10-26 op if err != nil {
57 14b3f96d 2019-10-26 op os.Exit(1)
58 14b3f96d 2019-10-26 op }
59 14b3f96d 2019-10-26 op defer win.CloseFiles()
60 14b3f96d 2019-10-26 op
61 6c2d529b 2019-10-26 op if *gt {
62 6c2d529b 2019-10-26 op tag, err := win.ReadAll("tag")
63 6c2d529b 2019-10-26 op if err != nil {
64 6c2d529b 2019-10-26 op log.Fatalln(err)
65 6c2d529b 2019-10-26 op }
66 6c2d529b 2019-10-26 op fmt.Println(string(tag))
67 6c2d529b 2019-10-26 op }
68 6c2d529b 2019-10-26 op
69 14b3f96d 2019-10-26 op if *cl {
70 14b3f96d 2019-10-26 op win.Ctl("cleartag")
71 14b3f96d 2019-10-26 op }
72 14b3f96d 2019-10-26 op
73 14b3f96d 2019-10-26 op sep := ""
74 14b3f96d 2019-10-26 op for _, arg := range flag.Args() {
75 14b3f96d 2019-10-26 op _, err = win.Write("tag", []byte(sep+arg))
76 14b3f96d 2019-10-26 op sep = " "
77 14b3f96d 2019-10-26 op }
78 14b3f96d 2019-10-26 op }