Skip to content

Commit

Permalink
add fix-hotkeys flag
Browse files Browse the repository at this point in the history
- adds fix-hotkeys flag
- no longer writes to hotkeys config every time Superfile is ran
- adds feedback to users for missing hotkey entries for Superfile
- adds directions on how to fix hotkey file using --fix-hotkeys flag
  • Loading branch information
Buttars committed Aug 10, 2024
1 parent 2a84d04 commit c0681eb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
11 changes: 11 additions & 0 deletions src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -62,13 +70,16 @@ 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())
if _, err := p.Run(); err != nil {
log.Fatalf("Alas, there's been an error: %v", err)
os.Exit(1)
}

CheckForUpdates()
return nil
},
Expand Down
1 change: 1 addition & 0 deletions src/config/fixed_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
HotkeysFilea string = SuperFileMainDir + "/hotkeys.toml"
ToggleDotFilea string = SuperFileDataDir + "/toggleDotFile"
LogFilea string = SuperFileStateDir + "/superfile.log"
FixHotkeys bool = false
)

const (
Expand Down
51 changes: 36 additions & 15 deletions src/internal/config_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -104,31 +104,40 @@ 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)
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)
}
}

if hasMissingHotkeysInConfig {
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)

for i := 0; i < val.NumField(); i++ {
Expand All @@ -142,12 +151,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() {
Expand Down

0 comments on commit c0681eb

Please sign in to comment.