Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better documentation #462

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 ") +
Expand Down
36 changes: 30 additions & 6 deletions src/internal/config_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -64,17 +67,24 @@ func initialConfig(dir string) (toggleDotFileBool bool, firstFilePanelDir string
return toggleDotFileBool, firstFilePanelDir
}

// 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
_ = toml.Unmarshal([]byte(ConfigTomlString), &Config)
//Initialize empty configs
tempForCheckMissingConfig := ConfigType{}

data, err := os.ReadFile(variable.ConfigFile)
if err != nil {
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") +
Expand All @@ -83,19 +93,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)
Expand All @@ -107,23 +118,30 @@ func loadConfigFile() {
}
}

// 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
_ = toml.Unmarshal([]byte(HotkeysTomlString), &hotkeys)
hotkeysFromConfig := HotkeysType{}
data, err := os.ReadFile(variable.HotkeysFile)

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)
}

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++ {
Expand All @@ -141,6 +159,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)
}
Expand All @@ -166,6 +185,7 @@ func loadHotkeysFile() {

}

// Write hotkeys inside the hotkeys toml file
func writeHotkeysFile(hotkeys HotkeysType) {
tomlData, err := toml.Marshal(hotkeys)
if err != nil {
Expand All @@ -178,6 +198,8 @@ func writeHotkeysFile(hotkeys HotkeysType) {
}
}

// 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 {
Expand All @@ -190,6 +212,8 @@ func loadThemeFile() {
}
}

// 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")
Expand Down
3 changes: 3 additions & 0 deletions src/internal/default_config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package internal

// Variables for holding default configurations of each settings
var (
HotkeysTomlString string
ConfigTomlString string
DefaultThemeString string
)

// Generate and return model containing default configurations
func defaultModelConfig(toggleDotFileBool bool, firstFilePanelDir string) model {
return model{
filePanelFocusIndex: 0,
Expand Down Expand Up @@ -57,6 +59,7 @@ func defaultModelConfig(toggleDotFileBool bool, firstFilePanelDir string) model
}
}

// Return help menu for hotkeys
func getHelpMenuData() []helpMenuModalData {
data := []helpMenuModalData{
{
Expand Down