Add custom theme support

This commit is contained in:
Seednode 2023-09-14 22:13:21 -05:00
parent fb74fa36f1
commit 5ab6d1ee85
4 changed files with 25 additions and 11 deletions

View File

@ -99,6 +99,13 @@ If any other (or no) value is provided, the selected file will be random.
Note: These patterns require sequentially-numbered files matching the following pattern: `filename###.extension`.
## Themes
The `--code` handler provides syntax highlighting via [alecthomas/chroma](https://github.com/alecthomas/chroma).
Any [supported theme](https://pkg.go.dev/github.com/alecthomas/chroma/v2@v2.9.1/styles#pkg-variables) can be passed via the `--theme` flag.
By default, [`solarized-dark256`](https://xyproto.github.io/splash/docs/solarized-dark256.html) is used.
## Usage output
```
Serves random media from the specified directories.
@ -130,6 +137,7 @@ Flags:
--russian remove selected images after serving
-s, --sort enable sorting
--text enable support for text files
--theme string theme for source code syntax highlighting (default "solarized-dark256")
-v, --verbose log accessed files and other information to stdout
-V, --version display version and exit
--video enable support for video files

View File

@ -11,7 +11,7 @@ import (
)
const (
ReleaseVersion string = "0.84.4"
ReleaseVersion string = "0.85.0"
)
var (
@ -37,6 +37,7 @@ var (
Russian bool
Sorting bool
Text bool
Theme string
Verbose bool
Version bool
Videos bool
@ -86,6 +87,7 @@ func init() {
rootCmd.Flags().BoolVar(&Russian, "russian", false, "remove selected images after serving")
rootCmd.Flags().BoolVarP(&Sorting, "sort", "s", false, "enable sorting")
rootCmd.Flags().BoolVar(&Text, "text", false, "enable support for text files")
rootCmd.Flags().StringVar(&Theme, "theme", "solarized-dark256", "theme for source code syntax highlighting")
rootCmd.Flags().BoolVarP(&Verbose, "verbose", "v", false, "log accessed files and other information to stdout")
rootCmd.Flags().BoolVarP(&Version, "version", "V", false, "display version and exit")
rootCmd.Flags().BoolVar(&Videos, "video", false, "enable support for video files")

View File

@ -249,7 +249,7 @@ func serveMedia(paths []string, regexes *regexes, formats *types.Types) httprout
rootUrl := Prefix + "/" + generateQueryParams(filters, sortOrder, refreshInterval)
var htmlBody strings.Builder
htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
htmlBody.WriteString(`<!DOCTYPE html><html class="bg" lang="en"><head>`)
htmlBody.WriteString(faviconHtml)
htmlBody.WriteString(fmt.Sprintf(`<style>%s</style>`, format.Css()))
htmlBody.WriteString((format.Title(rootUrl, fileUri, path, fileName, Prefix, mimeType)))
@ -331,7 +331,7 @@ func ServePage(args []string) error {
}
if Code || All {
formats.Add(code.New())
formats.Add(code.New(Theme))
}
if Flash || All {

View File

@ -19,7 +19,9 @@ import (
"seedno.de/seednode/roulette/types"
)
type Format struct{}
type Format struct {
Theme string
}
func (t Format) Css() string {
var css strings.Builder
@ -29,7 +31,7 @@ func (t Format) Css() string {
html.WithClasses(true),
html.WrapLongLines(true))
style := styles.Get("solarized-dark256")
style := styles.Get(t.Theme)
if style == nil {
style = styles.Fallback
}
@ -52,8 +54,8 @@ func (t Format) Css() string {
css.Write(b)
css.WriteString("a{margin:0;padding:0;height:100%;width:100%;color:inherit;text-decoration:none;}\n")
css.WriteString("html{background-color:#1c1c1c;}\n")
css.WriteString("html{height:100%;width:100%;}\n")
css.WriteString("a{bottom:0;left:0;position:absolute;right:0;top:0;margin:1rem;padding:0;height:99%;width:99%;color:inherit;text-decoration:none;}\n")
return css.String()
}
@ -83,7 +85,7 @@ func (t Format) Body(rootUrl, fileUri, filePath, fileName, prefix, mime string)
w := bufio.NewWriter(&response)
r := bufio.NewReader(&response)
style := styles.Get("solarized-dark256")
style := styles.Get(t.Theme)
if style == nil {
style = styles.Fallback
}
@ -219,10 +221,12 @@ func (t Format) Validate(filePath string) bool {
return true
}
func New() Format {
return Format{}
func New(theme string) Format {
return Format{
Theme: theme,
}
}
func init() {
types.SupportedFormats.Register(New())
types.SupportedFormats.Register(New(""))
}