theme

package
v1.8.0-beta.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2024 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseTemplatesFS

func ParseTemplatesFS(f fs.FS, t *template.Template) error

Types

type Instance

type Instance struct {
	Name            string
	Status          string
	Error           error
	CurrentReplicas int32
	DesiredReplicas int32
}

Instance holds the current state about an instance

type Options

type Options struct {
	DisplayName      string
	ShowDetails      bool
	InstanceStates   []Instance
	SessionDuration  time.Duration
	RefreshFrequency time.Duration
}

Options holds the customizable input to template

type Theme

type Theme struct {
	Name     string
	Embedded bool
}

Theme represents an available theme

type Themes

type Themes struct {
	// contains filtered or unexported fields
}

func New

func New() (*Themes, error)

func NewWithCustomThemes

func NewWithCustomThemes(custom fs.FS) (*Themes, error)

func (*Themes) List

func (t *Themes) List() []string

List all the loaded themes

func (*Themes) Render

func (t *Themes) Render(name string, opts Options, writer io.Writer) error
Example
package main

import (
	"fmt"
	"os"
	"testing/fstest"
	"time"

	"github.com/acouvreur/sablier/app/theme"
	"github.com/acouvreur/sablier/version"
)

var (
	StartingInstanceInfo = theme.Instance{
		Name:            "starting-instance",
		Status:          "instance is starting...",
		Error:           nil,
		CurrentReplicas: 0,
		DesiredReplicas: 1,
	}
	StartedInstanceInfo = theme.Instance{
		Name:            "started-instance",
		Status:          "instance is started.",
		Error:           nil,
		CurrentReplicas: 1,
		DesiredReplicas: 1,
	}
	ErrorInstanceInfo = theme.Instance{
		Name:            "error-instance",
		Error:           fmt.Errorf("instance does not exist"),
		CurrentReplicas: 0,
		DesiredReplicas: 1,
	}
)

func main() {
	const customTheme = `
<html lang="en">
	<head>
		<meta http-equiv="refresh" content="{{ .RefreshFrequency }}" />
	</head>
	<body>
		Starting {{ .DisplayName }}
		Your instances will stop after {{ .SessionDuration }} of inactivity
		<table>
			{{- range $i, $instance := .InstanceStates }}
			<tr>
				<td>{{ $instance.Name }}</td>
				{{- if $instance.Error }}
				<td>{{ $instance.Error }}</td>
				{{- else }}
				<td>{{ $instance.Status }} ({{ $instance.CurrentReplicas }}/{{ $instance.DesiredReplicas }})</td>
				{{- end}}
			</tr>
			{{- end }}
		</table>
		Sablier version {{ .Version }}
	</body>
</html>
`
	version.Version = "1.0.0"
	themes, err := theme.NewWithCustomThemes(fstest.MapFS{
		"inner/custom-theme.html": &fstest.MapFile{Data: []byte(customTheme)},
	})
	if err != nil {
		panic(err)
	}
	instances := []theme.Instance{
		StartingInstanceInfo,
		StartedInstanceInfo,
		ErrorInstanceInfo,
	}

	err = themes.Render("custom-theme", theme.Options{
		DisplayName:      "Test",
		InstanceStates:   instances,
		ShowDetails:      true,
		SessionDuration:  10 * time.Minute,
		RefreshFrequency: 5 * time.Second,
	}, os.Stdout)

	if err != nil {
		panic(err)
	}

}
Output:

<html lang="en">
	<head>
		<meta http-equiv="refresh" content="5" />
	</head>
	<body>
		Starting Test
		Your instances will stop after 10 minutes of inactivity
		<table>
			<tr>
				<td>starting-instance</td>
				<td>instance is starting... (0/1)</td>
			</tr>
			<tr>
				<td>started-instance</td>
				<td>instance is started. (1/1)</td>
			</tr>
			<tr>
				<td>error-instance</td>
				<td>instance does not exist</td>
			</tr>
		</table>
		Sablier version 1.0.0
	</body>
</html>

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL