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 Jan 27, 2025
2 parents b55d396 + 05df716 commit 18b58dd
Show file tree
Hide file tree
Showing 25 changed files with 875 additions and 984 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pnpm-debug.log*
# environment variables
.env
.env.production
.direnv

# macOS-specific files
.DS_Store
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ Add the binary file to your $PATH, e.g., in `/usr/local/bin`:
```bash
sudo mv ./bin/spf /usr/local/bin
```
## Start Superfile

```bash
spf
```

## Supported Systems

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func checkFirstUse() bool {
if _, err := os.Stat(file); os.IsNotExist(err) {
firstUse = true
if err := os.WriteFile(file, nil, 0644); err != nil {
log.Fatalln("failed to create file: %w", err)
log.Fatalf("Failed to create file: %v", err)
}
}
return firstUse
Expand Down
52 changes: 31 additions & 21 deletions src/internal/config_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package internal
import (
"embed"
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"

"github.com/barasher/go-exiftool"
Expand All @@ -19,16 +20,29 @@ import (
// 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, toggleFooter 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)
file, err := os.OpenFile(variable.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)

// Todo : This could be improved if we want to make superfile more resilient to errors
// For example if the log file directories have access issues.
// we could pass a dummy object to log.SetOutput() and the app would still function.
if err != nil {
log.Fatalf("Error while opening superfile.log file: %v", err)
// At this point, it will go to stdout since log file is not initilized
LogAndExit("Error while opening superfile.log file", "error", err)
}

loadConfigFile()

logLevel := slog.LevelInfo
if Config.Debug {
logLevel = slog.LevelDebug
}

slog.SetDefault(slog.New(slog.NewTextHandler(
file, &slog.HandlerOptions{Level: logLevel})))

slog.Debug("Runtime information", "runtime.GOOS", runtime.GOOS)

loadHotkeysFile()

loadThemeFile()
Expand Down Expand Up @@ -94,7 +108,7 @@ func loadConfigFile() {

data, err := os.ReadFile(variable.ConfigFile)
if err != nil {
log.Fatalf("Config file doesn't exist: %v", err)
LogAndExit("Config file doesn't exist", "error", err)
}

// Insert data present in the config file inside temp variable
Expand All @@ -113,23 +127,21 @@ func loadConfigFile() {
if !reflect.DeepEqual(Config, tempForCheckMissingConfig) && variable.FixConfigFile {
tomlData, err := toml.Marshal(Config)
if err != nil {
log.Fatalf("Error encoding config: %v", err)
LogAndExit("Error encoding config", "error", err)
}

err = os.WriteFile(variable.ConfigFile, tomlData, 0644)
if err != nil {
log.Fatalf("Error writing config file: %v", err)
LogAndExit("Error writing config file", "error", err)
}
}

if (Config.FilePreviewWidth > 10 || Config.FilePreviewWidth < 2) && Config.FilePreviewWidth != 0 {
fmt.Println(loadConfigError("file_preview_width"))
os.Exit(0)
LogAndExit(loadConfigError("file_preview_width"))
}

if Config.SidebarWidth != 0 && (Config.SidebarWidth < 3 || Config.SidebarWidth > 20) {
fmt.Println(loadConfigError("sidebar_width"))
os.Exit(0)
LogAndExit(loadConfigError("sidebar_width"))
}
}

Expand All @@ -144,14 +156,14 @@ func loadHotkeysFile() {
data, err := os.ReadFile(variable.HotkeysFile)

if err != nil {
log.Fatalf("Config file doesn't exist: %v", err)
LogAndExit("Config file doesn't exist", "error", 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)
LogAndExit("Error decoding hotkeys file ( your config file may have misconfigured", "error", err)
}

hasMissingHotkeysInConfig := !reflect.DeepEqual(hotkeys, hotkeysFromConfig)
Expand Down Expand Up @@ -186,15 +198,13 @@ func loadHotkeysFile() {
value := val.Field(i)

if value.Kind() != reflect.Slice || value.Type().Elem().Kind() != reflect.String {
fmt.Println(lodaHotkeysError(field.Name))
os.Exit(0)
LogAndExit(loadHotkeysError(field.Name))
}

hotkeysList := value.Interface().([]string)

if len(hotkeysList) == 0 || hotkeysList[0] == "" {
fmt.Println(lodaHotkeysError(field.Name))
os.Exit(0)
LogAndExit(loadHotkeysError(field.Name))
}
}

Expand All @@ -204,12 +214,12 @@ func loadHotkeysFile() {
func writeHotkeysFile(hotkeys HotkeysType) {
tomlData, err := toml.Marshal(hotkeys)
if err != nil {
log.Fatalf("Error encoding hotkeys: %v", err)
LogAndExit("Error encoding hotkeys", "error", err)
}

err = os.WriteFile(variable.HotkeysFile, tomlData, 0644)
if err != nil {
log.Fatalf("Error writing hotkeys file: %v", err)
LogAndExit("Error writing hotkeys file", "error", err)
}
}

Expand All @@ -223,7 +233,7 @@ func loadThemeFile() {

err = toml.Unmarshal(data, &theme)
if err != nil {
log.Fatalf("Error while decoding theme file( Your theme file may have errors ): %v", err)
LogAndExit("Error while decoding theme file( Your theme file may have errors", "error", err)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/internal/config_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ type ConfigType struct {
DefaultSortType int `toml:"default_sort_type" comment:"\nDefault sort type (0: Name, 1: Size, 2: Date Modified)."`
SortOrderReversed bool `toml:"sort_order_reversed" comment:"\nDefault sort order (false: Ascending, true: Descending)."`
CaseSensitiveSort bool `toml:"case_sensitive_sort" comment:"\nCase sensitive sort by name (captal \"B\" comes before \"a\" if true)."`
Debug bool `toml:"debug" comment:"\nWhether to enable debug mode."`

Nerdfont bool `toml:"nerdfont" comment:"\n================ Style =================\n\n If you don't have or don't want Nerdfont installed you can turn this off"`
TransparentBackground bool `toml:"transparent_background" comment:"\nSet transparent background or not (this only work when your terminal background is transparent)"`
Expand Down
15 changes: 9 additions & 6 deletions src/internal/file_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,24 @@ func trashMacOrLinux(src string) error {
err := moveElement(src, filepath.Join(variable.HomeDir, ".Trash", filepath.Base(src)))
if err != nil {
outPutLog("Delete single item function move file to trash can error", err)
return err
}
} else {
err := trash.Trash(src)
if err != nil {
outPutLog("Paste item function move file to trash can error", err)
return err
}
}
return nil
}

// pasteDir handles directory copying with progress tracking
func pasteDir(src, dst string, id string, m model) (model, error) {
// model would only have changes in m.processBarModel.process[id]
func pasteDir(src, dst string, id string, m *model) (error) {
dst, err := renameIfDuplicate(dst)
if err != nil {
return m, err
return err
}

// Check if we can do a fast move within the same partition
Expand All @@ -170,7 +173,7 @@ func pasteDir(src, dst string, id string, m model) (model, error) {
// For cut operations on same partition, try fast rename first
err = os.Rename(src, dst)
if err == nil {
return m, nil
return nil
}
// If rename fails, fall back to manual copy
}
Expand Down Expand Up @@ -239,16 +242,16 @@ func pasteDir(src, dst string, id string, m model) (model, error) {
})

if err != nil {
return m, err
return err
}

// If this was a cut operation and we had to do a manual copy, remove the source
if m.copyItems.cut && !sameDev {
err = os.RemoveAll(src)
if err != nil {
return m, fmt.Errorf("failed to remove source after move: %v", err)
return fmt.Errorf("failed to remove source after move: %v", err)
}
}

return m, nil
return nil
}
14 changes: 9 additions & 5 deletions src/internal/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/hex"
"fmt"
"io"
"log"
"log/slog"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -211,11 +211,15 @@ func arrayContains(s []string, str string) bool {
return false
}

// Todo : Eventually we will replace all calls to direct slog calls
func outPutLog(values ...interface{}) {
log.SetOutput(logOutput)
for _, value := range values {
log.Println(value)
}
slog.Info(fmt.Sprintln(values...))
}

// Todo : Eventually we want to remove all such usage that can result in app exiting abruptly
func LogAndExit(msg string, values ...any) {
slog.Error(msg, values...)
os.Exit(1)
}

func removeElementByValue(slice []string, value string) []string {
Expand Down
Loading

0 comments on commit 18b58dd

Please sign in to comment.