mirror of
				https://github.com/TomWright/dasel.git
				synced 2022-05-22 02:32:45 +03:00 
			
		
		
		
	Add some colourise funcs within the storage package
This commit is contained in:
		
							
								
								
									
										25
									
								
								internal/storage/colourise.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								internal/storage/colourise.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| package storage | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"github.com/alecthomas/chroma/quick" | ||||
| ) | ||||
|  | ||||
| // ColouriseStyle is the style used when colourising output. | ||||
| const ColouriseStyle = "solarized-dark256" | ||||
|  | ||||
| // ColouriseFormatter is the formatter used when colourising output. | ||||
| const ColouriseFormatter = "terminal" | ||||
|  | ||||
| // ColouriseBuffer colourises the given buffer in-place. | ||||
| func ColouriseBuffer(content *bytes.Buffer, lexer string) error { | ||||
| 	contentString := content.String() | ||||
| 	content.Reset() | ||||
| 	return quick.Highlight(content, contentString, lexer, ColouriseFormatter, ColouriseStyle) | ||||
| } | ||||
|  | ||||
| // Colourise colourises the given string. | ||||
| func Colourise(content string, lexer string) (*bytes.Buffer, error) { | ||||
| 	buf := new(bytes.Buffer) | ||||
| 	return buf, quick.Highlight(buf, content, lexer, ColouriseFormatter, ColouriseStyle) | ||||
| } | ||||
| @@ -1,7 +0,0 @@ | ||||
| package storage | ||||
|  | ||||
| // ColouriseStyle is the style used when colourising output. | ||||
| const ColouriseStyle = "solarized-dark256" | ||||
|  | ||||
| // ColouriseFormatter is the formatter used when colourising output. | ||||
| const ColouriseFormatter = "terminal" | ||||
| @@ -4,7 +4,6 @@ import ( | ||||
| 	"bytes" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"github.com/alecthomas/chroma/quick" | ||||
| 	"io" | ||||
| ) | ||||
|  | ||||
| @@ -93,10 +92,8 @@ func (p *JSONParser) ToBytes(value interface{}, options ...ReadWriteOption) ([]b | ||||
| 	} | ||||
|  | ||||
| 	if colourise { | ||||
| 		source := buffer.String() | ||||
| 		buffer.Reset() | ||||
| 		if err := quick.Highlight(buffer, source, "json", ColouriseFormatter, ColouriseStyle); err != nil { | ||||
| 			return nil, fmt.Errorf("could not colourise json output: %w", err) | ||||
| 		if err := ColouriseBuffer(buffer, "json"); err != nil { | ||||
| 			return nil, fmt.Errorf("could not colourise output: %w", err) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1,8 +1,6 @@ | ||||
| package storage_test | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"github.com/alecthomas/chroma/quick" | ||||
| 	"github.com/tomwright/dasel/internal/storage" | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
| @@ -117,12 +115,11 @@ func TestJSONParser_ToBytes(t *testing.T) { | ||||
| 			t.Errorf("unexpected error: %s", err) | ||||
| 			return | ||||
| 		} | ||||
| 		buf := new(bytes.Buffer) | ||||
| 		_ = quick.Highlight(buf, `{ | ||||
| 		expBuf, _ := storage.Colourise(`{ | ||||
|   "name": "Tom" | ||||
| } | ||||
| `, "json", storage.ColouriseFormatter, storage.ColouriseStyle) | ||||
| 		exp := buf.Bytes() | ||||
| `, "json") | ||||
| 		exp := expBuf.Bytes() | ||||
| 		if !reflect.DeepEqual(exp, got) { | ||||
| 			t.Errorf("expected %v, got %v", exp, got) | ||||
| 		} | ||||
|   | ||||
| @@ -3,7 +3,6 @@ package storage | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 	"github.com/alecthomas/chroma/quick" | ||||
| 	"github.com/pelletier/go-toml" | ||||
| ) | ||||
|  | ||||
| @@ -78,10 +77,8 @@ func (p *TOMLParser) ToBytes(value interface{}, options ...ReadWriteOption) ([]b | ||||
| 	} | ||||
|  | ||||
| 	if colourise { | ||||
| 		source := buf.String() | ||||
| 		buf.Reset() | ||||
| 		if err := quick.Highlight(buf, source, "toml", ColouriseFormatter, ColouriseStyle); err != nil { | ||||
| 			return nil, fmt.Errorf("could not colourise json output: %w", err) | ||||
| 		if err := ColouriseBuffer(buf, "toml"); err != nil { | ||||
| 			return nil, fmt.Errorf("could not colourise output: %w", err) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1,8 +1,6 @@ | ||||
| package storage_test | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"github.com/alecthomas/chroma/quick" | ||||
| 	"github.com/tomwright/dasel/internal/storage" | ||||
| 	"reflect" | ||||
| 	"strings" | ||||
| @@ -69,9 +67,8 @@ func TestTOMLParser_ToBytes(t *testing.T) { | ||||
| 			return | ||||
| 		} | ||||
|  | ||||
| 		buf := new(bytes.Buffer) | ||||
| 		_ = quick.Highlight(buf, string(tomlBytes), "toml", storage.ColouriseFormatter, storage.ColouriseStyle) | ||||
| 		exp := buf.Bytes() | ||||
| 		expBuf, _ := storage.Colourise(string(tomlBytes), "toml") | ||||
| 		exp := expBuf.Bytes() | ||||
| 		if !reflect.DeepEqual(exp, got) { | ||||
| 			t.Errorf("expected %v, got %v", exp, got) | ||||
| 		} | ||||
|   | ||||
| @@ -3,7 +3,6 @@ package storage | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 	"github.com/alecthomas/chroma/quick" | ||||
| 	"github.com/clbanning/mxj/v2" | ||||
| 	"strings" | ||||
| ) | ||||
| @@ -88,10 +87,8 @@ func (p *XMLParser) ToBytes(value interface{}, options ...ReadWriteOption) ([]by | ||||
| 	} | ||||
|  | ||||
| 	if colourise { | ||||
| 		source := buf.String() | ||||
| 		buf.Reset() | ||||
| 		if err := quick.Highlight(buf, source, "xml", ColouriseFormatter, ColouriseStyle); err != nil { | ||||
| 			return nil, fmt.Errorf("could not colourise json output: %w", err) | ||||
| 		if err := ColouriseBuffer(buf, "xml"); err != nil { | ||||
| 			return nil, fmt.Errorf("could not colourise output: %w", err) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1,8 +1,6 @@ | ||||
| package storage_test | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"github.com/alecthomas/chroma/quick" | ||||
| 	"github.com/tomwright/dasel/internal/storage" | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
| @@ -80,9 +78,8 @@ func TestXMLParser_ToBytes_SingleDocument_Colourise(t *testing.T) { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	buf := new(bytes.Buffer) | ||||
| 	_ = quick.Highlight(buf, string(xmlBytes), "xml", storage.ColouriseFormatter, storage.ColouriseStyle) | ||||
| 	exp := buf.Bytes() | ||||
| 	expBuf, _ := storage.Colourise(string(xmlBytes), "xml") | ||||
| 	exp := expBuf.Bytes() | ||||
| 	if !reflect.DeepEqual(exp, got) { | ||||
| 		t.Errorf("expected %v, got %v", exp, got) | ||||
| 	} | ||||
|   | ||||
| @@ -3,7 +3,6 @@ package storage | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 	"github.com/alecthomas/chroma/quick" | ||||
| 	"gopkg.in/yaml.v2" | ||||
| 	"io" | ||||
| ) | ||||
| @@ -111,10 +110,8 @@ func (p *YAMLParser) ToBytes(value interface{}, options ...ReadWriteOption) ([]b | ||||
| 	} | ||||
|  | ||||
| 	if colourise { | ||||
| 		source := buffer.String() | ||||
| 		buffer.Reset() | ||||
| 		if err := quick.Highlight(buffer, source, "yaml", ColouriseFormatter, ColouriseStyle); err != nil { | ||||
| 			return nil, fmt.Errorf("could not colourise json output: %w", err) | ||||
| 		if err := ColouriseBuffer(buffer, "yaml"); err != nil { | ||||
| 			return nil, fmt.Errorf("could not colourise output: %w", err) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1,8 +1,6 @@ | ||||
| package storage_test | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"github.com/alecthomas/chroma/quick" | ||||
| 	"github.com/tomwright/dasel/internal/storage" | ||||
| 	"reflect" | ||||
| 	"strings" | ||||
| @@ -105,9 +103,8 @@ func TestYAMLParser_ToBytes(t *testing.T) { | ||||
| 			t.Errorf("unexpected error: %s", err) | ||||
| 			return | ||||
| 		} | ||||
| 		buf := new(bytes.Buffer) | ||||
| 		_ = quick.Highlight(buf, string(yamlBytes), "yaml", storage.ColouriseFormatter, storage.ColouriseStyle) | ||||
| 		exp := buf.Bytes() | ||||
| 		expBuf, _ := storage.Colourise(string(yamlBytes), "yaml") | ||||
| 		exp := expBuf.Bytes() | ||||
| 		if !reflect.DeepEqual(exp, got) { | ||||
| 			t.Errorf("expected %v, got %v", exp, got) | ||||
| 		} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Tom Wright
					Tom Wright