From ab59104dcf32cc3916138f5c30fb7cab9a72c58a Mon Sep 17 00:00:00 2001 From: Bruno Llacer Trotti Date: Wed, 13 Nov 2024 21:38:28 -0300 Subject: [PATCH 1/2] godocs config_function and default_config file --- src/cmd/main.go | 5 ++++- src/internal/config_function.go | 30 ++++++++++++++++++++++++------ src/internal/default_config.go | 3 +++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/cmd/main.go b/src/cmd/main.go index 24cac49..ec50295 100644 --- a/src/cmd/main.go +++ b/src/cmd/main.go @@ -61,9 +61,10 @@ func Run(content embed.FS) { }, }, Action: func(c *cli.Context) error { + // If no args are called along with "spf" use current dir path := "" if c.Args().Present() { - path = c.Args().First() + path = c.Args().First() } InitConfigFile() @@ -171,6 +172,7 @@ func checkFirstUse() bool { } return firstUse } + func writeConfigFile(path, data string) error { if _, err := os.Stat(path); os.IsNotExist(err) { if err := os.WriteFile(path, []byte(data), 0644); err != nil { @@ -227,6 +229,7 @@ func CheckForUpdates() { return } + //Check if the local version is outdated if versionToNumber(release.TagName) > versionToNumber(variable.CurrentVersion) { fmt.Println(lipgloss.NewStyle().Foreground(lipgloss.Color("#FF69E1")).Render("┃ ") + lipgloss.NewStyle().Foreground(lipgloss.Color("#FFBA52")).Bold(true).Render("A new version ") + diff --git a/src/internal/config_function.go b/src/internal/config_function.go index 9fa8a88..a5375f3 100644 --- a/src/internal/config_function.go +++ b/src/internal/config_function.go @@ -16,9 +16,12 @@ import ( "github.com/yorukot/superfile/src/config/icon" ) +// initialConfig load and handle all configuration files (spf config,hotkeys +// themes) setted up. Returns absolute path of dir pointing to the file Panel func initialConfig(dir string) (toggleDotFileBool bool, firstFilePanelDir string) { var err error + // Open log stream logOutput, err = os.OpenFile(variable.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatalf("Error while opening superfile.log file: %v", err) @@ -64,9 +67,12 @@ func initialConfig(dir string) (toggleDotFileBool bool, firstFilePanelDir string return toggleDotFileBool, firstFilePanelDir } +// Load and fix config toml file func loadConfigFile() { + //Initialize default configs _ = toml.Unmarshal([]byte(ConfigTomlString), &Config) + //Initialize empty configs tempForCheckMissingConfig := ConfigType{} data, err := os.ReadFile(variable.ConfigFile) @@ -74,7 +80,9 @@ func loadConfigFile() { log.Fatalf("Config file doesn't exist: %v", err) } + // Insert data present in the config file inside temp variable _ = toml.Unmarshal(data, &tempForCheckMissingConfig) + // Replace default values for values specifieds in config file err = toml.Unmarshal(data, &Config) if err != nil && !variable.FixConfigFile { fmt.Print(lipgloss.NewStyle().Foreground(lipgloss.Color("#F93939")).Render("Error") + @@ -83,19 +91,20 @@ func loadConfigFile() { fmt.Println("To add missing fields to hotkeys directory automaticially run Superfile with the --fix-config-file flag `spf --fix-config-file`") } - if !reflect.DeepEqual(Config, tempForCheckMissingConfig) { + // If data is different and FixConfigFile option is on, then fullfill then + // fullfill the config file with the default values + if !reflect.DeepEqual(Config, tempForCheckMissingConfig) && variable.FixConfigFile { tomlData, err := toml.Marshal(Config) if err != nil { log.Fatalf("Error encoding config: %v", err) } - if variable.FixConfigFile { - err = os.WriteFile(variable.ConfigFile, tomlData, 0644) - if err != nil { - log.Fatalf("Error writing config file: %v", err) - } + err = os.WriteFile(variable.ConfigFile, tomlData, 0644) + if err != nil { + log.Fatalf("Error writing config file: %v", err) } } + if (Config.FilePreviewWidth > 10 || Config.FilePreviewWidth < 2) && Config.FilePreviewWidth != 0 { fmt.Println(loadConfigError("file_preview_width")) os.Exit(0) @@ -107,8 +116,10 @@ func loadConfigFile() { } } +// Load and handle keybinds settings in hotkeys toml file func loadHotkeysFile() { + // load default Hotkeys configs _ = toml.Unmarshal([]byte(HotkeysTomlString), &hotkeys) hotkeysFromConfig := HotkeysType{} data, err := os.ReadFile(variable.HotkeysFile) @@ -116,7 +127,9 @@ func loadHotkeysFile() { if err != nil { log.Fatalf("Config file doesn't exist: %v", err) } + // Load data from hotkeys file _ = toml.Unmarshal(data, &hotkeysFromConfig) + // Override default hotkeys with the ones from the file err = toml.Unmarshal(data, &hotkeys) if err != nil { log.Fatalf("Error decoding hotkeys file ( your config file may have misconfigured ): %v", err) @@ -124,6 +137,7 @@ func loadHotkeysFile() { hasMissingHotkeysInConfig := !reflect.DeepEqual(hotkeys, hotkeysFromConfig) + // If FixHotKeys is not on then check if every needed hotkey is properly setted if hasMissingHotkeysInConfig && !variable.FixHotkeys { hotKeysConfig := reflect.ValueOf(hotkeysFromConfig) for i := 0; i < hotKeysConfig.NumField(); i++ { @@ -141,6 +155,7 @@ func loadHotkeysFile() { fmt.Println("To add missing fields to hotkeys directory automaticially run Superfile with the --fix-hotkeys flag `spf --fix-hotkeys`") } + // Override hotkey files with default configs if the Fix flag is on if hasMissingHotkeysInConfig && variable.FixHotkeys { writeHotkeysFile(hotkeys) } @@ -166,6 +181,7 @@ func loadHotkeysFile() { } +// Write hotkeys inside the hotkeys toml file func writeHotkeysFile(hotkeys HotkeysType) { tomlData, err := toml.Marshal(hotkeys) if err != nil { @@ -178,6 +194,7 @@ func writeHotkeysFile(hotkeys HotkeysType) { } } +// Load coonfigurations from theme file func loadThemeFile() { data, err := os.ReadFile(variable.ThemeFolder + "/" + Config.Theme + ".toml") if err != nil { @@ -190,6 +207,7 @@ func loadThemeFile() { } } +// Load all default configurations from superfile_config folder func LoadAllDefaultConfig(content embed.FS) { temp, err := content.ReadFile("src/superfile_config/hotkeys.toml") diff --git a/src/internal/default_config.go b/src/internal/default_config.go index 3c802d5..bb654e9 100644 --- a/src/internal/default_config.go +++ b/src/internal/default_config.go @@ -1,11 +1,13 @@ package internal +// Variables for holding default configurations of each settings var ( HotkeysTomlString string ConfigTomlString string DefaultThemeString string ) +// Generate model containing default configurations func defaultModelConfig(toggleDotFileBool bool, firstFilePanelDir string) model { return model{ filePanelFocusIndex: 0, @@ -57,6 +59,7 @@ func defaultModelConfig(toggleDotFileBool bool, firstFilePanelDir string) model } } +// Return help menu for hotkeys func getHelpMenuData() []helpMenuModalData { data := []helpMenuModalData{ { From 9230e5a2a1a8efa1f00e79ae62c88a6286cf0b68 Mon Sep 17 00:00:00 2001 From: Bruno Llacer Trotti Date: Wed, 13 Nov 2024 21:52:45 -0300 Subject: [PATCH 2/2] improve docs --- src/internal/config_function.go | 14 ++++++++++---- src/internal/default_config.go | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/internal/config_function.go b/src/internal/config_function.go index a5375f3..0c6b128 100644 --- a/src/internal/config_function.go +++ b/src/internal/config_function.go @@ -67,7 +67,9 @@ func initialConfig(dir string) (toggleDotFileBool bool, firstFilePanelDir string return toggleDotFileBool, firstFilePanelDir } -// Load and fix config toml file +// Load configurations from the configuration file. Compares the content +// with the default values and modify the config file to include default configs +// if the FixConfigFile flag is on func loadConfigFile() { //Initialize default configs @@ -116,7 +118,9 @@ func loadConfigFile() { } } -// Load and handle keybinds settings in hotkeys toml file +// Load keybinds from the hotkeys file. Compares the content +// with the default values and modify the hotkeys if the FixHotkeys flag is on. +// If is off check if all hotkeys are properly setted func loadHotkeysFile() { // load default Hotkeys configs @@ -194,7 +198,8 @@ func writeHotkeysFile(hotkeys HotkeysType) { } } -// Load coonfigurations from theme file +// Load configurations from theme file into &theme and return default values +// if file theme folder is empty func loadThemeFile() { data, err := os.ReadFile(variable.ThemeFolder + "/" + Config.Theme + ".toml") if err != nil { @@ -207,7 +212,8 @@ func loadThemeFile() { } } -// Load all default configurations from superfile_config folder +// Load all default configurations from superfile_config folder into global +// configurations variables func LoadAllDefaultConfig(content embed.FS) { temp, err := content.ReadFile("src/superfile_config/hotkeys.toml") diff --git a/src/internal/default_config.go b/src/internal/default_config.go index bb654e9..504f095 100644 --- a/src/internal/default_config.go +++ b/src/internal/default_config.go @@ -7,7 +7,7 @@ var ( DefaultThemeString string ) -// Generate model containing default configurations +// Generate and return model containing default configurations func defaultModelConfig(toggleDotFileBool bool, firstFilePanelDir string) model { return model{ filePanelFocusIndex: 0,