Embed default dark style into binary

This commit is contained in:
Christian Muehlhaeuser 2019-11-26 21:03:46 +01:00
parent 532fceed22
commit 1f5aa27934
4 changed files with 61 additions and 10 deletions

View file

@ -4,30 +4,41 @@ Render markdown on the CLI, with _pizzazz_!
## Usage
Supply a JSON stylesheet with the `-s` flag. Use a markdown source as the argument.
Use a markdown source as the argument:
Read from file:
```
./gold -s dark.json README.md
./gold README.md
```
Read from stdin:
```
./gold -s dark.json -
./gold -
```
Fetch README from GitHub:
```
./gold -s dark.json github.com/charmbracelet/gold
./gold github.com/charmbracelet/gold
```
Fetch markdown from an HTTP source:
```
./gold -s dark.json https://host.tld/file.md
./gold https://host.tld/file.md
```
When `gold` is started without any markdown source, it will try to find a `README.md`
or `README` file in the current working directory.
When `gold` is started without any markdown source, it will try to find a
`README.md` or `README` file in the current working directory.
You can supply a JSON stylesheet with the `-s` flag:
```
./gold -s mystyle.json
```
Style definitions located in `styles/` can be embedded into the binary by
running [statik](https://github.com/rakyll/statik).
```
statik -f -src styles
```
## Colors
@ -35,4 +46,4 @@ Currently `gold` uses the [Aurora ANSI colors](https://godoc.org/github.com/logr
## Example Output
![Gold Dark Theme](https://github.com/charmbracelet/gold/raw/master/cmd/gold/gold_dark.png)
![Gold Dark Style](https://github.com/charmbracelet/gold/raw/master/cmd/gold/gold_dark.png)

32
main.go
View file

@ -12,9 +12,11 @@ import (
"github.com/mattn/go-isatty"
"github.com/muesli/go-wordwrap"
"github.com/rakyll/statik/fs"
"github.com/spf13/cobra"
"github.com/charmbracelet/gold"
_ "github.com/charmbracelet/gold/cmd/gold/statik"
)
var (
@ -91,6 +93,27 @@ func readerFromArg(s string) (*Source, error) {
return &Source{r, u}, err
}
func loadStyle(f string) ([]byte, error) {
var r io.ReadCloser
var err error
r, err = os.Open(f)
if err != nil {
statikFS, err := fs.New()
if err != nil {
return nil, err
}
r, err = statikFS.Open("/" + f + ".json")
if err != nil {
return nil, err
}
}
defer r.Close()
return ioutil.ReadAll(r)
}
func execute(cmd *cobra.Command, args []string) error {
var arg string
if len(args) > 0 {
@ -105,7 +128,12 @@ func execute(cmd *cobra.Command, args []string) error {
r := gold.NewPlainTermRenderer()
if isatty.IsTerminal(os.Stdout.Fd()) {
r, err = gold.NewTermRenderer(style)
json, err := loadStyle(style)
if err != nil {
return err
}
r, err = gold.NewTermRendererFromBytes(json)
if err != nil {
return err
}
@ -129,6 +157,6 @@ func main() {
}
func init() {
rootCmd.Flags().StringVarP(&style, "style", "s", "dark.json", "style JSON path")
rootCmd.Flags().StringVarP(&style, "style", "s", "dark", "style name or JSON path")
rootCmd.Flags().UintVarP(&width, "width", "w", 100, "word-wrap at width")
}

12
statik/statik.go Normal file
View file

@ -0,0 +1,12 @@
// Code generated by statik. DO NOT EDIT.
package statik
import (
"github.com/rakyll/statik/fs"
)
func init() {
data := "PK\x03\x04\x14\x00\x08\x00\x08\x00\xa9\xacyO\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00 \x00dark.jsonUT\x05\x00\x01\x8fI\xdc]|\x8f\xdd\n\x830\x0cF\xef\xfb\x14!\xd7\x0e\x9c\xde\x0c_Fj\x1b\xb4\x18\x9bQ+\x8c\x0d\xdf}\x94\xfdt\x13\xb7\xbb\x10\xce9\xf0\xdd\x14\x00\xb2\xf3#6\x90n\x004\xc2\x12\xb0\x01<V5\x16\x8f\xdf\xe2-\x05v\x9e\xb0\x81\x18\x16R\x00k\xf1R\xdbH\x97\xf8\xdf\xef\x84\xedVu\x93\xeeiG;\xe1\x1b\x19H[\xe7\xfb\x0c}t\x8a\x8dVg\xcd\x88\xdd\x0bWe\xf9\xcd\xb4\x1d\x8b\xd9[\x9e\xc8g?\x0e4\xa5\x18\xce\xc2:\xb8+\xd9\x83\xd5a\xcc!\x9a\xceCN\xb8\xa8\xd9\x99\xed\xd49\x06\xf91#1jU\xf7\x00\x00\x00\xff\xffPK\x07\x08Z\xdf\xb38\xa0\x00\x00\x00\x88\x01\x00\x00PK\x01\x02\x14\x03\x14\x00\x08\x00\x08\x00\xa9\xacyOZ\xdf\xb38\xa0\x00\x00\x00\x88\x01\x00\x00 \x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00dark.jsonUT\x05\x00\x01\x8fI\xdc]PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00@\x00\x00\x00\xe0\x00\x00\x00\x00\x00"
fs.Register(data)
}