Skip to content

Commit

Permalink
automatic user configuration saving and loading
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Feb 1, 2024
1 parent a65c3b4 commit bb88653
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
77 changes: 55 additions & 22 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,59 @@ package main
import (
"embed"
_ "embed"
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"

"github.com/adrg/xdg"
)

// Config is the configuration options for a screenshot.
type Config struct {
Input string `arg:"" help:"Code to screenshot." optional:""`
Input string `json:",omitempty" arg:"" help:"Code to screenshot." optional:""`

// Window
Background string `help:"Apply a background fill." short:"b" placeholder:"#FFF" group:"Window"`
Margin []int `help:"Apply margin to the window." short:"m" placeholder:"0" group:"Window"`
Padding []int `help:"Apply padding to the code." short:"p" placeholder:"0" group:"Window"`
Window bool `help:"Display window controls." short:"w" group:"Window"`
Background string `json:"background" help:"Apply a background fill." short:"b" placeholder:"#FFF" group:"Window"`
Margin []int `json:"margin" help:"Apply margin to the window." short:"m" placeholder:"0" group:"Window"`
Padding []int `json:"padding" help:"Apply padding to the code." short:"p" placeholder:"0" group:"Window"`
Window bool `json:"window" help:"Display window controls." short:"w" group:"Window"`

// Settings
Config string `help:"Base configuration file or template." short:"c" group:"Settings" default:"base" placeholder:"base"`
Interactive bool `help:"Use an interactive form for configuration options." short:"i" group:"Settings"`
Language string `help:"Language of code file." short:"l" group:"Settings" placeholder:"go"`
Output string `help:"Output location for {{.svg}}, {{.png}}, or {{.jpeg}}." short:"o" group:"Settings" default:"out.svg" placeholder:"out.svg"`
Theme string `help:"Theme to use for syntax highlighting." short:"t" group:"Settings" placeholder:"charm"`
Config string `json:"config,omitempty" help:"Base configuration file or template." short:"c" group:"Settings" default:"default" placeholder:"base"`
Interactive bool `json:",omitempty" help:"Use an interactive form for configuration options." short:"i" group:"Settings"`
Language string `json:"language,omitempty" help:"Language of code file." short:"l" group:"Settings" placeholder:"go"`
Output string `json:"output,omitempty" help:"Output location for {{.svg}}, {{.png}}, or {{.jpeg}}." short:"o" group:"Settings" default:"out.svg" placeholder:"out.svg"`
Theme string `json:"theme" help:"Theme to use for syntax highlighting." short:"t" group:"Settings" placeholder:"charm"`

// Decoration
Border Border `embed:"" prefix:"border." group:"Border"`
Shadow Shadow `embed:"" prefix:"shadow." help:"add a shadow to the window" short:"s" group:"Shadow"`
Border Border `json:"border" embed:"" prefix:"border." group:"Border"`
Shadow Shadow `json:"shadow" embed:"" prefix:"shadow." help:"add a shadow to the window" short:"s" group:"Shadow"`

// Font
Font Font `embed:"" prefix:"font." group:"Font"`
LineHeight float64 `help:"Line height relative to font size." group:"Font" placeholder:"1.2"`
Font Font `json:"font" embed:"" prefix:"font." group:"Font"`
LineHeight float64 `json:"line_height" help:"Line height relative to font size." group:"Font" placeholder:"1.2"`
}

// Shadow is the configuration options for a drop shadow.
type Shadow struct {
Blur int `help:"Shadow Gaussian Blur." placeholder:"0"`
X int `help:"Shadow offset {{x}} coordinate" placeholder:"0"`
Y int `help:"Shadow offset {{y}} coordinate" placeholder:"0"`
Blur int `json:"blur" help:"Shadow Gaussian Blur." placeholder:"0"`
X int `json:"x" help:"Shadow offset {{x}} coordinate" placeholder:"0"`
Y int `json:"y" help:"Shadow offset {{y}} coordinate" placeholder:"0"`
}

// Border is the configuration options for a window border.
type Border struct {
Radius int `help:"Corner radius of window." short:"r" placeholder:"0"`
Width int `help:"Border width thickness." placeholder:"1"`
Color string `help:"Border color." placeholder:"#000"`
Radius int `json:"radius" help:"Corner radius of window." short:"r" placeholder:"0"`
Width int `json:"width" help:"Border width thickness." placeholder:"1"`
Color string `json:"color" help:"Border color." placeholder:"#000"`
}

// Font is the configuration options for a font.
type Font struct {
Family string `help:"Font family to use for code." placeholder:"monospace"`
Size float64 `help:"Font size to use for code." placeholder:"14"`
Family string `json:"family" help:"Font family to use for code." placeholder:"monospace"`
Size float64 `json:"size" help:"Font size to use for code." placeholder:"14"`
}

//go:embed configurations/*
Expand Down Expand Up @@ -78,3 +85,29 @@ const (
bottom side = 2
left side = 3
)

var userConfigPath = filepath.Join(xdg.ConfigHome, "freeze", "default.json")

func loadUserConfig() (fs.File, error) {
return os.Open(userConfigPath)
}

func saveUserConfig(config Config) error {
config.Input = ""
config.Output = ""
config.Interactive = false

err := os.MkdirAll(filepath.Dir(userConfigPath), os.ModePerm)

if err != nil {
return err
}
f, err := os.Create(userConfigPath)
b, err := json.Marshal(config)
if err != nil {
return err
}
_, err = f.Write(b)
fmt.Println(b)
return err
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/charmbracelet/freeze
go 1.21

require (
github.com/adrg/xdg v0.4.0
github.com/alecthomas/chroma v0.10.1-0.20220126230913-d491f1b5c1d2
github.com/alecthomas/kong v0.8.2-0.20240122082144-30e84613fe34
github.com/beevik/etree v1.3.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
github.com/alecthomas/assert/v2 v2.5.0 h1:OJKYg53BQx06/bMRBSPDCO49CbCDNiUQXwdoNrt6x5w=
github.com/alecthomas/assert/v2 v2.5.0/go.mod h1:fw5suVxB+wfYJ3291t0hRTqtGzFYdSwstnRQdaQx2DM=
github.com/alecthomas/chroma v0.10.1-0.20220126230913-d491f1b5c1d2 h1:Gg09t2u+C08At6TYucNrD3Cbaq97SUHax84BzQwRTgU=
Expand Down Expand Up @@ -71,6 +73,7 @@ golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRj
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
Expand Down
21 changes: 14 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ func main() {
printErrorFatal("Invalid Usage", err)
}

c, err := configs.Open("configurations/" + config.Config + ".json")
isDefaultConfig := config.Config == "default"

configFile, err := loadUserConfig()
if err != nil || !isDefaultConfig {
configFile, err = configs.Open("configurations/" + config.Config + ".json")
}
if err != nil {
c, err = os.Open(config.Config)
if err != nil {
c, _ = configs.Open("configurations/base.json")
}
configFile, err = os.Open(config.Config)
}

r, err := kong.JSON(c)
if err != nil {
configFile, _ = configs.Open("configurations/base.json")
}
r, err := kong.JSON(configFile)
if err != nil {
printErrorFatal("Invalid JSON", err)
}
Expand All @@ -63,6 +67,9 @@ func main() {
if err != nil {
printErrorFatal("", err)
}
if isDefaultConfig {
_ = saveUserConfig(*cfg)
}
}

config.Margin = expandMargin(config.Margin)
Expand Down

0 comments on commit bb88653

Please sign in to comment.