Skip to content

Commit

Permalink
Merge pull request #488 from wassup05/print-last-dir
Browse files Browse the repository at this point in the history
Adds the argument --print-last-dir
  • Loading branch information
yorukot authored Nov 27, 2024
2 parents 8bd8b21 + 98fa1b3 commit ee32d22
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 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
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)
}
}

0 comments on commit ee32d22

Please sign in to comment.