Skip to content

Commit

Permalink
Separate theme pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Mar 20, 2024
1 parent 8ca74b8 commit 6f6c9e3
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 92 deletions.
13 changes: 12 additions & 1 deletion internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,16 @@ func Reduce(args []string) {
return
}

fmt.Println(output)
node, err := jsonx.Parse([]byte(output))
if err != nil {
println(err.Error())
os.Exit(1)
}

if len(node.Value) > 0 && node.Value[0] == '"' {
s, _ := strconv.Unquote(string(node.Value))
fmt.Println(s)
return
}
fmt.Print(node.PrettyPrint())
}
16 changes: 9 additions & 7 deletions internal/jsonx/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strconv"
"strings"
"unicode/utf8"

"github.com/antonmedv/fx/internal/utils"
)

type jsonParser struct {
Expand Down Expand Up @@ -96,7 +98,7 @@ func (p *jsonParser) parseString() *Node {
var unicode string
for i := 0; i < 4; i++ {
p.next()
if !isHexDigit(p.lastChar) {
if !utils.IsHexDigit(p.lastChar) {
panic(fmt.Sprintf("Invalid Unicode escape sequence '\\u%s%c'", unicode, p.lastChar))
}
unicode += string(p.lastChar)
Expand Down Expand Up @@ -134,7 +136,7 @@ func (p *jsonParser) parseNumber() *Node {
// Handle negative numbers
if p.lastChar == '-' {
p.next()
if !isDigit(p.lastChar) {
if !utils.IsDigit(p.lastChar) {
panic(fmt.Sprintf("Invalid character %q in number", p.lastChar))
}
}
Expand All @@ -143,18 +145,18 @@ func (p *jsonParser) parseNumber() *Node {
if p.lastChar == '0' {
p.next()
} else {
for isDigit(p.lastChar) {
for utils.IsDigit(p.lastChar) {
p.next()
}
}

// Decimal portion
if p.lastChar == '.' {
p.next()
if !isDigit(p.lastChar) {
if !utils.IsDigit(p.lastChar) {
panic(fmt.Sprintf("Invalid character %q in number", p.lastChar))
}
for isDigit(p.lastChar) {
for utils.IsDigit(p.lastChar) {
p.next()
}
}
Expand All @@ -165,10 +167,10 @@ func (p *jsonParser) parseNumber() *Node {
if p.lastChar == '+' || p.lastChar == '-' {
p.next()
}
if !isDigit(p.lastChar) {
if !utils.IsDigit(p.lastChar) {
panic(fmt.Sprintf("Invalid character %q in number", p.lastChar))
}
for isDigit(p.lastChar) {
for utils.IsDigit(p.lastChar) {
p.next()
}
}
Expand Down
31 changes: 31 additions & 0 deletions internal/jsonx/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package jsonx

import (
"strings"

"github.com/antonmedv/fx/internal/theme"
)

func (n *Node) String() string {
Expand All @@ -28,3 +30,32 @@ func (n *Node) String() string {

return out.String()
}

func (n *Node) PrettyPrint() string {
var out strings.Builder

it := n
for it != nil {
for ident := 0; ident < int(it.Depth); ident++ {
out.WriteString(" ")
}
if it.Key != nil {
out.Write(theme.CurrentTheme.Key(it.Key))
out.Write(theme.Colon)
}
if it.Value != nil {
out.Write(theme.Value(it.Value, false, false)(it.Value))
}
if it.Comma {
out.Write(theme.Comma)
}
out.WriteByte('\n')
if it.IsCollapsed() {
it = it.Collapsed
} else {
it = it.Next
}
}

return out.String()
}
102 changes: 52 additions & 50 deletions theme.go → internal/theme/theme.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package theme

import (
"encoding/json"
Expand All @@ -10,50 +10,52 @@ import (

"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"

"github.com/antonmedv/fx/internal/utils"
)

type theme struct {
Cursor color
Syntax color
Preview color
StatusBar color
Search color
Key color
String color
Null color
Boolean color
Number color
Size color
type Theme struct {
Cursor Color
Syntax Color
Preview Color
StatusBar Color
Search Color
Key Color
String Color
Null Color
Boolean Color
Number Color
Size Color
}

type color func(s []byte) []byte
type Color func(s []byte) []byte

func valueStyle(b []byte, selected, chunk bool) color {
func Value(b []byte, selected, chunk bool) Color {
if selected {
return currentTheme.Cursor
return CurrentTheme.Cursor
} else if chunk {
return currentTheme.String
return CurrentTheme.String
} else {
switch b[0] {
case '"':
return currentTheme.String
return CurrentTheme.String
case 't', 'f':
return currentTheme.Boolean
return CurrentTheme.Boolean
case 'n':
return currentTheme.Null
return CurrentTheme.Null
case '{', '[', '}', ']':
return currentTheme.Syntax
return CurrentTheme.Syntax
default:
if isDigit(b[0]) || b[0] == '-' {
return currentTheme.Number
if utils.IsDigit(b[0]) || b[0] == '-' {
return CurrentTheme.Number
}
return noColor
}
}
}

var (
termOutput = termenv.NewOutput(os.Stderr)
TermOutput = termenv.NewOutput(os.Stderr)
)

func init() {
Expand All @@ -71,51 +73,51 @@ func init() {
showSizesValue, ok := os.LookupEnv("FX_SHOW_SIZE")
if ok {
showSizesValue := strings.ToLower(showSizesValue)
showSizes = showSizesValue == "true" || showSizesValue == "yes" || showSizesValue == "on" || showSizesValue == "1"
ShowSizes = showSizesValue == "true" || showSizesValue == "yes" || showSizesValue == "on" || showSizesValue == "1"
}

currentTheme, ok = themes[themeId]
CurrentTheme, ok = themes[themeId]
if !ok {
_, _ = fmt.Fprintf(os.Stderr, "fx: unknown theme %q, available themes: %v\n", themeId, themeNames)
os.Exit(1)
}

if termOutput.ColorProfile() == termenv.Ascii {
currentTheme = themes["0"]
if TermOutput.ColorProfile() == termenv.Ascii {
CurrentTheme = themes["0"]
}

colon = currentTheme.Syntax([]byte{':', ' '})
colonPreview = currentTheme.Preview([]byte{':'})
comma = currentTheme.Syntax([]byte{','})
empty = currentTheme.Preview([]byte{'~'})
dot3 = currentTheme.Preview([]byte("…"))
closeCurlyBracket = currentTheme.Syntax([]byte{'}'})
closeSquareBracket = currentTheme.Syntax([]byte{']'})
Colon = CurrentTheme.Syntax([]byte{':', ' '})
ColonPreview = CurrentTheme.Preview([]byte{':'})
Comma = CurrentTheme.Syntax([]byte{','})
Empty = CurrentTheme.Preview([]byte{'~'})
Dot3 = CurrentTheme.Preview([]byte("…"))
CloseCurlyBracket = CurrentTheme.Syntax([]byte{'}'})
CloseSquareBracket = CurrentTheme.Syntax([]byte{']'})
}

var (
themeNames []string
currentTheme theme
CurrentTheme Theme
defaultCursor = toColor(lipgloss.NewStyle().Reverse(true).Render)
defaultPreview = toColor(lipgloss.NewStyle().Foreground(lipgloss.Color("8")).Render)
defaultStatusBar = toColor(lipgloss.NewStyle().Background(lipgloss.Color("7")).Foreground(lipgloss.Color("0")).Render)
defaultSearch = toColor(lipgloss.NewStyle().Background(lipgloss.Color("11")).Foreground(lipgloss.Color("16")).Render)
defaultNull = fg("243")
defaultSize = toColor(lipgloss.NewStyle().Foreground(lipgloss.Color("8")).Render)
showSizes = false
ShowSizes = false
)

var (
colon []byte
colonPreview []byte
comma []byte
empty []byte
dot3 []byte
closeCurlyBracket []byte
closeSquareBracket []byte
Colon []byte
ColonPreview []byte
Comma []byte
Empty []byte
Dot3 []byte
CloseCurlyBracket []byte
CloseSquareBracket []byte
)

var themes = map[string]theme{
var themes = map[string]Theme{
"0": {
Cursor: defaultCursor,
Syntax: noColor,
Expand Down Expand Up @@ -268,21 +270,21 @@ func noColor(s []byte) []byte {
return s
}

func toColor(f func(s ...string) string) color {
func toColor(f func(s ...string) string) Color {
return func(s []byte) []byte {
return []byte(f(string(s)))
}
}

func fg(color string) color {
func fg(color string) Color {
return toColor(lipgloss.NewStyle().Foreground(lipgloss.Color(color)).Render)
}

func boldFg(color string) color {
func boldFg(color string) Color {
return toColor(lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color(color)).Render)
}

func themeTester() {
func ThemeTester() {
title := lipgloss.NewStyle().Bold(true)
for _, name := range themeNames {
t := themes[name]
Expand Down Expand Up @@ -319,7 +321,7 @@ func themeTester() {
}
}

func exportThemes() {
func ExportThemes() {
lipgloss.SetColorProfile(termenv.ANSI256) // Export in Terminal.app compatible colors
placeholder := []byte{'_'}
extract := func(b []byte) string {
Expand Down
6 changes: 3 additions & 3 deletions internal/jsonx/utils.go → internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package jsonx
package utils

func isHexDigit(ch byte) bool {
func IsHexDigit(ch byte) bool {
return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')
}

func isDigit(ch byte) bool {
func IsDigit(ch byte) bool {
return ch >= '0' && ch <= '9'
}
Loading

0 comments on commit 6f6c9e3

Please sign in to comment.