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 14b3f96d 2019-10-26 op package main
13 14b3f96d 2019-10-26 op
14 b4e3e59c 2020-08-02 op // BUG(op) it cannot change the text before the | character.
15 b4e3e59c 2020-08-02 op
16 14b3f96d 2019-10-26 op import (
17 14b3f96d 2019-10-26 op "flag"
18 14b3f96d 2019-10-26 op "fmt"
19 6c2d529b 2019-10-26 op "log"
20 14b3f96d 2019-10-26 op "os"
21 14b3f96d 2019-10-26 op "strconv"
22 14b3f96d 2019-10-26 op
23 14b3f96d 2019-10-26 op "9fans.net/go/acme"
24 14b3f96d 2019-10-26 op )
25 14b3f96d 2019-10-26 op
26 14b3f96d 2019-10-26 op var (
27 14b3f96d 2019-10-26 op cl = flag.Bool("c", false, `Clear the tag`)
28 6c2d529b 2019-10-26 op gt = flag.Bool("g", false, `Get the content of the tag`)
29 14b3f96d 2019-10-26 op )
30 14b3f96d 2019-10-26 op
31 14b3f96d 2019-10-26 op func open() (*acme.Win, error) {
32 14b3f96d 2019-10-26 op winid := os.Getenv("winid")
33 14b3f96d 2019-10-26 op id, err := strconv.Atoi(winid)
34 14b3f96d 2019-10-26 op if err != nil {
35 14b3f96d 2019-10-26 op return nil, err
36 14b3f96d 2019-10-26 op }
37 14b3f96d 2019-10-26 op win, err := acme.Open(id, nil)
38 14b3f96d 2019-10-26 op return win, err
39 14b3f96d 2019-10-26 op }
40 14b3f96d 2019-10-26 op
41 14b3f96d 2019-10-26 op func usage() {
42 14b3f96d 2019-10-26 op me := os.Args[0]
43 14b3f96d 2019-10-26 op fmt.Println(me, "- manage acme(1) tag")
44 4dd5aafa 2022-11-27 op fmt.Println("Usage:", me, " [-c] [-g] [entries...]")
45 8e6a76fc 2020-06-10 op fmt.Println(" where entries are words to be added to the acme tag bar")
46 14b3f96d 2019-10-26 op flag.PrintDefaults()
47 14b3f96d 2019-10-26 op os.Exit(1)
48 14b3f96d 2019-10-26 op }
49 14b3f96d 2019-10-26 op
50 14b3f96d 2019-10-26 op func main() {
51 14b3f96d 2019-10-26 op flag.Usage = usage
52 14b3f96d 2019-10-26 op flag.Parse()
53 14b3f96d 2019-10-26 op
54 14b3f96d 2019-10-26 op win, err := open()
55 14b3f96d 2019-10-26 op if err != nil {
56 14b3f96d 2019-10-26 op os.Exit(1)
57 14b3f96d 2019-10-26 op }
58 14b3f96d 2019-10-26 op defer win.CloseFiles()
59 14b3f96d 2019-10-26 op
60 6c2d529b 2019-10-26 op if *gt {
61 6c2d529b 2019-10-26 op tag, err := win.ReadAll("tag")
62 6c2d529b 2019-10-26 op if err != nil {
63 6c2d529b 2019-10-26 op log.Fatalln(err)
64 6c2d529b 2019-10-26 op }
65 6c2d529b 2019-10-26 op fmt.Println(string(tag))
66 6c2d529b 2019-10-26 op }
67 6c2d529b 2019-10-26 op
68 14b3f96d 2019-10-26 op if *cl {
69 14b3f96d 2019-10-26 op win.Ctl("cleartag")
70 14b3f96d 2019-10-26 op }
71 14b3f96d 2019-10-26 op
72 14b3f96d 2019-10-26 op sep := ""
73 14b3f96d 2019-10-26 op for _, arg := range flag.Args() {
74 14b3f96d 2019-10-26 op _, err = win.Write("tag", []byte(sep+arg))
75 14b3f96d 2019-10-26 op sep = " "
76 14b3f96d 2019-10-26 op }
77 14b3f96d 2019-10-26 op }