1package main
2
3import (
4	"flag"
5	"fmt"
6	"os"
7
8	"github.com/simeji/jid"
9)
10
11const VERSION = "0.7.6"
12
13func main() {
14	content := os.Stdin
15
16	var qm bool
17	var help bool
18	var version bool
19	var mono bool
20	var pretty bool
21	qs := "."
22
23	flag.BoolVar(&qm, "q", false, "Output query mode")
24	flag.BoolVar(&help, "h", false, "print a help")
25	flag.BoolVar(&help, "help", false, "print a help")
26	flag.BoolVar(&version, "version", false, "print the version and exit")
27	flag.BoolVar(&mono, "M", false, "monochrome output mode")
28	flag.BoolVar(&pretty, "p", false, "pretty print json result")
29	flag.Parse()
30
31	if help {
32		flag.Usage()
33		fmt.Println(getHelpString())
34		os.Exit(0)
35	}
36
37	if version {
38		fmt.Println(fmt.Sprintf("jid version v%s", VERSION))
39		os.Exit(0)
40	}
41	args := flag.Args()
42	if len(args) > 0 {
43		qs = args[0]
44	}
45
46	ea := &jid.EngineAttribute{
47		DefaultQuery: qs,
48		Monochrome:   mono,
49		PrettyResult: pretty,
50	}
51
52	e, err := jid.NewEngine(content, ea)
53
54	if err != nil {
55		fmt.Println(err)
56		os.Exit(1)
57	}
58	os.Exit(run(e, qm))
59}
60
61func run(e jid.EngineInterface, qm bool) int {
62
63	result := e.Run()
64	if result.GetError() != nil {
65		return 2
66	}
67	if qm {
68		fmt.Printf("%s", result.GetQueryString())
69	} else {
70		fmt.Printf("%s", result.GetContent())
71	}
72	return 0
73}
74
75func getHelpString() string {
76	return `
77
78============ Load JSON from a file ==============
79
80$ jid < file.json
81
82============ With a JSON filter mode =============
83
84TAB / CTRL-I
85  Show available items and choice them
86
87CTRL-W
88  Delete from the cursor to the start of the word
89
90CTRL-U
91  Delete whole query
92
93CTRL-F / Right Arrow
94  Move cursor a character to the right
95
96CTRL-B / Left Arrow
97  Move cursor a character to the left
98
99CTRL-A
100  To the first character of the 'Filter'
101
102CTRL-E
103  To the end of the 'Filter'
104
105CTRL-J
106  Scroll json buffer 1 line downwards
107
108CTRL-K
109  Scroll json buffer 1 line upwards
110
111CTRL-G
112  Scroll json buffer to bottom
113
114CTRL-T
115  Scroll json buffer to top
116
117CTRL-N
118  Scroll json buffer 'Page Down'
119
120CTRL-P
121  Scroll json buffer 'Page Up'
122
123CTRL-L
124  Change view mode whole json or keys (only object)
125
126ESC
127  Hide a candidate box
128
129`
130}
131