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`. 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 ## Usage output
``` ```
Serves random media from the specified directories. Serves random media from the specified directories.
@ -130,6 +137,7 @@ Flags:
--russian remove selected images after serving --russian remove selected images after serving
-s, --sort enable sorting -s, --sort enable sorting
--text enable support for text files --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, --verbose log accessed files and other information to stdout
-V, --version display version and exit -V, --version display version and exit
--video enable support for video files --video enable support for video files

View File

@ -11,7 +11,7 @@ import (
) )
const ( const (
ReleaseVersion string = "0.84.4" ReleaseVersion string = "0.85.0"
) )
var ( var (
@ -37,6 +37,7 @@ var (
Russian bool Russian bool
Sorting bool Sorting bool
Text bool Text bool
Theme string
Verbose bool Verbose bool
Version bool Version bool
Videos bool Videos bool
@ -86,6 +87,7 @@ func init() {
rootCmd.Flags().BoolVar(&Russian, "russian", false, "remove selected images after serving") rootCmd.Flags().BoolVar(&Russian, "russian", false, "remove selected images after serving")
rootCmd.Flags().BoolVarP(&Sorting, "sort", "s", false, "enable sorting") rootCmd.Flags().BoolVarP(&Sorting, "sort", "s", false, "enable sorting")
rootCmd.Flags().BoolVar(&Text, "text", false, "enable support for text files") 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(&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().BoolVarP(&Version, "version", "V", false, "display version and exit")
rootCmd.Flags().BoolVar(&Videos, "video", false, "enable support for video files") 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) rootUrl := Prefix + "/" + generateQueryParams(filters, sortOrder, refreshInterval)
var htmlBody strings.Builder 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(faviconHtml)
htmlBody.WriteString(fmt.Sprintf(`<style>%s</style>`, format.Css())) htmlBody.WriteString(fmt.Sprintf(`<style>%s</style>`, format.Css()))
htmlBody.WriteString((format.Title(rootUrl, fileUri, path, fileName, Prefix, mimeType))) htmlBody.WriteString((format.Title(rootUrl, fileUri, path, fileName, Prefix, mimeType)))
@ -331,7 +331,7 @@ func ServePage(args []string) error {
} }
if Code || All { if Code || All {
formats.Add(code.New()) formats.Add(code.New(Theme))
} }
if Flash || All { if Flash || All {

View File

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