diff --git a/app.go b/app.go index 16da2351..fe574f56 100644 --- a/app.go +++ b/app.go @@ -4,8 +4,6 @@ import ( "fmt" "log" "os" - "os/exec" - "runtime" "strconv" "strings" ) @@ -38,20 +36,7 @@ func newApp() *app { func waitKey() error { // TODO: this should be done with termbox somehow - c := `echo - echo -n 'Press any key to continue' - old=$(stty -g) - stty raw -echo - eval "ignore=\$(dd bs=1 count=1 2> /dev/null)" - stty $old - echo` - - var cmd *exec.Cmd - if runtime.GOOS == "windows" { - cmd = exec.Command("cmd", "/c", "pause") - } else { - cmd = exec.Command(gOpts.shell, "-c", c) - } + cmd := pauseCommand() cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout @@ -136,17 +121,7 @@ func (app *app) exportVars() { func (app *app) runShell(s string, args []string, wait bool, async bool) { app.exportVars() - if len(gOpts.ifs) != 0 { - s = fmt.Sprintf("IFS='%s'; %s", gOpts.ifs, s) - } - - if runtime.GOOS == "windows" { - args = append([]string{"/c", s}, args...) - } else { - args = append([]string{"-c", s, "--"}, args...) - } - - cmd := exec.Command(gOpts.shell, args...) + cmd := shellCommand(s, args) if !async { cmd.Stdin = os.Stdin diff --git a/main.go b/main.go index cc9b0505..68efd635 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ import ( "os" "os/exec" "path/filepath" - "runtime" "runtime/pprof" ) @@ -48,15 +47,10 @@ func init() { envConfig = filepath.Join(envHome, ".config") } - tmp := os.TempDir() + gSocketProt = gDefaultSocketProt + gSocketPath = gDefaultSocketPath - if runtime.GOOS == "windows" { - gSocketProt = "tcp" - gSocketPath = ":12345" - } else { - gSocketProt = "unix" - gSocketPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.sock", envUser)) - } + tmp := os.TempDir() gClientID = 1000 gLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.%d.log", envUser, gClientID)) diff --git a/nav.go b/nav.go index f6f8e6a6..287c959a 100644 --- a/nav.go +++ b/nav.go @@ -5,9 +5,7 @@ import ( "fmt" "log" "os" - "os/exec" "path/filepath" - "runtime" "sort" "strings" ) @@ -513,44 +511,11 @@ func (nav *nav) put() error { dir := nav.currDir() - var sh string - var args []string + cmd := putCommand(list, dir, copy) - if runtime.GOOS == "windows" { - sh = "robocopy" - if !copy { - args = []string{"/move"} - } - for _, f := range list { - stat, err := os.Stat(f) - if err != nil { - log.Printf("getting file information: %s", err) - continue - } - base := filepath.Base(f) - dest := filepath.Dir(f) - if stat.IsDir() { - exec.Command(sh, append(args, f, filepath.Join(dir.path, base))...).Run() - } else { - exec.Command(sh, append(args, dest, dir.path, base)...).Run() - } - } - } else { - if copy { - sh = "cp" - args = append(args, "-r") - } else { - sh = "mv" - } - args = append(args, "--backup=numbered") - args = append(args, list...) - args = append(args, dir.path) - - cmd := exec.Command(sh, args...) - - if err := cmd.Run(); err != nil { - return fmt.Errorf("%s: %s", sh, err) - } + if err := cmd.Run(); err != nil { + // TODO: add name of external command to message + return fmt.Errorf("putting files: %s", err) } // TODO: async? diff --git a/opts.go b/opts.go index a6b79a8b..a52d69ce 100644 --- a/opts.go +++ b/opts.go @@ -1,9 +1,6 @@ package main -import ( - "runtime" - "time" -) +import "time" var gOpts struct { dircounts bool @@ -43,11 +40,7 @@ func init() { gOpts.scrolloff = 0 gOpts.tabstop = 8 gOpts.filesep = ":" - if runtime.GOOS == "windows" { - gOpts.shell = "cmd" - } else { - gOpts.shell = "/bin/sh" - } + gOpts.shell = gDefaultShell gOpts.sortby = "natural" gOpts.timefmt = time.ANSIC gOpts.ratios = []int{1, 2, 3} diff --git a/os.go b/os.go new file mode 100644 index 00000000..70f8ad67 --- /dev/null +++ b/os.go @@ -0,0 +1,57 @@ +// +build !windows + +package main + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" +) + +var gDefaultShell = "/bin/sh" +var gDefaultSocketProt = "unix" +var gDefaultSocketPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.sock", envUser)) + +func pauseCommand() *exec.Cmd { + c := `echo + echo -n 'Press any key to continue' + old=$(stty -g) + stty raw -echo + eval "ignore=\$(dd bs=1 count=1 2> /dev/null)" + stty $old + echo` + + return exec.Command(gOpts.shell, "-c", c) +} + +func shellCommand(s string, args []string) *exec.Cmd { + if len(gOpts.ifs) != 0 { + s = fmt.Sprintf("IFS='%s'; %s", gOpts.ifs, s) + } + + args = append([]string{"-c", s, "--"}, args...) + + return exec.Command(gOpts.shell, args...) +} + +func putCommand(list []string, dir *dir, copy bool) *exec.Cmd { + var sh string + var args []string + + if copy { + sh = "cp" + args = append(args, "-r") + } else { + sh = "mv" + } + args = append(args, "--backup=numbered") + args = append(args, list...) + args = append(args, dir.path) + + return exec.Command(sh, args...) +} + +func moveCursor(y, x int) { + fmt.Printf("\033[%d;%dH", y, x) +} diff --git a/os_windows.go b/os_windows.go new file mode 100644 index 00000000..7d8c6f34 --- /dev/null +++ b/os_windows.go @@ -0,0 +1,54 @@ +package main + +import ( + "log" + "os" + "os/exec" + "path/filepath" +) + +var gDefaultShell = "cmd" +var gDefaultSocketProt = "tcp" +var gDefaultSocketPath = ":12345" + +func pauseCommand() *exec.Cmd { + return exec.Command("cmd", "/c", "pause") +} + +func shellCommand(s string, args []string) *exec.Cmd { + args = append([]string{"/c", s}, args...) + return exec.Command(gOpts.shell, args...) +} + +func putCommand(list []string, dir *dir, copy bool) *exec.Cmd { + var args []string + + sh := "robocopy" + if !copy { + args = []string{"/move"} + } + for _, f := range list { + stat, err := os.Stat(f) + if err != nil { + log.Printf("getting file information: %s", err) + continue + } + base := filepath.Base(f) + dest := filepath.Dir(f) + if stat.IsDir() { + exec.Command(sh, append(args, f, filepath.Join(dir.path, base))...).Run() + } else { + exec.Command(sh, append(args, dest, dir.path, base)...).Run() + } + } + + // TODO: add backup options + // TODO: return 0 on success + + return exec.Command(sh, args...) +} + +func moveCursor(y, x int) { + // TODO: implement + return +} diff --git a/ui.go b/ui.go index 6f6e0274..4f5b5edf 100644 --- a/ui.go +++ b/ui.go @@ -9,7 +9,6 @@ import ( "os" "os/exec" "path/filepath" - "runtime" "sort" "strconv" "strings" @@ -515,14 +514,6 @@ func (ui *ui) loadFile(nav *nav) { } } -func moveCursor(y, x int) { - // TODO: implement for windows - if runtime.GOOS == "windows" { - return - } - fmt.Printf("\033[%d;%dH", y, x) -} - func (ui *ui) draw(nav *nav) { fg, bg := termbox.ColorDefault, termbox.ColorDefault