Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: paulhammond/jp
base: 3e2fa08b06
...
head fork: paulhammond/jp
compare: cc185ab111
  • 7 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Showing with 124 additions and 43 deletions.
  1. +2 −2 LICENSE.txt
  2. +78 −36 jp.go
  3. +11 −2 jp/main.go
  4. +9 −3 jp_test.go
  5. +24 −0 package.sh
View
4 LICENSE.txt
@@ -1,4 +1,4 @@
-Copyright (c) 2013 Paul Hammond
+Copyright (c) 2013-2014 Paul Hammond
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
+SOFTWARE.
View
114 jp.go
@@ -1,4 +1,4 @@
-// Copyright 2013 Paul Hammond.
+// Copyright 2013-2014 Paul Hammond.
// This software is licensed under the MIT license, see LICENSE.txt for details.
package jp
@@ -12,48 +12,56 @@ import (
)
type dict struct {
- indented string
- objEmpty string
- objOpen string
- objClose string
- arrEmpty string
- arrOpen string
- arrClose string
- colon string
- comma string
- strOpen string
- strClose string
- end string
+ indented string
+ objEmpty string
+ objOpen string
+ objClose string
+ arrEmpty string
+ arrOpen string
+ arrClose string
+ colon string
+ comma string
+ strOpen string
+ strClose string
+ otherSpace string
+ otherOpen string
+ otherClose string
+ end string
}
var dicts = map[string]dict{
- "pretty": {"\n ", "{ }", "{\n", "\n}", "[ ]", "[\n", "\n]", ": ", ",\n", `"`, `"`, "\n"},
- "compact": {"\n", "{}", "{", "}", "[]", "[", "]", ":", ",", `"`, `"`, ""},
+ "pretty16": {"\n ", "\033[0;32m{ }", "\033[0;32m{\n", "\033[0;32m\n}", "\033[0;32m[ ]", "\033[0;32m[\n", "\033[0;32m\n]", "\033[0;32m: ", "\033[0;32m,\n", "\033[0m\"\033[1m", "\033[0m\"", "\033[0;41m \033[0m\033[1;33m", "\033[1;33m", "", "\n"},
+ "compact16": {"\n", "\033[0;32m{}", "\033[0;32m{", "\033[0;32m}", "\033[0;32m[]", "\033[0;32m[", "\033[0;32m]", "\033[0;32m:", "\033[0;32m,", "\033[0m\"\033[1m", "\033[0m\"", "\033[0;41m \033[0m\033[1;33m", "\033[1;33m", "", ""},
+ "pretty": {"\n ", "{ }", "{\n", "\n}", "[ ]", "[\n", "\n]", ": ", ",\n", `"`, `"`, " ", "", "", "\n"},
+ "compact": {"\n", "{}", "{", "}", "[]", "[", "]", ":", ",", `"`, `"`, " ", "", "", ""},
}
func (d dict) indent() dict {
return dict{
d.indented,
strings.Replace(d.objEmpty, "\n", d.indented, 1),
- strings.Replace(d.objOpen , "\n", d.indented, 1),
+ strings.Replace(d.objOpen, "\n", d.indented, 1),
strings.Replace(d.objClose, "\n", d.indented, 1),
strings.Replace(d.arrEmpty, "\n", d.indented, 1),
- strings.Replace(d.arrOpen , "\n", d.indented, 1),
+ strings.Replace(d.arrOpen, "\n", d.indented, 1),
strings.Replace(d.arrClose, "\n", d.indented, 1),
- strings.Replace(d.colon , "\n", d.indented, 1),
- strings.Replace(d.comma , "\n", d.indented, 1),
- strings.Replace(d.strOpen , "\n", d.indented, 1),
+ strings.Replace(d.colon, "\n", d.indented, 1),
+ strings.Replace(d.comma, "\n", d.indented, 1),
+ strings.Replace(d.strOpen, "\n", d.indented, 1),
strings.Replace(d.strClose, "\n", d.indented, 1),
+ d.otherSpace,
+ strings.Replace(d.otherOpen, "\n", d.indented, 1),
+ strings.Replace(d.otherClose, "\n", d.indented, 1),
d.end,
}
}
type scanner struct {
- r *bufio.Reader
- w *bufio.Writer
- indentSize int
+ r *bufio.Reader
+ w *bufio.Writer
+ indentSize int
indentDicts []dict
- dict *dict
+ dict *dict
}
func (s scanner) writeRune(r rune) (e error) {
@@ -74,6 +82,13 @@ func (s *scanner) indent(d int) {
s.dict = &s.indentDicts[s.indentSize]
}
+func (s scanner) unread() {
+ e := s.r.UnreadRune()
+ if e != nil {
+ panic(e.Error()) // we only ever read runes, so this shouldn't happen
+ }
+}
+
func (s scanner) read() (r rune, e error) {
for e == nil {
r, _, e = s.r.ReadRune()
@@ -108,6 +123,38 @@ func (s scanner) copyString() (e error) {
return e
}
+func (s scanner) copyOther() (e error) {
+ var r rune
+ var space bool
+ e = s.writeString(s.dict.otherOpen)
+ for e == nil {
+ r, _, e = s.r.ReadRune()
+ if e != nil {
+ break
+ }
+ switch r {
+ case '{', '}', '[', ']', ',', ':', '"':
+ s.unread()
+ e = s.writeString(s.dict.otherClose)
+ return e
+ default:
+ if unicode.IsSpace(r) {
+ space = true
+ break
+ }
+ if space {
+ space = false
+ e = s.writeString(s.dict.otherSpace)
+ if e != nil {
+ return e
+ }
+ }
+ e = s.writeRune(r)
+ }
+ }
+ return e
+}
+
func (s scanner) expand() (e error) {
var r rune
for e == nil {
@@ -124,10 +171,7 @@ func (s scanner) expand() (e error) {
if r == '}' {
e = s.writeString(s.dict.objEmpty)
} else {
- e = s.r.UnreadRune()
- if e != nil {
- break // this really shouldn't happen
- }
+ s.unread()
s.indent(1)
e = s.writeString(s.dict.objOpen)
}
@@ -142,10 +186,7 @@ func (s scanner) expand() (e error) {
if r == ']' {
e = s.writeString(s.dict.arrEmpty)
} else {
- e = s.r.UnreadRune()
- if e != nil {
- break // this really shouldn't happen
- }
+ s.unread()
s.indent(1)
e = s.writeString(s.dict.arrOpen)
}
@@ -160,7 +201,8 @@ func (s scanner) expand() (e error) {
e = s.copyString()
// todo unicode.ReplacementChar
default:
- e = s.writeRune(r)
+ s.unread()
+ e = s.copyOther()
}
}
s.writeString(s.dict.end)
@@ -178,10 +220,10 @@ func Expand(reader io.Reader, writer io.Writer, format string) error {
}
indentDicts := []dict{d}
s := &scanner{
- r: bufio.NewReader(reader),
- w: bufio.NewWriter(writer),
+ r: bufio.NewReader(reader),
+ w: bufio.NewWriter(writer),
indentDicts: indentDicts,
- dict: &indentDicts[0],
+ dict: &indentDicts[0],
}
return s.expand()
}
View
13 jp/main.go
@@ -1,4 +1,4 @@
-// Copyright 2013 Paul Hammond.
+// Copyright 2013-2014 Paul Hammond.
// This software is licensed under the MIT license, see LICENSE.txt for details.
package main
@@ -6,8 +6,10 @@ package main
import (
"flag"
"fmt"
- "github.com/paulhammond/jp"
"os"
+
+ "code.google.com/p/go.crypto/ssh/terminal"
+ "github.com/paulhammond/jp"
)
func main() {
@@ -15,7 +17,11 @@ func main() {
fmt.Fprintf(os.Stderr, "usage: jp [file]\n")
flag.PrintDefaults()
}
+
+ isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
+
compact := flag.Bool("compact", false, "compact format")
+ colors := flag.Bool("color", isTerminal, "colored format")
flag.Parse()
args := flag.Args()
@@ -28,6 +34,9 @@ func main() {
if *compact {
format = "compact"
}
+ if *colors {
+ format += "16"
+ }
var fd *os.File
var e error
View
12 jp_test.go
@@ -1,12 +1,12 @@
-// Copyright 2013 Paul Hammond.
+// Copyright 2013-2014 Paul Hammond.
// This software is licensed under the MIT license, see LICENSE.txt for details.
package jp
import (
"bytes"
- "os"
"fmt"
+ "os"
"strings"
"testing"
)
@@ -45,9 +45,15 @@ func TestExpand(t *testing.T) {
{extraspaces, pretty, "pretty"},
{pretty, pretty, "pretty"},
{compact, compact, "compact"},
- // this checks for an edge cases in strings
+ // this checks for an edge case in strings
{`{"slash\\" : "foo" }`, `{"slash\\":"foo"}`, "compact"},
{`{"" : "foo" }`, `{"":"foo"}`, "compact"},
+ // check invalid spaces between elements are preserved
+ {`{"foo":[1 2, 3]}`, `{"foo":[1 2,3]}`, "compact"},
+ {`{"foo":[1 true, 3]}`, `{"foo":[1 true,3]}`, "compact"},
+ {`{"foo":[true false, 3]}`, `{"foo":[true false,3]}`, "compact"},
+ {`{"foo":[true false]}`, `{"foo":[true false]}`, "compact"},
+ {`{"foo":[true {}]}`, `{"foo":[true{}]}`, "compact"},
}
for _, test := range tests {
r := strings.NewReader(test.in)
View
24 package.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+# To get this script working you need go set up go to do cross compilation.
+# . for mac/homebrew, run "brew install go --cross-compile-common"
+# . on linux install from source then run something like:
+# GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 ./make.bash --no-clean
+# GOOS=linux GOARCH=386 CGO_ENABLED=0 ./make.bash --no-clean
+
+set -eu
+
+VERSION=${1:-"dev"}
+
+for DEST in linux-386 linux-amd64 darwin-amd64; do
+ OS=${DEST%-*}
+ ARCH=${DEST#*-}
+ DIR=pkg/$DEST/jp-$VERSION
+ mkdir -p $DIR
+ cp README.md $DIR
+ cp LICENSE.txt $DIR
+ GOOS=$OS GOARCH=$ARCH go build -o $DIR/jp github.com/paulhammond/jp/jp
+ cd pkg/$DEST
+ tar -czf ../jp-${VERSION}-${OS}-${ARCH}.tgz jp-$VERSION
+ cd ../..
+done

No commit comments for this range

Something went wrong with that request. Please try again.