mirror of
https://github.com/maaslalani/slides.git
synced 2026-01-07 21:43:58 -05:00
Allow user customization for slide themes
Closes #1. This change allows users to add a configuration section to their slides, which allows them to specify a different theme if they would like to change the styles of the default theme. A user can add ``` --- theme: ascii --- ``` To use an ascii theme or use `theme: ./styles.json` to specify their own custom JSON theme using glamour styles. Co-authored-by: Britton Hayes <brittonhayes@users.noreply.github.com>
This commit is contained in:
committed by
Maas Lalani
parent
7ec32cf397
commit
e21cc0c0b3
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
/slides
|
||||
.idea
|
||||
|
||||
10
cmd/root.go
10
cmd/root.go
@@ -12,7 +12,9 @@ import (
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/maaslalani/slides/internal/meta"
|
||||
"github.com/maaslalani/slides/internal/model"
|
||||
"github.com/maaslalani/slides/styles"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -47,11 +49,19 @@ var root = &cobra.Command{
|
||||
return errors.New("could not get current user")
|
||||
}
|
||||
|
||||
m, exists := meta.New().ParseHeader(slides[0])
|
||||
// If the user specifies a custom configuration options
|
||||
// skip the first "slide" since this is all configuration
|
||||
if exists {
|
||||
slides = slides[1:]
|
||||
}
|
||||
|
||||
p := tea.NewProgram(model.Model{
|
||||
Slides: slides,
|
||||
Page: 0,
|
||||
Author: user.Name,
|
||||
Date: time.Now().Format("2006-01-02"),
|
||||
Theme: styles.SelectTheme(m.Theme),
|
||||
}, tea.WithAltScreen())
|
||||
|
||||
err = p.Start()
|
||||
|
||||
56
examples/ascii_slides.md
Normal file
56
examples/ascii_slides.md
Normal file
@@ -0,0 +1,56 @@
|
||||
---
|
||||
theme: ascii
|
||||
---
|
||||
|
||||
# Welcome to Slides
|
||||
A terminal based presentation tool
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("Written in Go!")
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Everything is markdown
|
||||
In fact this entire presentation is a markdown file
|
||||
|
||||
---
|
||||
|
||||
# h1
|
||||
## h2
|
||||
### h3
|
||||
#### h4
|
||||
##### h5
|
||||
###### h6
|
||||
|
||||
# Markdown components
|
||||
You can use everything in markdown!
|
||||
* Like bulleted list
|
||||
* You know the deal
|
||||
|
||||
1. Numbered lists too
|
||||
|
||||
| Tables | Too |
|
||||
| ------ | ------ |
|
||||
| Even | Tables |
|
||||
|
||||
---
|
||||
|
||||
All you need to do is separate slides with triple dashes `---` on a separate line,
|
||||
like so:
|
||||
|
||||
```markdown
|
||||
# Slide 1
|
||||
Some stuff
|
||||
|
||||
---
|
||||
|
||||
# Slide 2
|
||||
Some other stuff
|
||||
```
|
||||
3
go.mod
3
go.mod
@@ -3,9 +3,12 @@ module github.com/maaslalani/slides
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/adrg/frontmatter v0.2.0
|
||||
github.com/charmbracelet/bubbles v0.8.0
|
||||
github.com/charmbracelet/bubbletea v0.14.0
|
||||
github.com/charmbracelet/glamour v0.3.0
|
||||
github.com/charmbracelet/lipgloss v0.2.1
|
||||
github.com/spf13/cobra v1.1.3
|
||||
github.com/stretchr/testify v1.7.0
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
||||
14
go.sum
14
go.sum
@@ -11,9 +11,12 @@ cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqCl
|
||||
cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I=
|
||||
cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw=
|
||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
|
||||
github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4=
|
||||
github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE=
|
||||
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U=
|
||||
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI=
|
||||
github.com/alecthomas/chroma v0.8.2 h1:x3zkuE2lUk/RIekyAJ3XRqSCP4zwWDfcw/YJCuCAACg=
|
||||
@@ -58,6 +61,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
|
||||
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
|
||||
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
|
||||
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/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
|
||||
@@ -130,8 +134,10 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
@@ -176,6 +182,7 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
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/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
|
||||
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
|
||||
@@ -216,6 +223,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
|
||||
@@ -351,6 +360,7 @@ google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiq
|
||||
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
|
||||
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
@@ -358,7 +368,11 @@ gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
||||
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
||||
18
internal/file/file.go
Normal file
18
internal/file/file.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// Package file includes utility functions
|
||||
// for working with the filesystem
|
||||
package file
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Exists is a helper to verify
|
||||
// that the provided filepath exists
|
||||
// on the system
|
||||
func Exists(filepath string) bool {
|
||||
info, err := os.Stat(filepath)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
||||
28
internal/file/file_test.go
Normal file
28
internal/file/file_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package file_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/maaslalani/slides/internal/file"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestExists(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
filepath string
|
||||
want bool
|
||||
}{
|
||||
{name: "Find file exists", filepath: "file.go", want: true},
|
||||
{name: "Return false for missing file", filepath: "afilethatdoesntexist.go", want: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
isExist := file.Exists(tt.filepath)
|
||||
if isExist {
|
||||
assert.FileExists(t, tt.filepath)
|
||||
}
|
||||
assert.Equal(t, tt.want, isExist)
|
||||
})
|
||||
}
|
||||
}
|
||||
42
internal/meta/meta.go
Normal file
42
internal/meta/meta.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Package meta implements markdown frontmatter parsing for simple
|
||||
// slides configuration
|
||||
package meta
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/adrg/frontmatter"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// Meta contains all of the data to be parsed
|
||||
// out of a markdown file's header section
|
||||
type Meta struct {
|
||||
Theme string `yaml:"theme"`
|
||||
}
|
||||
|
||||
// New creates a new instance of the
|
||||
// slideshow meta header object
|
||||
func New() *Meta {
|
||||
return &Meta{}
|
||||
}
|
||||
|
||||
// ParseHeader parses metadata from a slideshows header slide
|
||||
// including theme information
|
||||
//
|
||||
// If no front matter is provided, it will fallback to the default theme and
|
||||
// return false to acknowledge that there is no front matter in this slide
|
||||
func (m *Meta) ParseHeader(header string) (*Meta, bool) {
|
||||
fallback := &Meta{Theme: "default"}
|
||||
bytes, err := frontmatter.Parse(strings.NewReader(header), &m)
|
||||
if err != nil {
|
||||
return fallback, false
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(bytes, &m)
|
||||
if err != nil {
|
||||
return fallback, false
|
||||
}
|
||||
|
||||
return m, true
|
||||
}
|
||||
72
internal/meta/meta_test.go
Normal file
72
internal/meta/meta_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package meta_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/maaslalani/slides/internal/meta"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMeta_ParseHeader(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
slideshow string
|
||||
want *meta.Meta
|
||||
}{
|
||||
{
|
||||
name: "Parse theme from header",
|
||||
slideshow: fmt.Sprintf("---\ntheme: %q\n", "dark"),
|
||||
want: &meta.Meta{
|
||||
Theme: "dark",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Fallback to default if no theme provided",
|
||||
slideshow: "\n# Header Slide\n > Subtitle\n",
|
||||
want: &meta.Meta{
|
||||
Theme: "default",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m := &meta.Meta{}
|
||||
got, hasMeta := m.ParseHeader(tt.slideshow)
|
||||
if !hasMeta {
|
||||
assert.NotNil(t, got)
|
||||
}
|
||||
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
want *meta.Meta
|
||||
}{
|
||||
{name: "Create meta struct", want: &meta.Meta{}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equal(t, meta.New(), tt.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleMeta_ParseHeader() {
|
||||
header := `
|
||||
---
|
||||
theme: "dark"
|
||||
---
|
||||
`
|
||||
// Parse the header from the markdown
|
||||
// file
|
||||
m, _ := meta.New().ParseHeader(header)
|
||||
|
||||
// Print the return theme
|
||||
// meta
|
||||
fmt.Println(m.Theme)
|
||||
}
|
||||
@@ -14,6 +14,7 @@ type Model struct {
|
||||
Page int
|
||||
Author string
|
||||
Date string
|
||||
Theme glamour.TermRendererOption
|
||||
viewport viewport.Model
|
||||
}
|
||||
|
||||
@@ -42,12 +43,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m Model) View() string {
|
||||
r, _ := glamour.NewTermRenderer(glamour.WithStylesFromJSONBytes(styles.Theme), glamour.WithWordWrap(0))
|
||||
r, _ := glamour.NewTermRenderer(m.Theme, glamour.WithWordWrap(0))
|
||||
slide, err := r.Render(m.Slides[m.Page])
|
||||
if err != nil {
|
||||
slide = fmt.Sprintf("Error: Could not render markdown! (%v)", err)
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
// Package styles implements the theming logic for slides
|
||||
package styles
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"strings"
|
||||
|
||||
_ "embed"
|
||||
"github.com/charmbracelet/glamour"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
@@ -19,6 +21,11 @@ var (
|
||||
Status = lipgloss.NewStyle().Padding(1)
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed theme.json
|
||||
DefaultTheme []byte
|
||||
)
|
||||
|
||||
func JoinHorizontal(left, right string, width int) string {
|
||||
length := lipgloss.Width(left + right)
|
||||
if width < length {
|
||||
@@ -37,5 +44,19 @@ func JoinVertical(top, bottom string, height int) string {
|
||||
return top + fill + bottom
|
||||
}
|
||||
|
||||
//go:embed theme.json
|
||||
var Theme []byte
|
||||
// SelectTheme picks a glamour style config based
|
||||
// on the theme provided in the markdown header
|
||||
func SelectTheme(theme string) glamour.TermRendererOption {
|
||||
switch theme {
|
||||
case "ascii":
|
||||
return glamour.WithStyles(glamour.ASCIIStyleConfig)
|
||||
case "light":
|
||||
return glamour.WithStyles(glamour.LightStyleConfig)
|
||||
case "dark":
|
||||
return glamour.WithStyles(glamour.DarkStyleConfig)
|
||||
case "notty":
|
||||
return glamour.WithStyles(glamour.NoTTYStyleConfig)
|
||||
default:
|
||||
return glamour.WithStylesFromJSONBytes(DefaultTheme)
|
||||
}
|
||||
}
|
||||
|
||||
74
styles/styles_test.go
Normal file
74
styles/styles_test.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package styles_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/charmbracelet/glamour"
|
||||
"github.com/charmbracelet/glamour/ansi"
|
||||
"github.com/maaslalani/slides/styles"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSelectTheme(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
theme string
|
||||
want ansi.StyleConfig
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "Select dark theme", theme: "dark", want: glamour.DarkStyleConfig, wantErr: false},
|
||||
{name: "Select light theme", theme: "light", want: glamour.LightStyleConfig, wantErr: false},
|
||||
{name: "Select ascii theme", theme: "ascii", want: glamour.ASCIIStyleConfig, wantErr: false},
|
||||
{name: "Select notty theme", theme: "notty", want: glamour.NoTTYStyleConfig, wantErr: false},
|
||||
{name: "Select theme with error", theme: "notty", want: glamour.DarkStyleConfig, wantErr: true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Execute the theme selection and ensure
|
||||
// it returns a non-nil theme
|
||||
selectedTheme := styles.SelectTheme(tt.theme)
|
||||
assert.NotNil(t, selectedTheme)
|
||||
|
||||
// Initialize renderers to compare output
|
||||
gotRenderer, _ := glamour.NewTermRenderer(selectedTheme)
|
||||
wantRenderer, _ := glamour.NewTermRenderer(glamour.WithStyles(tt.want))
|
||||
|
||||
// Render a the same string with two different
|
||||
// renderers
|
||||
gotOutput, _ := gotRenderer.Render(tt.name)
|
||||
wantOutput, _ := wantRenderer.Render(tt.name)
|
||||
|
||||
// Inject exception to ensure a style that doesn't match
|
||||
// it's associated string
|
||||
if tt.wantErr {
|
||||
assert.NotEqual(t, wantOutput, gotOutput)
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure they both match
|
||||
assert.Equal(t, wantOutput, gotOutput)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectTheme_file(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
theme string
|
||||
fileExists bool
|
||||
}{
|
||||
{name: "Select custom theme json", theme: "./theme.json", fileExists: true},
|
||||
{name: "Use an invalid filepath", theme: "./someinvalidfile.toml", fileExists: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Successfully return a theme if a file exists
|
||||
assert.NotNil(t, styles.SelectTheme(tt.theme))
|
||||
|
||||
// Successfully return a theme if a file doesn't exist
|
||||
if !tt.fileExists {
|
||||
assert.NotNil(t, styles.SelectTheme(tt.theme))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user