2017-08-05 16:23:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-03-17 17:16:19 +00:00
|
|
|
"fmt"
|
2017-08-05 16:23:55 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-08-06 08:05:46 +00:00
|
|
|
"os/user"
|
2017-08-05 16:23:55 +00:00
|
|
|
"path/filepath"
|
2017-08-06 08:05:46 +00:00
|
|
|
"strings"
|
2018-07-31 21:21:55 +00:00
|
|
|
"syscall"
|
2017-08-05 16:23:55 +00:00
|
|
|
)
|
|
|
|
|
2018-06-28 20:37:01 +00:00
|
|
|
var (
|
|
|
|
envOpener = os.Getenv("OPENER")
|
|
|
|
envEditor = os.Getenv("EDITOR")
|
|
|
|
envPager = os.Getenv("PAGER")
|
|
|
|
envShell = os.Getenv("SHELL")
|
|
|
|
)
|
|
|
|
|
2018-06-28 21:12:54 +00:00
|
|
|
var (
|
|
|
|
envPathExt = os.Getenv("PATHEXT")
|
|
|
|
)
|
|
|
|
|
2017-08-06 08:05:46 +00:00
|
|
|
var (
|
|
|
|
gDefaultShell = "cmd"
|
|
|
|
gDefaultSocketProt = "tcp"
|
|
|
|
gDefaultSocketPath = ":12345"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-06-28 16:37:19 +00:00
|
|
|
gUser *user.User
|
|
|
|
gConfigPaths []string
|
2018-07-11 17:09:26 +00:00
|
|
|
gMarksPath string
|
2018-07-11 17:49:55 +00:00
|
|
|
gHistoryPath string
|
2017-08-06 08:05:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-06-28 20:37:01 +00:00
|
|
|
if envOpener == "" {
|
2019-03-17 17:16:19 +00:00
|
|
|
envOpener = `start ""`
|
2018-06-28 20:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if envEditor == "" {
|
|
|
|
envEditor = "notepad"
|
|
|
|
}
|
|
|
|
|
|
|
|
if envPager == "" {
|
|
|
|
envPager = "more"
|
|
|
|
}
|
|
|
|
|
|
|
|
if envShell == "" {
|
|
|
|
envShell = "cmd"
|
|
|
|
}
|
|
|
|
|
2017-11-19 18:55:13 +00:00
|
|
|
u, err := user.Current()
|
2017-08-06 08:05:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("user: %s", err)
|
|
|
|
}
|
2017-11-19 18:55:13 +00:00
|
|
|
gUser = u
|
2017-08-06 08:05:46 +00:00
|
|
|
|
|
|
|
// remove domain prefix
|
|
|
|
gUser.Username = strings.Split(gUser.Username, `\`)[1]
|
|
|
|
|
2018-07-11 17:49:55 +00:00
|
|
|
data := os.Getenv("LOCALAPPDATA")
|
|
|
|
|
2018-06-28 16:45:59 +00:00
|
|
|
gConfigPaths = []string{
|
|
|
|
filepath.Join(os.Getenv("ProgramData"), "lf", "lfrc"),
|
2018-07-11 17:49:55 +00:00
|
|
|
filepath.Join(data, "lf", "lfrc"),
|
2018-06-28 16:45:59 +00:00
|
|
|
}
|
2018-07-11 17:09:26 +00:00
|
|
|
|
2018-07-11 17:49:55 +00:00
|
|
|
gMarksPath = filepath.Join(data, "lf", "marks")
|
|
|
|
gHistoryPath = filepath.Join(data, "lf", "history")
|
2019-03-17 16:52:34 +00:00
|
|
|
|
|
|
|
exportEnvVars()
|
2017-08-06 08:05:46 +00:00
|
|
|
}
|
2017-08-05 16:23:55 +00:00
|
|
|
|
2018-07-31 21:21:55 +00:00
|
|
|
func detachedCommand(name string, arg ...string) *exec.Cmd {
|
|
|
|
cmd := exec.Command(name, arg...)
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 8}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-08-05 16:23:55 +00:00
|
|
|
func pauseCommand() *exec.Cmd {
|
|
|
|
return exec.Command("cmd", "/c", "pause")
|
|
|
|
}
|
|
|
|
|
|
|
|
func shellCommand(s string, args []string) *exec.Cmd {
|
|
|
|
args = append([]string{"/c", s}, args...)
|
2018-06-26 18:14:55 +00:00
|
|
|
|
|
|
|
args = append(gOpts.shellopts, args...)
|
|
|
|
|
2017-08-05 16:23:55 +00:00
|
|
|
return exec.Command(gOpts.shell, args...)
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:15:36 +00:00
|
|
|
func setDefaults() {
|
2018-06-28 20:37:01 +00:00
|
|
|
gOpts.cmds["open"] = &execExpr{"&", "%OPENER% %f%"}
|
|
|
|
gOpts.keys["e"] = &execExpr{"$", "%EDITOR% %f%"}
|
|
|
|
gOpts.keys["i"] = &execExpr{"!", "%PAGER% %f%"}
|
|
|
|
gOpts.keys["w"] = &execExpr{"$", "%SHELL%"}
|
2018-07-07 14:50:47 +00:00
|
|
|
|
2018-07-08 11:27:35 +00:00
|
|
|
gOpts.cmds["doc"] = &execExpr{"!", "lf -doc | %PAGER%"}
|
2018-07-07 14:50:47 +00:00
|
|
|
gOpts.keys["<f-1>"] = &callExpr{"doc", nil, 1}
|
2018-03-02 20:15:36 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 16:23:55 +00:00
|
|
|
func moveCursor(y, x int) {
|
|
|
|
// TODO: implement
|
|
|
|
return
|
|
|
|
}
|
2018-06-28 21:12:54 +00:00
|
|
|
|
|
|
|
func isExecutable(f os.FileInfo) bool {
|
|
|
|
exts := strings.Split(envPathExt, string(filepath.ListSeparator))
|
|
|
|
for _, e := range exts {
|
|
|
|
if strings.HasSuffix(strings.ToLower(f.Name()), strings.ToLower(e)) {
|
|
|
|
log.Print(f.Name(), e)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-03-17 17:16:19 +00:00
|
|
|
|
|
|
|
func exportFiles(f string, fs []string) {
|
|
|
|
envFile := fmt.Sprintf(`"%s"`, f)
|
|
|
|
|
|
|
|
var quotedFiles []string
|
|
|
|
for _, f := range fs {
|
|
|
|
quotedFiles = append(quotedFiles, fmt.Sprintf(`"%s"`, f))
|
|
|
|
}
|
|
|
|
envFiles := strings.Join(quotedFiles, gOpts.filesep)
|
|
|
|
|
|
|
|
os.Setenv("f", envFile)
|
|
|
|
os.Setenv("fs", envFiles)
|
|
|
|
|
|
|
|
if len(fs) == 0 {
|
|
|
|
os.Setenv("fx", envFile)
|
|
|
|
} else {
|
|
|
|
os.Setenv("fx", envFiles)
|
|
|
|
}
|
|
|
|
}
|