Blob


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