mirror of
https://github.com/skanehira/tson
synced 2024-11-15 12:13:04 +00:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
c6350f3337 | ||
|
4f590aae4e | ||
|
061f693a5f | ||
|
669f19fe9b | ||
|
ca51ccc045 | ||
|
41a970b963 | ||
|
b251ec1363 | ||
|
7f3874bf16 | ||
|
6e97b2dd7e | ||
|
d5a9b6c749 | ||
|
18ed987be1 | ||
|
706bb85b5c |
29
README.md
29
README.md
@ -1,3 +1,4 @@
|
||||
# This repository is no longer to develop.
|
||||
# tson
|
||||
`tson` is JSON viewer and editor written in Go.
|
||||
This tool displays JSON as a tree and you can search and edit key or values.
|
||||
@ -26,6 +27,32 @@ $ curl -X POST http://gorilla/likes/regist | tson
|
||||
$ tson -url http://gorilla/likes/json
|
||||
```
|
||||
|
||||
### Use `tson` as a library in your application
|
||||
You can use tson in your application as following.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
tson "github.com/skanehira/tson/lib"
|
||||
)
|
||||
|
||||
func main() {
|
||||
j := []byte(`{"name":"gorilla"}`)
|
||||
|
||||
// tson.Edit([]byte) will return []byte, error
|
||||
res, err := tson.Edit(j)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(string(res))
|
||||
}
|
||||
```
|
||||
|
||||
## Keybinding
|
||||
### JSON tree
|
||||
|
||||
@ -49,7 +76,7 @@ $ tson -url http://gorilla/likes/json
|
||||
| e | edit json with $EDITOR |
|
||||
| q | quit tson |
|
||||
| Enter | edit node |
|
||||
| / | search nodes |
|
||||
| / or f | search nodes |
|
||||
| ? | show helps |
|
||||
| space | expand/collaspe children nodes |
|
||||
| ctrl-j | move to next parent node |
|
||||
|
6
go.mod
6
go.mod
@ -6,8 +6,10 @@ require (
|
||||
github.com/creack/pty v1.1.9
|
||||
github.com/gdamore/tcell v1.3.0
|
||||
github.com/gofrs/uuid v3.2.0+incompatible
|
||||
github.com/lucasb-eyer/go-colorful v1.0.3 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.6 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0
|
||||
github.com/rivo/tview v0.0.0-20190324182152-8a9e26fab0ff
|
||||
github.com/rivo/uniseg v0.1.0 // indirect
|
||||
github.com/rivo/tview v0.0.0-20191129065140-82b05c9fb329
|
||||
golang.org/x/crypto v0.0.0-20191029031824-8986dd9e96cf
|
||||
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 // indirect
|
||||
)
|
||||
|
12
go.sum
12
go.sum
@ -10,14 +10,16 @@ github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4
|
||||
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
||||
github.com/lucasb-eyer/go-colorful v1.0.2 h1:mCMFu6PgSozg9tDNMMK3g18oJBX7oYGrC09mS6CXfO4=
|
||||
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
|
||||
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
|
||||
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
|
||||
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
||||
github.com/mattn/go-runewidth v0.0.6 h1:V2iyH+aX9C5fsYCpK60U8BYIvmhqxuOL3JZcqc1NB7k=
|
||||
github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/rivo/tview v0.0.0-20190324182152-8a9e26fab0ff h1:GrQgx8/nVONecTx4oGQ6O78pD8lVehvpQqeHGGPrCQM=
|
||||
github.com/rivo/tview v0.0.0-20190324182152-8a9e26fab0ff/go.mod h1:J4W+hErFfITUbyFAEXizpmkuxX7ZN56dopxHB4XQhMw=
|
||||
github.com/rivo/tview v0.0.0-20191018125527-685bf6da76c2 h1:GVXSfgXOMAeLvFH7IrpY3yYM8H3YekZEFcZ14q9gQXM=
|
||||
github.com/rivo/tview v0.0.0-20191018125527-685bf6da76c2/go.mod h1:/rBeY22VG2QprWnEqG57IBC8biVu3i0DOIjRLc9I8H0=
|
||||
github.com/rivo/tview v0.0.0-20191129065140-82b05c9fb329 h1:MubHhHJ4mB0A5wMcc2am0/51RydztIDoumyOd0r0yBw=
|
||||
github.com/rivo/tview v0.0.0-20191129065140-82b05c9fb329/go.mod h1:/rBeY22VG2QprWnEqG57IBC8biVu3i0DOIjRLc9I8H0=
|
||||
github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=
|
||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
@ -31,6 +33,8 @@ golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeo
|
||||
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191018095205-727590c5006e h1:ZtoklVMHQy6BFRHkbG6JzK+S6rX82//Yeok1vMlizfQ=
|
||||
golang.org/x/sys v0.0.0-20191018095205-727590c5006e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 h1:ZBzSG/7F4eNKz2L3GE9o300RX0Az1Bw5HF7PDraD+qU=
|
||||
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
||||
|
17
gui/gui.go
17
gui/gui.go
@ -150,6 +150,7 @@ func (g *Gui) LoadJSON() {
|
||||
log.Println(fmt.Sprintf("can't open file: %s", err))
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
i, err := UnMarshalJSON(file)
|
||||
if err != nil {
|
||||
@ -215,7 +216,7 @@ func (g *Gui) SaveJSONToFile(file string) error {
|
||||
enc := json.NewEncoder(&buf)
|
||||
enc.SetIndent("", " ")
|
||||
|
||||
if err := enc.Encode(g.makeJSON(g.Tree.GetRoot())); err != nil {
|
||||
if err := enc.Encode(g.MakeJSON(g.Tree.OriginRoot)); err != nil {
|
||||
log.Println(fmt.Sprintf("can't marshal json: %s", err))
|
||||
return err
|
||||
}
|
||||
@ -228,7 +229,7 @@ func (g *Gui) SaveJSONToFile(file string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gui) makeJSON(node *tview.TreeNode) interface{} {
|
||||
func (g *Gui) MakeJSON(node *tview.TreeNode) interface{} {
|
||||
ref := node.GetReference().(Reference)
|
||||
children := node.GetChildren()
|
||||
|
||||
@ -236,22 +237,25 @@ func (g *Gui) makeJSON(node *tview.TreeNode) interface{} {
|
||||
case Object:
|
||||
i := make(map[string]interface{})
|
||||
for _, n := range children {
|
||||
i[n.GetText()] = g.makeJSON(n)
|
||||
i[n.GetText()] = g.MakeJSON(n)
|
||||
}
|
||||
return i
|
||||
case Array:
|
||||
var i []interface{}
|
||||
for _, n := range children {
|
||||
i = append(i, g.makeJSON(n))
|
||||
i = append(i, g.MakeJSON(n))
|
||||
}
|
||||
return i
|
||||
case Key:
|
||||
if len(node.GetChildren()) == 0 {
|
||||
return ""
|
||||
}
|
||||
v := node.GetChildren()[0]
|
||||
if v.GetReference().(Reference).JSONType == Value {
|
||||
return g.parseValue(v)
|
||||
}
|
||||
return map[string]interface{}{
|
||||
node.GetText(): g.makeJSON(v),
|
||||
node.GetText(): g.MakeJSON(v),
|
||||
}
|
||||
}
|
||||
|
||||
@ -347,7 +351,6 @@ func (g *Gui) EditWithEditor() {
|
||||
return
|
||||
}
|
||||
f.Close()
|
||||
defer os.RemoveAll(f.Name())
|
||||
|
||||
if err := g.SaveJSONToFile(f.Name()); err != nil {
|
||||
log.Println(fmt.Sprintf("can't write to temp file: %s", err))
|
||||
@ -419,6 +422,7 @@ func (g *Gui) EditWithEditor() {
|
||||
g.Message(err.Error(), "main", func() {})
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
i, err := UnMarshalJSON(f)
|
||||
if err != nil {
|
||||
@ -427,6 +431,7 @@ func (g *Gui) EditWithEditor() {
|
||||
return
|
||||
}
|
||||
|
||||
os.RemoveAll(f.Name())
|
||||
g.Tree.UpdateView(g, i)
|
||||
})
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ var (
|
||||
editNodes = fmt.Sprintf(RedColor, "e", " edit json with $EDITOR")
|
||||
quitTson = fmt.Sprintf(RedColor, "q", " quit tson")
|
||||
editNodeValue = fmt.Sprintf(RedColor, "Enter", "edit current node")
|
||||
searchNodes = fmt.Sprintf(RedColor, "/", " search nodes")
|
||||
searchNodes = fmt.Sprintf(RedColor, "/ or f", " search nodes")
|
||||
toggleExpandNodes = fmt.Sprintf(RedColor, "space", " expand/collaspe nodes")
|
||||
moveNextParentNode = fmt.Sprintf(RedColor, "ctrl-j", "move to next parent node")
|
||||
movePreParentNode = fmt.Sprintf(RedColor, "ctrl-k", "move to previous parent node")
|
||||
|
32
gui/tree.go
32
gui/tree.go
@ -12,8 +12,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
moveToNext int = iota + 1
|
||||
moveToPre
|
||||
moveNext int = iota + 1
|
||||
movePre
|
||||
)
|
||||
|
||||
type Tree struct {
|
||||
@ -67,15 +67,15 @@ func (t *Tree) AddNode(node interface{}) []*tview.TreeNode {
|
||||
case []interface{}:
|
||||
for _, v := range node {
|
||||
id := uuid.Must(uuid.NewV4()).String()
|
||||
switch n := v.(type) {
|
||||
switch v.(type) {
|
||||
case map[string]interface{}:
|
||||
r := reflect.ValueOf(n)
|
||||
if r.Kind() != reflect.Slice {
|
||||
objectNode := tview.NewTreeNode("{object}").
|
||||
SetChildren(t.AddNode(v)).SetReference(Reference{ID: id, JSONType: Object})
|
||||
|
||||
nodes = append(nodes, objectNode)
|
||||
}
|
||||
objectNode := tview.NewTreeNode("{object}").
|
||||
SetChildren(t.AddNode(v)).SetReference(Reference{ID: id, JSONType: Object})
|
||||
nodes = append(nodes, objectNode)
|
||||
case []interface{}:
|
||||
arrayNode := tview.NewTreeNode("{array}").
|
||||
SetChildren(t.AddNode(v)).SetReference(Reference{ID: id, JSONType: Array})
|
||||
nodes = append(nodes, arrayNode)
|
||||
default:
|
||||
nodes = append(nodes, t.AddNode(v)...)
|
||||
}
|
||||
@ -147,7 +147,7 @@ func (t *Tree) SetKeybindings(g *Gui) {
|
||||
g.LoadJSON()
|
||||
case 's':
|
||||
g.SaveJSON()
|
||||
case '/':
|
||||
case '/', 'f':
|
||||
g.Search()
|
||||
case 'a':
|
||||
g.AddNode()
|
||||
@ -166,9 +166,9 @@ func (t *Tree) SetKeybindings(g *Gui) {
|
||||
|
||||
switch event.Key() {
|
||||
case tcell.KeyCtrlJ:
|
||||
t.moveParent(moveToNext)
|
||||
t.moveNode(moveNext)
|
||||
case tcell.KeyCtrlK:
|
||||
t.moveParent(moveToPre)
|
||||
t.moveNode(movePre)
|
||||
}
|
||||
|
||||
return event
|
||||
@ -189,19 +189,19 @@ func (t *Tree) CollapseValues(node *tview.TreeNode) {
|
||||
})
|
||||
}
|
||||
|
||||
func (t *Tree) moveParent(movement int) {
|
||||
func (t *Tree) moveNode(movement int) {
|
||||
current := t.GetCurrentNode()
|
||||
t.GetRoot().Walk(func(node, parent *tview.TreeNode) bool {
|
||||
if parent != nil {
|
||||
children := parent.GetChildren()
|
||||
for i, n := range children {
|
||||
if n.GetReference().(Reference).ID == current.GetReference().(Reference).ID {
|
||||
if movement == moveToNext {
|
||||
if movement == moveNext {
|
||||
if i < len(children)-1 {
|
||||
t.SetCurrentNode(children[i+1])
|
||||
return false
|
||||
}
|
||||
} else if movement == moveToPre {
|
||||
} else if movement == movePre {
|
||||
if i > 0 {
|
||||
t.SetCurrentNode(children[i-1])
|
||||
return false
|
||||
|
28
lib/edit.go
Normal file
28
lib/edit.go
Normal file
@ -0,0 +1,28 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
"github.com/skanehira/tson/gui"
|
||||
)
|
||||
|
||||
// Edit use tson as a library
|
||||
func Edit(b []byte) ([]byte, error) {
|
||||
// dont output log
|
||||
log.SetOutput(ioutil.Discard)
|
||||
|
||||
var i interface{}
|
||||
if err := json.Unmarshal(b, &i); err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
g := gui.New()
|
||||
if err := g.Run(i); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return json.Marshal(g.MakeJSON(g.Tree.GetRoot()))
|
||||
}
|
Loading…
Reference in New Issue
Block a user