Help us shape the future of Go! Take the 2026 Go Developer Survey

state

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package state persists per-project dump records in the user's application data directory so delta dumps can know the last commit a project was dumped from, without polluting the repository itself.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanonicalRoot added in v1.3.0

func CanonicalRoot(rootDir string) (string, error)

CanonicalRoot resolves rootDir to the absolute, cleaned path every state-key site must use. Writers (record) and readers (delta, diff, picker) share this helper so a dump recorded as "." is found again as ".", an absolute path, or any equivalent spelling.

func EffectiveTheme added in v1.4.0

func EffectiveTheme(p Preferences) string

EffectiveTheme resolves the single shared theme id: Theme wins, with the legacy UITheme as fallback for files written before unification. Empty means the default theme; "system" means follow the OS (web only, the TUI falls back to its default).

func GetProjectKey

func GetProjectKey(absPath string) string

GetProjectKey returns a stable 16-hex-char key for an absolute project path.

func LockPath added in v1.3.0

func LockPath() (string, error)

LockPath returns the advisory lock file alongside state.json. It must be held across every read-modify-write so concurrent processes (e.g. a watch session and a manual dump) cannot clobber each other's records.

func MutateState added in v1.3.0

func MutateState(fn func(st *AppState) error) error

MutateState runs fn inside a cross-process file lock plus the in-process mutex, loading the state, applying fn, and saving atomically. All state mutations must go through here; Load/Save remain for lock-free reads and single-shot writes.

func RecordRecent added in v1.4.0

func RecordRecent(root string)

RecordRecent moves root to the front of the shared recents list, refreshing its branch and timestamp. It never fails the caller: state bookkeeping must not break a project open.

func RemoveRecent added in v1.4.0

func RemoveRecent(root string) error

RemoveRecent drops root from the shared recents list.

func SavePreferences

func SavePreferences(prefs Preferences) error

SavePreferences writes user preferences atomically, sanitizing first.

func SetTheme added in v1.4.0

func SetTheme(id string) error

SetTheme records id as the shared theme for both the TUI and serve, preserving every other preference. It replaces the old pattern of saving Preferences{UITheme: name}, which silently wiped the rest of the file.

func StatePath

func StatePath() (string, error)

StatePath returns the JSON state file location in the user's config directory: ~/.config/sift/state.json on Linux, %APPDATA% on Windows.

Types

type AppState

type AppState struct {
	Projects map[string]DumpRecord `json:"projects"`
}

AppState holds the dump record of every project that has been dumped, keyed by GetProjectKey.

func Load

func Load() (*AppState, error)

Load reads the state file, returning an empty state when it does not exist.

func (*AppState) Get

func (s *AppState) Get(key string) (DumpRecord, bool)

Get returns the dump record for a project key, if present.

func (*AppState) Record

func (s *AppState) Record(key, commitHash, commitMsg string, files, tokens int)

Record stores a new dump record for key.

func (*AppState) Save

func (s *AppState) Save() error

Save writes the state to disk atomically so a crash never truncates it.

type DumpRecord

type DumpRecord struct {
	LastCommitHash string    `json:"last_commit_hash"`
	LastCommitMsg  string    `json:"last_commit_msg"`
	DumpTimestamp  time.Time `json:"dump_timestamp"`
	FilesDumped    int       `json:"files_dumped"`
	TokensDumped   int       `json:"tokens_dumped"`
}

DumpRecord describes the most recent dump of a project.

type Preferences

type Preferences struct {
	UITheme string `json:"ui_theme,omitempty"`
	// Theme is the web UI theme id ([a-z0-9-]{1,32}); "system" follows the OS.
	Theme string `json:"theme,omitempty"`
	// DefaultStyle is the default output style (xml, markdown, plain).
	DefaultStyle string `json:"default_style,omitempty"`
	// DefaultBudget is the default token budget for fresh sessions.
	DefaultBudget int `json:"default_budget,omitempty"`
	// ShowHidden reveals dotfiles in the serve folder browser. Default hides them.
	ShowHidden bool `json:"show_hidden,omitempty"`
	// FileSort orders the serve folder browser: "name" (default) or "updated".
	FileSort string `json:"file_sort,omitempty"`
}

Preferences contains user-level interactive preferences that should survive picker sessions but do not belong to a project's dump history. The TUI and `sift serve` share this file: theme ids and defaults set in either surface take effect in both.

func LoadPreferences

func LoadPreferences() (Preferences, error)

LoadPreferences loads user preferences. Missing preferences are normal and return the zero-value preference set.

func SanitizePreferences added in v1.4.0

func SanitizePreferences(p Preferences) Preferences

type Recent added in v1.4.0

type Recent struct {
	Root       string `json:"root"`
	Name       string `json:"name"`
	LastOpened string `json:"last_opened"`
	Branch     string `json:"branch,omitempty"`
}

Recent is one entry in the shared recent-projects list.

func ListRecents added in v1.4.0

func ListRecents() ([]Recent, error)

ListRecents returns the shared recent-projects list, most recent first. A corrupt file resets to empty so one bad write never wedges the UI.

Jump to

Keyboard shortcuts

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