Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/yorukot/superfile
Browse files Browse the repository at this point in the history
  • Loading branch information
yorukot committed Nov 14, 2024
2 parents 6931195 + 0b56008 commit fedc687
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
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
33 changes: 18 additions & 15 deletions website/src/content/docs/getting-started/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,38 @@ First make sure you have the following tools installed on your machine:
- [Any Nerd-font ](https://www.nerdfonts.com/font-downloads)

:::tip
If you don't install `Nerd font` superfile it will still work, but the UI may be a bit ugly.
If you don't install `Nerd font`, superfile will still work, but the UI may be a bit ugly.
:::

## Installation Script
## Installation Scripts

Copy and paste the following one-line command into your machine's terminal.

### Linux / MacOs
Just copy and paste this one-line command:

With `curl`:

```bash
bash -c "$(curl -sLo- https://superfile.netlify.app/install.sh)"
```
Or wget:

Or with `wget`:
```bash
bash -c "$(wget -qO- https://superfile.netlify.app/install.sh)"
```

## Windows
### Windows

It actually supports windows! Well.. sort of.

Use powershell to run this command:
With `powershell`:

```bash
powershell -ExecutionPolicy Bypass -Command "Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://superfile.netlify.app/install.ps1'))"
```
:::note
For uninstall do the same but uninstall.ps1
:::
To uninstall, run the above `powershell` command with the modified URL:

`https://superfile.netlify.app/uninstall.ps1`
:::

## Community maintained packages

Expand All @@ -57,15 +60,15 @@ For uninstall do the same but uninstall.ps1
sudo pacman -S superfile
```

###### Fetches prebuilt binaries from github
###### Fetches prebuilt binaries from GitHub

```bash
sudo pacman -S superfile-bin
```

### Homebrew

Install homebrew and execute the following commands
Install [Homebrew](https://brew.sh/) and then run the following command:

```bash
brew install superfile
Expand Down Expand Up @@ -113,15 +116,15 @@ x env use superfile

## Start superfile

After completing the installation, you can restart the terminal (if necessary)
After completing the installation, you can restart the terminal (if necessary).

You can use `spf` to start superfile
Run `spf` to start superfile

```bash
spf
```

## Next-step
## Next steps

- [Tutorial](/getting-started/tutorial)
- [Hotkey list](/list/hotkey-list)

0 comments on commit fedc687

Please sign in to comment.