lf/os_windows.go

117 lines
2.2 KiB
Go
Raw Normal View History

package main
import (
"log"
"os"
"os/exec"
2017-08-06 08:05:46 +00:00
"os/user"
"path/filepath"
2017-08-06 08:05:46 +00:00
"strings"
2018-07-31 21:21:55 +00:00
"syscall"
)
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 (
gUser *user.User
gConfigPaths []string
gMarksPath string
gHistoryPath string
2017-08-06 08:05:46 +00:00
)
func init() {
2018-06-28 20:37:01 +00:00
if envOpener == "" {
envOpener = "start"
}
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]
data := os.Getenv("LOCALAPPDATA")
gConfigPaths = []string{
filepath.Join(os.Getenv("ProgramData"), "lf", "lfrc"),
filepath.Join(data, "lf", "lfrc"),
}
gMarksPath = filepath.Join(data, "lf", "marks")
gHistoryPath = filepath.Join(data, "lf", "history")
2017-08-06 08:05:46 +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
}
func pauseCommand() *exec.Cmd {
return exec.Command("cmd", "/c", "pause")
}
func shellCommand(s string, args []string) *exec.Cmd {
args = append([]string{"/c", s}, args...)
args = append(gOpts.shellopts, args...)
return exec.Command(gOpts.shell, args...)
}
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-08 11:27:35 +00:00
gOpts.cmds["doc"] = &execExpr{"!", "lf -doc | %PAGER%"}
gOpts.keys["<f-1>"] = &callExpr{"doc", nil, 1}
}
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
}