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 31d00f8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 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
53 changes: 40 additions & 13 deletions src/internal/config_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,47 @@ func loadConfigFile() {

func loadHotkeysFile() {

// 1. Load the defaults into hotkeys
// 2. Read file data
// 3. Decode toml data and overlay onto hotkeys (with default) and tempForCheckMissingConfig (no defaults)
// 4. Check if hotkeys (defaults + custom) are equal to tempForCheckingMissingConfig (only custom) and encode the hotkeys if they are not
// Check if the contents of the config contain all of the keys

_ = 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 +157,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 31d00f8

Please sign in to comment.