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