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 30, 2024
2 parents a46f750 + b6eaebb commit 974d3be
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 55 deletions.
31 changes: 20 additions & 11 deletions src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,25 @@ func Run(content embed.FS) {
Usage: "Adds any missing hotkeys to the hotkey config file",
Value: false,
},
&cli.BoolFlag{
Name: "print-last-dir",
Aliases: []string{"pld"},
Usage: "Print the last dir to stdout on exit (to use for cd)",
Value: false,
},
},
Action: func(c *cli.Context) error {
// If no args are called along with "spf" use current dir
// 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()

variable.FixHotkeys = c.Bool("fix-hotkeys")
variable.FixConfigFile = c.Bool("fix-config-file")
variable.PrintLastDir = c.Bool("print-last-dir")

firstUse := checkFirstUse()

Expand All @@ -80,10 +87,13 @@ func Run(content embed.FS) {
log.Fatalf("Alas, there's been an error: %v", err)
}

if variable.PrintLastDir {
fmt.Println(variable.LastDir)
}

CheckForUpdates()
return nil
},

}

err := app.Run(os.Args)
Expand All @@ -92,7 +102,7 @@ func Run(content embed.FS) {
}
}

// Create proper directories for storing configuration and write default
// Create proper directories for storing configuration and write default
// configurations to Config and Hotkeys toml
func InitConfigFile() {
// Create directories
Expand Down Expand Up @@ -167,7 +177,7 @@ func createFiles(files ...string) error {
return nil
}

// Check if is the first time initializing the app, if it is create
// Check if is the first time initializing the app, if it is create
// use check file
func checkFirstUse() bool {
file := variable.FirstUseCheck
Expand All @@ -192,13 +202,12 @@ func writeConfigFile(path, data string) error {
}

// Check for the need of updates if AutoCheckUpdate is on, if its the first time
//that version is checked or if has more than 24h since the last version check,
//look into the repo if there's any more recent version
// that version is checked or if has more than 24h since the last version check,
// look into the repo if there's any more recent version
func CheckForUpdates() {
var Config internal.ConfigType


// Get AutoCheck flag from configuration files
// Get AutoCheck flag from configuration files
data, err := os.ReadFile(variable.ConfigFile)
if err != nil {
log.Fatalf("Config file doesn't exist: %v", err)
Expand All @@ -213,7 +222,7 @@ func CheckForUpdates() {
return
}

// Check last time the version was checked
// Check last time the version was checked
lastTime, err := readLastTimeCheckVersionFromFile(variable.LastCheckVersion)
if err != nil && !os.IsNotExist(err) {
fmt.Println("Error reading from file:", err)
Expand Down Expand Up @@ -244,7 +253,7 @@ func CheckForUpdates() {
return
}

//Check if the local version is outdated
//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
2 changes: 2 additions & 0 deletions src/config/fixed_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var (
LogFile string = SuperFileStateDir + "/superfile.log"
FixHotkeys bool = false
FixConfigFile bool = false
LastDir string = ""
PrintLastDir bool = false
)

const (
Expand Down
35 changes: 10 additions & 25 deletions src/internal/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func returnFolderElement(location string, displayDotFile bool, sortOptions sortO
switch sortOptions.options[sortOptions.selected] {
case "Name":
order = func(i, j int) bool {
_, errI := os.ReadDir(location+"/"+files[i].Name());
_, errJ := os.ReadDir(location+"/"+files[j].Name());
if (files[i].IsDir()||errI==nil) && (!files[j].IsDir()&&errJ!=nil) {
_, errI := os.ReadDir(location + "/" + files[i].Name())
_, errJ := os.ReadDir(location + "/" + files[j].Name())
if (files[i].IsDir() || errI == nil) && (!files[j].IsDir() && errJ != nil) {
return true
}
if (!files[i].IsDir()&&errI!=nil) && (files[j].IsDir()||errJ==nil) {
if (!files[i].IsDir() && errI != nil) && (files[j].IsDir() || errJ == nil) {
return false
}
if Config.CaseSensitiveSort {
Expand All @@ -79,13 +79,13 @@ func returnFolderElement(location string, displayDotFile bool, sortOptions sortO
// Files sorted by size
fileInfoI, _ := files[i].Info()
fileInfoJ, _ := files[j].Info()
_, errI := os.ReadDir(location+"/"+files[i].Name());
_, errJ := os.ReadDir(location+"/"+files[j].Name());
_, errI := os.ReadDir(location + "/" + files[i].Name())
_, errJ := os.ReadDir(location + "/" + files[j].Name())

if (files[i].IsDir()||errI==nil) && (!files[j].IsDir()&&errJ!=nil) {
if (files[i].IsDir() || errI == nil) && (!files[j].IsDir() && errJ != nil) {
return true
}
if (!files[i].IsDir()&&errI!=nil) && (files[j].IsDir()||errJ==nil) {
if (!files[i].IsDir() && errI != nil) && (files[j].IsDir() || errJ == nil) {
return false
}
if files[i].IsDir() && files[j].IsDir() {
Expand Down Expand Up @@ -351,7 +351,8 @@ func (m *model) returnMetaData() {
fileInfo, err := os.Stat(filePath)

if isSymlink(filePath) {
if isBrokenSymlink(filePath) {
_, err := filepath.EvalSymlinks(filePath)
if err != nil {
m.fileMetaData.metaData = append(m.fileMetaData.metaData, [2]string{"Link file is broken!", ""})
} else {
m.fileMetaData.metaData = append(m.fileMetaData.metaData, [2]string{"This is a link file.", ""})
Expand Down Expand Up @@ -473,22 +474,6 @@ func countFiles(dirPath string) (int, error) {
return count, err
}

// Check whether is broken recursive symlinks
func isBrokenSymlink(filePath string) bool {
linkPath, err := os.Readlink(filePath)
if err != nil {
return true
}

absLinkPath, err := filepath.Abs(linkPath)
if err != nil {
return true
}

_, err = os.Stat(absLinkPath)
return err != nil
}

// Check whether is symlinks
func isSymlink(filePath string) bool {

Expand Down
24 changes: 20 additions & 4 deletions src/internal/handle_panel_movement.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,41 @@ func (m *model) enterPanel() {
}

if fileInfo.Mode()&os.ModeSymlink != 0 {
if isBrokenSymlink(panel.element[panel.cursor].location) {
targetPath, err := filepath.EvalSymlinks(panel.element[panel.cursor].location)
if err != nil {
return
}

linkPath, _ := os.Readlink(panel.element[panel.cursor].location)
targetInfo, err := os.Lstat(targetPath)

absLinkPath, err := filepath.Abs(linkPath)
if err != nil {
return
}

m.fileModel.filePanels[m.filePanelFocusIndex].location = absLinkPath
if targetInfo.IsDir() {
m.fileModel.filePanels[m.filePanelFocusIndex].location = targetPath
}

return
}

openCommand := "xdg-open"
if runtime.GOOS == "darwin" {
openCommand = "open"
} else if runtime.GOOS == "windows" {

dllpath := filepath.Join(os.Getenv("SYSTEMROOT"), "System32", "rundll32.exe")
dllfile := "url.dll,FileProtocolHandler"

cmd := exec.Command(dllpath, dllfile, panel.element[panel.cursor].location)
err = cmd.Start()
if err != nil {
outPutLog(fmt.Sprintf("err when open file with %s", openCommand), err)
}

return
}

cmd := exec.Command(openCommand, panel.element[panel.cursor].location)
err = cmd.Start()
if err != nil {
Expand Down
30 changes: 16 additions & 14 deletions src/internal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,18 +418,20 @@ func (m *model) getFilePanelItems() {
// Close superfile application. Cd into the curent dir if CdOnQuit on and save
// the path in state direcotory
func (m model) quitSuperfile() {
// close exiftool session
if Config.Metadata {
et.Close()
}
// cd on quit
if Config.CdOnQuit {
currentDir := m.fileModel.filePanels[m.filePanelFocusIndex].location
if currentDir == variable.HomeDir {
return
}
// escape single quote
currentDir = strings.ReplaceAll(currentDir, "'", "'\\''")
os.WriteFile(variable.SuperFileStateDir+"/lastdir", []byte("cd '"+currentDir+"'"), 0755)
}
// close exiftool session
if Config.Metadata {
et.Close();
}
// cd on quit
currentDir := m.fileModel.filePanels[m.filePanelFocusIndex].location
variable.LastDir = currentDir

if Config.CdOnQuit {
if currentDir == variable.HomeDir {
return
}
// escape single quote
currentDir = strings.ReplaceAll(currentDir, "'", "'\\''")
os.WriteFile(variable.SuperFileStateDir+"/lastdir", []byte("cd '"+currentDir+"'"), 0755)
}
}
2 changes: 1 addition & 1 deletion src/internal/model_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func (m model) typineModalRender() string {
}

func (m model) introduceModalRender() string {
title := sidebarTitleStyle.Render(" Thanks for use superfile!!") + modalStyle.Render("\n You can read the following information before starting to use it!")
title := sidebarTitleStyle.Render(" Thanks for using superfile!!") + modalStyle.Render("\n You can read the following information before starting to use it!")
vimUserWarn := processErrorStyle.Render(" ** Very importantly ** If you are a Vim/Nvim user, go to:\n https://superfile.netlify.app/configure/custom-hotkeys/ to change your hotkey settings!")
subOne := sidebarTitleStyle.Render(" (1)") + modalStyle.Render(" If this is your first time, make sure you read:\n https://superfile.netlify.app/getting-started/tutorial/")
subTwo := sidebarTitleStyle.Render(" (2)") + modalStyle.Render(" If you forget the relevant keys during use,\n you can press \"?\" (shift+/) at any time to query the keys!")
Expand Down

0 comments on commit 974d3be

Please sign in to comment.