-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add syntax highlighting to file viewer
- Loading branch information
1 parent
44721f7
commit 00b82ac
Showing
5 changed files
with
93 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters