Skip to content

Commit

Permalink
Merge pull request #51 from simeji/v0.7.0
Browse files Browse the repository at this point in the history
V0.7.0
  • Loading branch information
simeji authored Jan 4, 2017
2 parents e0b9167 + a245252 commit 82654b8
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 113 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ You can drill down JSON interactively by using filtering queries like [jq](https

## Demo

![demo-jid-main](https://github.com/simeji/jid/wiki/images/demo-jid-main-640.gif)
![demo-jid-main](https://github.com/simeji/jid/wiki/images/demo-jid-main-640-colorize.gif)

## Installation

Expand Down Expand Up @@ -112,6 +112,11 @@ jid < file.json

### Option

First argument: Initial query

-q : Print query (for jq)
|option|description|
|:-----------|:----------|
|First argument ($1) | Initial query|
|-h | print a help|
|-help | print a help|
|-version | print the version and exit|
|-q | Output query mode (for jq)|
|-M | monochrome output mode|
9 changes: 8 additions & 1 deletion cmd/jid/jid.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ func main() {
var qm bool
var help bool
var version bool
var mono bool
qs := "."

flag.BoolVar(&qm, "q", false, "Output query mode")
flag.BoolVar(&help, "h", false, "print a help")
flag.BoolVar(&help, "help", false, "print a help")
flag.BoolVar(&version, "version", false, "print the version and exit")
flag.BoolVar(&mono, "M", false, "monochrome output mode")
flag.Parse()

if help {
Expand All @@ -38,7 +40,12 @@ func main() {
qs = args[0]
}

e, err := jid.NewEngine(content, qs)
ea := &jid.EngineAttribute{
DefaultQuery: qs,
Monochrome: mono,
}

e, err := jid.NewEngine(content, ea)

if err != nil {
fmt.Println(err)
Expand Down
88 changes: 46 additions & 42 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,42 @@ type EngineResultInterface interface {
}

type Engine struct {
manager *JsonManager
query QueryInterface
term *Terminal
complete []string
keymode bool
candidates []string
candidatemode bool
candidateidx int
contentOffset int
queryConfirm bool
cursorOffsetX int
manager *JsonManager
query QueryInterface
queryCursorIdx int
term *Terminal
complete []string
keymode bool
candidates []string
candidatemode bool
candidateidx int
contentOffset int
queryConfirm bool
}

func NewEngine(s io.Reader, qs string) (EngineInterface, error) {
type EngineAttribute struct {
DefaultQuery string
Monochrome bool
}

func NewEngine(s io.Reader, ea *EngineAttribute) (EngineInterface, error) {
j, err := NewJsonManager(s)
if err != nil {
return nil, err
}
e := &Engine{
manager: j,
term: NewTerminal(FilterPrompt, DefaultY),
query: NewQuery([]rune(qs)),
term: NewTerminal(FilterPrompt, DefaultY, ea.Monochrome),
query: NewQuery([]rune(ea.DefaultQuery)),
complete: []string{"", ""},
keymode: false,
candidates: []string{},
candidatemode: false,
candidateidx: 0,
contentOffset: 0,
queryConfirm: false,
cursorOffsetX: 0,
}
e.cursorOffsetX = len(e.query.Get())
e.queryCursorIdx = e.query.Length()
return e, nil
}

Expand Down Expand Up @@ -94,7 +98,7 @@ func (e *Engine) Run() EngineResultInterface {

if e.query.StringGet() == "" {
e.query.StringSet(".")
e.cursorOffsetX = len(e.query.StringGet())
e.queryCursorIdx = e.query.Length()
}

contents = e.getContents()
Expand All @@ -103,15 +107,17 @@ func (e *Engine) Run() EngineResultInterface {

ta := &TerminalDrawAttributes{
Query: e.query.StringGet(),
CursorOffsetX: e.cursorOffsetX,
Contents: contents,
CandidateIndex: e.candidateidx,
ContentsOffsetY: e.contentOffset,
Complete: e.complete[0],
Candidates: e.candidates,
CursorOffset: e.query.IndexOffset(e.queryCursorIdx),
}
err = e.term.Draw(ta)
if err != nil {
panic(err)
}

e.term.draw(ta)

switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
Expand Down Expand Up @@ -193,21 +199,22 @@ func (e *Engine) setCandidateData() {
func (e *Engine) confirmCandidate() {
_, _ = e.query.PopKeyword()
_ = e.query.StringAdd(".")
q := e.query.StringAdd(e.candidates[e.candidateidx])
e.cursorOffsetX = len(q)
_ = e.query.StringAdd(e.candidates[e.candidateidx])
e.queryCursorIdx = e.query.Length()
e.queryConfirm = true
}

func (e *Engine) deleteChar() {
if e.cursorOffsetX > 0 {
_ = e.query.Delete(e.cursorOffsetX - 1)
e.cursorOffsetX -= 1
if i := e.queryCursorIdx - 1; i > 0 {
_ = e.query.Delete(i)
e.queryCursorIdx--
}

}

func (e *Engine) deleteLineQuery() {
_ = e.query.StringSet("")
e.cursorOffsetX = 0
e.queryCursorIdx = 0
}

func (e *Engine) scrollToBelow() {
Expand All @@ -225,14 +232,12 @@ func (e *Engine) deleteWordBackward() {
if k, _ := e.query.StringPopKeyword(); k != "" && !strings.Contains(k, "[") {
_ = e.query.StringAdd(".")
}
e.cursorOffsetX = len(e.query.Get())
e.queryCursorIdx = e.query.Length()
}
func (e *Engine) tabAction() {
if !e.candidatemode {
e.candidatemode = true
if e.query.StringGet() == "" {
_ = e.query.StringAdd(".")
} else if e.complete[0] != e.complete[1] && e.complete[0] != "" {
if e.complete[0] != e.complete[1] && e.complete[0] != "" {
if k, _ := e.query.StringPopKeyword(); !strings.Contains(k, "[") {
_ = e.query.StringAdd(".")
}
Expand All @@ -243,36 +248,35 @@ func (e *Engine) tabAction() {
} else {
e.candidateidx = e.candidateidx + 1
}
e.cursorOffsetX = len(e.query.Get())
e.queryCursorIdx = e.query.Length()
}
func (e *Engine) escapeCandidateMode() {
e.candidatemode = false
}
func (e *Engine) inputChar(ch rune) {
b := len(e.query.Get())
q := e.query.StringInsert(string(ch), e.cursorOffsetX)
if b < len(q) {
e.cursorOffsetX += 1
}
_ = e.query.Insert([]rune{ch}, e.queryCursorIdx)
e.queryCursorIdx++
}

func (e *Engine) moveCursorBackward() {
if e.cursorOffsetX > 0 {
e.cursorOffsetX -= 1
if i := e.queryCursorIdx - 1; i >= 0 {
e.queryCursorIdx--
}
}

func (e *Engine) moveCursorForward() {
if len(e.query.Get()) > e.cursorOffsetX {
e.cursorOffsetX += 1
if e.query.Length() > e.queryCursorIdx {
e.queryCursorIdx++
}
}

func (e *Engine) moveCursorWordBackwark() {
}
func (e *Engine) moveCursorWordForward() {
}
func (e *Engine) moveCursorToTop() {
e.cursorOffsetX = 0
e.queryCursorIdx = 0
}
func (e *Engine) moveCursorToEnd() {
e.cursorOffsetX = len(e.query.Get())
e.queryCursorIdx = e.query.Length()
}
Loading

0 comments on commit 82654b8

Please sign in to comment.