diff --git a/src/cmd/main.go b/src/cmd/main.go index 9129d6ff..912af7bd 100644 --- a/src/cmd/main.go +++ b/src/cmd/main.go @@ -54,6 +54,14 @@ func Run(content embed.FS) { }, }, }, + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "fix-hotkeys", + Aliases: []string{"fh"}, + Usage: "Adds any missing hotkeys to the hotkey config file", + Value: false, + }, + }, Action: func(c *cli.Context) error { path := "" if c.Args().Present() { @@ -62,6 +70,8 @@ func Run(content embed.FS) { InitConfigFile() + varibale.FixHotkeys = c.Bool("fix-hotkeys") + firstUse := checkFirstUse() p := tea.NewProgram(internal.InitialModel(path, firstUse), tea.WithAltScreen(), tea.WithMouseCellMotion()) @@ -69,6 +79,7 @@ func Run(content embed.FS) { log.Fatalf("Alas, there's been an error: %v", err) os.Exit(1) } + CheckForUpdates() return nil }, diff --git a/src/config/fixed_variable.go b/src/config/fixed_variable.go index b3b9f6fc..2575f0bd 100644 --- a/src/config/fixed_variable.go +++ b/src/config/fixed_variable.go @@ -24,6 +24,7 @@ var ( HotkeysFilea string = SuperFileMainDir + "/hotkeys.toml" ToggleDotFilea string = SuperFileDataDir + "/toggleDotFile" LogFilea string = SuperFileStateDir + "/superfile.log" + FixHotkeys bool = false ) const ( diff --git a/src/internal/config_function.go b/src/internal/config_function.go index 9811420c..8c91ceb1 100644 --- a/src/internal/config_function.go +++ b/src/internal/config_function.go @@ -52,7 +52,7 @@ func initialConfig(dir string) (toggleDotFileBool bool, firstFilePanelDir string if dir != "" { firstFilePanelDir, err = filepath.Abs(dir) } else { - Config.DefaultDirectory = strings.Replace(Config.DefaultDirectory, "~", varibale.HomeDir, -1) + Config.DefaultDirectory = strings.Replace(Config.DefaultDirectory, "~", varibale.HomeDir, -1) firstFilePanelDir, err = filepath.Abs(Config.DefaultDirectory) } @@ -90,7 +90,7 @@ func loadConfigFile() { log.Fatalf("Error writing config file: %v", err) } } - if (Config.FilePreviewWidth > 10 || Config.FilePreviewWidth < 2) && Config.FilePreviewWidth != 0{ + if (Config.FilePreviewWidth > 10 || Config.FilePreviewWidth < 2) && Config.FilePreviewWidth != 0 { fmt.Println(loadConfigError("file_preview_width")) os.Exit(0) } @@ -104,29 +104,37 @@ func loadConfigFile() { func loadHotkeysFile() { _ = toml.Unmarshal([]byte(HotkeysTomlString), &hotkeys) - tempForCheckMissingConfig := HotkeysType{} + hotkeysFromConfig := HotkeysType{} data, err := os.ReadFile(varibale.HotkeysFilea) if err != nil { log.Fatalf("Config file doesn't exist: %v", err) } - - _ = toml.Unmarshal(data, &tempForCheckMissingConfig) + _ = toml.Unmarshal(data, &hotkeysFromConfig) err = toml.Unmarshal(data, &hotkeys) if err != nil { log.Fatalf("Error decoding hotkeys file ( your config file may have misconfigured ): %v", err) } - if !reflect.DeepEqual(hotkeys, tempForCheckMissingConfig) { - tomlData, err := toml.Marshal(hotkeys) - if err != nil { - log.Fatalf("Error encoding hotkeys: %v", err) - } + hasMissingHotkeysInConfig := reflect.DeepEqual(hotkeys, hotkeysFromConfig) == false - err = os.WriteFile(varibale.HotkeysFilea, tomlData, 0644) - if err != nil { - log.Fatalf("Error writing hotkeys file: %v", err) + if hasMissingHotkeysInConfig && varibale.FixHotkeys == false { + hotKeysConfig := reflect.ValueOf(hotkeysFromConfig) + for i := 0; i < hotKeysConfig.NumField(); i++ { + field := hotKeysConfig.Type().Field(i) + value := hotKeysConfig.Field(i) + name := field.Name + isMissing := value.Len() == 0 + + if isMissing { + fmt.Printf("Field \"%s\" is missing in hotkeys configuration\n", name) + } } + fmt.Println("To add missing fields to hotkeys directory automaticially run Superfile with the --fix-hotkeys flag") + } + + if hasMissingHotkeysInConfig && varibale.FixHotkeys { + writeHotkeysFile(hotkeys) } val := reflect.ValueOf(hotkeys) @@ -142,12 +150,24 @@ func loadHotkeysFile() { hotkeysList := value.Interface().([]string) - if len(hotkeysList) == 0 || hotkeysList[0] == "" { + if len(hotkeysList) == 0 || hotkeysList[0] == "" { fmt.Println(lodaHotkeysError(field.Name)) os.Exit(0) } } - + +} + +func writeHotkeysFile(hotkeys HotkeysType) { + tomlData, err := toml.Marshal(hotkeys) + if err != nil { + log.Fatalf("Error encoding hotkeys: %v", err) + } + + err = os.WriteFile(varibale.HotkeysFilea, tomlData, 0644) + if err != nil { + log.Fatalf("Error writing hotkeys file: %v", err) + } } func loadThemeFile() {