Skip to content

Commit

Permalink
Add syntax highlighting to file viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
paololazzari committed Oct 23, 2023
1 parent 44721f7 commit 00b82ac
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"golang.org/x/term"
)

const version = "0.2.4"
const version = "0.3.0"

func completionCommand() *cobra.Command {
return &cobra.Command{
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/paololazzari/play
go 1.18

require (
github.com/alecthomas/chroma v0.10.0
github.com/gdamore/tcell/v2 v2.6.0
github.com/rivo/tview v0.0.0-20230916092115-0ad06c2ea3dd
github.com/spf13/cobra v1.7.0
Expand All @@ -11,6 +12,7 @@ require (
)

require (
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg=
Expand All @@ -9,6 +16,8 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/tview v0.0.0-20230916092115-0ad06c2ea3dd h1:5fv4woBUz69TNaDvJl19bFdMiDdhdGKtYmzZOk6pGVY=
github.com/rivo/tview v0.0.0-20230916092115-0ad06c2ea3dd/go.mod h1:nVwGv4MP47T0jvlk7KuTTjjuSmrGO4JF0iaiNt4bufE=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand All @@ -19,6 +28,9 @@ github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down Expand Up @@ -53,4 +65,6 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
66 changes: 66 additions & 0 deletions src/ui/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ui

import (
"io"
"strings"

"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
)

const (
style = "monokai"
)

var (
buff strings.Builder
backGroundColor string

// Color formatter.
Color = formatters.Register("Color", chroma.FormatterFunc(func(w io.Writer, s *chroma.Style, iterator chroma.Iterator) error {
for t := iterator(); t != chroma.EOF; t = iterator() {
colour := s.Get(t.Type).Colour
backGroundColor = s.Get(t.Type).Background.String()
var sb strings.Builder

sb.WriteString("[")
sb.WriteString(colour.String())
sb.WriteString("]")
sb.WriteString(t.Value)

if _, err := w.Write([]byte(sb.String())); err != nil {
return err
}
}
return nil
}))
)

func Colorize(partialFileContents string, fileContents string, filename string) {
buff.Reset()

// attempt to the language from its filename.
l := lexers.Match(filename)
if l == nil {
// otherwise attempt by its partial contents
l = lexers.Analyse(partialFileContents)
if l == nil {
// otherwise attempt by its entire contents
l = lexers.Analyse(fileContents)
if l == nil {
// otherwise fall back to default
l = lexers.Fallback
}
}
}
s := styles.Get(style)
c := Color
it, err := l.Tokenise(nil, fileContents)
if err != nil {
buff.Write([]byte(fileContents))
}
c.Format(&buff, s, it)
return
}
16 changes: 10 additions & 6 deletions src/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ type UI struct {
}

type nodeReference struct {
path string
fileContents string
path string
partialFileContents string
}

func getNodePath(node *tview.TreeNode) string {
ref := node.GetReference()
return ref.(nodeReference).path
}

func getNodeFileContents(node *tview.TreeNode) string {
func getNodePartialFileContents(node *tview.TreeNode) string {
ref := node.GetReference()
return ref.(nodeReference).fileContents
return ref.(nodeReference).partialFileContents
}

// Returns the TextView with the command itself
Expand Down Expand Up @@ -471,9 +471,13 @@ func (ui *UI) configFileOptionsTreeView() {
if ui.FileOptionsTreeView.GetCurrentNode() == ui.FileOptionsTreeView.GetRoot() {
return event
}
file, err := os.ReadFile(getNodePath(ui.FileOptionsTreeView.GetCurrentNode()))
filename := getNodePath(ui.FileOptionsTreeView.GetCurrentNode())
file, err := os.ReadFile(filename)
if err == nil {
ui.FileView.SetText(string(file))
fileContents := string(file)
Colorize(getNodePartialFileContents(ui.FileOptionsTreeView.GetCurrentNode()), fileContents, filename)
ui.FileView.SetText(buff.String())
ui.FileView.SetBackgroundColor(tcell.GetColor(backGroundColor))
ui.App.SetRoot(ui.FileView, true).
SetFocus(ui.FileView)
}
Expand Down

0 comments on commit 00b82ac

Please sign in to comment.