Load finds the file through paths (no ./vcom.toml), returns defaults plus an error for a broken file instead of exiting, warns about unknown and deprecated keys (strict second pass), resets invalid values with a warning, and aliases center_width_percent to ui.preview_width_percent. New keys from design §9.2 get defaults. SetKey edits one line keeping comments, order and unknown keys, appends missing keys/sections, creates the XDG file, refuses a file that does not parse and verifies the result before an atomic write. The old UI shows config errors in the status line and survives an unknown theme name; the reference vcom.toml is documented and current. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
227 lines
7.7 KiB
Go
227 lines
7.7 KiB
Go
// Package config holds the vcom.toml schema, loading and the in-place
|
|
// SetKey edit (design §9.2).
|
|
//
|
|
// Loading never aborts the program: a broken file yields Default() plus an
|
|
// error for the status line; unknown or deprecated keys yield warnings; an
|
|
// invalid value is replaced by its default with a warning.
|
|
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
Startup StartupConfig `toml:"startup"`
|
|
UI UIConfig `toml:"ui"`
|
|
Browser BrowserConfig `toml:"browser"`
|
|
Preview PreviewConfig `toml:"preview"`
|
|
Behavior BehaviorConfig `toml:"behavior"`
|
|
Transfer TransferConfig `toml:"transfer"`
|
|
SSH SSHConfig `toml:"ssh"`
|
|
}
|
|
|
|
type StartupConfig struct {
|
|
LeftPath string `toml:"left_path"`
|
|
RightPath string `toml:"right_path"`
|
|
}
|
|
|
|
type UIConfig struct {
|
|
// AppTitle is deprecated (warning when set); only the old UI still shows
|
|
// it. It goes away with the old UI in stage 7.
|
|
AppTitle string `toml:"app_title"`
|
|
Theme string `toml:"theme"`
|
|
IconMode string `toml:"icon_mode"`
|
|
ShowFooter bool `toml:"show_footer"`
|
|
Border string `toml:"border"`
|
|
PathDisplay string `toml:"path_display"`
|
|
PaneGap int `toml:"pane_gap"`
|
|
// PreviewWidthPercent is the preview width in the Info screen. The old
|
|
// name center_width_percent is accepted as an alias with a warning.
|
|
PreviewWidthPercent int `toml:"preview_width_percent"`
|
|
}
|
|
|
|
type BrowserConfig struct {
|
|
ShowHidden bool `toml:"show_hidden"`
|
|
DirsFirst bool `toml:"dirs_first"`
|
|
HumanReadableSize bool `toml:"human_readable_size"`
|
|
Sort SortConfig `toml:"sort"`
|
|
Columns BrowserColumnsConfig `toml:"columns"`
|
|
}
|
|
|
|
type SortConfig struct {
|
|
By string `toml:"by"`
|
|
Reverse bool `toml:"reverse"`
|
|
}
|
|
|
|
type BrowserColumnsConfig struct {
|
|
Name bool `toml:"name"`
|
|
Size bool `toml:"size"`
|
|
Modified bool `toml:"modified"`
|
|
Created bool `toml:"created"`
|
|
Permissions bool `toml:"permissions"`
|
|
Extension bool `toml:"extension"`
|
|
}
|
|
|
|
type PreviewConfig struct {
|
|
ShowMetadata bool `toml:"show_metadata"`
|
|
WrapText bool `toml:"wrap_text"`
|
|
MaxPreviewBytes int64 `toml:"max_preview_bytes"`
|
|
DirectoryPreviewLimit int `toml:"directory_preview_limit"`
|
|
// ImageBackend: auto | kitty | ueberzugpp | none.
|
|
ImageBackend string `toml:"image_backend"`
|
|
// MaxRemoteImageBytes: larger remote images are not downloaded for preview.
|
|
MaxRemoteImageBytes int64 `toml:"max_remote_image_bytes"`
|
|
}
|
|
|
|
type BehaviorConfig struct {
|
|
ConfirmDelete bool `toml:"confirm_delete"` // asks before moving to the trash
|
|
ConfirmOverwrite bool `toml:"confirm_overwrite"`
|
|
CalculateDirSizeOnSpace bool `toml:"calculate_dir_size_on_space"`
|
|
FollowSymlinks bool `toml:"follow_symlinks"` // default of the transfer dialog
|
|
ConfirmQuit bool `toml:"confirm_quit"`
|
|
}
|
|
|
|
type TransferConfig struct {
|
|
OnConflict string `toml:"on_conflict"` // ask | overwrite | skip | newer | rename
|
|
Preserve bool `toml:"preserve"` // keep mode and mtime
|
|
Fsync string `toml:"fsync"` // replace | always | never
|
|
MaxParallelJobs int `toml:"max_parallel_jobs"`
|
|
}
|
|
|
|
type SSHConfig struct {
|
|
KeepaliveInterval Duration `toml:"keepalive_interval"`
|
|
ConnectTimeout Duration `toml:"connect_timeout"`
|
|
}
|
|
|
|
// Duration is a time.Duration written as a Go duration string ("15s").
|
|
type Duration struct{ time.Duration }
|
|
|
|
func (d Duration) MarshalText() ([]byte, error) { return []byte(d.String()), nil }
|
|
|
|
func (d *Duration) UnmarshalText(b []byte) error {
|
|
v, err := time.ParseDuration(string(b))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
d.Duration = v
|
|
return nil
|
|
}
|
|
|
|
func Default() Config {
|
|
return Config{
|
|
UI: UIConfig{
|
|
AppTitle: "vcom",
|
|
Theme: "catppuccin-mocha",
|
|
IconMode: "auto",
|
|
ShowFooter: true,
|
|
Border: "rounded",
|
|
PathDisplay: "short",
|
|
PaneGap: 1,
|
|
PreviewWidthPercent: 50,
|
|
},
|
|
Browser: BrowserConfig{
|
|
ShowHidden: true,
|
|
DirsFirst: true,
|
|
HumanReadableSize: true,
|
|
Sort: SortConfig{By: "name"},
|
|
Columns: BrowserColumnsConfig{Name: true, Size: true, Modified: true},
|
|
},
|
|
Preview: PreviewConfig{
|
|
ShowMetadata: true,
|
|
MaxPreviewBytes: 64 * 1024,
|
|
DirectoryPreviewLimit: 80,
|
|
ImageBackend: "auto",
|
|
MaxRemoteImageBytes: 20 << 20,
|
|
},
|
|
Behavior: BehaviorConfig{
|
|
ConfirmDelete: true,
|
|
ConfirmOverwrite: true,
|
|
CalculateDirSizeOnSpace: true,
|
|
ConfirmQuit: true,
|
|
},
|
|
Transfer: TransferConfig{
|
|
OnConflict: "ask",
|
|
Preserve: true,
|
|
Fsync: "replace",
|
|
MaxParallelJobs: 2,
|
|
},
|
|
SSH: SSHConfig{
|
|
KeepaliveInterval: Duration{15 * time.Second},
|
|
ConnectTimeout: Duration{20 * time.Second},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Validate normalises c and replaces invalid values with their defaults. It
|
|
// returns one warning per replaced value; it never fails.
|
|
func (c *Config) Validate() []string {
|
|
d := Default()
|
|
var warns []string
|
|
reset := func(key string, got any, fix func()) {
|
|
warns = append(warns, fmt.Sprintf("%s: invalid value %v, using the default", key, got))
|
|
fix()
|
|
}
|
|
oneOf := func(key string, v *string, def string, allowed ...string) {
|
|
s := strings.ToLower(strings.TrimSpace(*v))
|
|
if s == "" {
|
|
*v = def
|
|
return
|
|
}
|
|
for _, a := range allowed {
|
|
if s == a {
|
|
*v = s
|
|
return
|
|
}
|
|
}
|
|
reset(key, *v, func() { *v = def })
|
|
}
|
|
|
|
if strings.TrimSpace(c.UI.Theme) == "" {
|
|
c.UI.Theme = d.UI.Theme
|
|
}
|
|
if strings.TrimSpace(c.UI.AppTitle) == "" {
|
|
c.UI.AppTitle = d.UI.AppTitle
|
|
}
|
|
oneOf("ui.icon_mode", &c.UI.IconMode, "auto", "auto", "nerd", "ascii")
|
|
if strings.TrimSpace(c.UI.PathDisplay) == "" {
|
|
c.UI.PathDisplay = d.UI.PathDisplay
|
|
}
|
|
if c.UI.PaneGap < 0 || c.UI.PaneGap > 4 {
|
|
reset("ui.pane_gap", c.UI.PaneGap, func() { c.UI.PaneGap = d.UI.PaneGap })
|
|
}
|
|
if c.UI.PreviewWidthPercent < 20 || c.UI.PreviewWidthPercent > 80 {
|
|
reset("ui.preview_width_percent", c.UI.PreviewWidthPercent, func() { c.UI.PreviewWidthPercent = d.UI.PreviewWidthPercent })
|
|
}
|
|
oneOf("browser.sort.by", &c.Browser.Sort.By, "name", "name", "modified", "size", "created", "extension")
|
|
if !c.Browser.Columns.Name {
|
|
reset("browser.columns.name", false, func() { c.Browser.Columns.Name = true })
|
|
}
|
|
if c.Preview.MaxPreviewBytes <= 0 {
|
|
reset("preview.max_preview_bytes", c.Preview.MaxPreviewBytes, func() { c.Preview.MaxPreviewBytes = d.Preview.MaxPreviewBytes })
|
|
}
|
|
if c.Preview.DirectoryPreviewLimit <= 0 {
|
|
reset("preview.directory_preview_limit", c.Preview.DirectoryPreviewLimit, func() {
|
|
c.Preview.DirectoryPreviewLimit = d.Preview.DirectoryPreviewLimit
|
|
})
|
|
}
|
|
oneOf("preview.image_backend", &c.Preview.ImageBackend, "auto", "auto", "kitty", "ueberzugpp", "none")
|
|
if c.Preview.MaxRemoteImageBytes < 0 {
|
|
reset("preview.max_remote_image_bytes", c.Preview.MaxRemoteImageBytes, func() {
|
|
c.Preview.MaxRemoteImageBytes = d.Preview.MaxRemoteImageBytes
|
|
})
|
|
}
|
|
oneOf("transfer.on_conflict", &c.Transfer.OnConflict, "ask", "ask", "overwrite", "skip", "newer", "rename")
|
|
oneOf("transfer.fsync", &c.Transfer.Fsync, "replace", "replace", "always", "never")
|
|
if c.Transfer.MaxParallelJobs < 1 || c.Transfer.MaxParallelJobs > 16 {
|
|
reset("transfer.max_parallel_jobs", c.Transfer.MaxParallelJobs, func() { c.Transfer.MaxParallelJobs = d.Transfer.MaxParallelJobs })
|
|
}
|
|
if c.SSH.KeepaliveInterval.Duration < time.Second {
|
|
reset("ssh.keepalive_interval", c.SSH.KeepaliveInterval, func() { c.SSH.KeepaliveInterval = d.SSH.KeepaliveInterval })
|
|
}
|
|
if c.SSH.ConnectTimeout.Duration < time.Second {
|
|
reset("ssh.connect_timeout", c.SSH.ConnectTimeout, func() { c.SSH.ConnectTimeout = d.SSH.ConnectTimeout })
|
|
}
|
|
return warns
|
|
}
|