lf/os.go

149 lines
2.8 KiB
Go
Raw Normal View History

// +build !windows
package main
import (
"fmt"
2017-08-06 08:05:46 +00:00
"log"
"os"
"os/exec"
2017-08-06 08:05:46 +00:00
"os/user"
"path/filepath"
"runtime"
2019-03-17 17:16:19 +00:00
"strings"
)
var (
2018-06-28 18:51:24 +00:00
envOpener = os.Getenv("OPENER")
envEditor = os.Getenv("EDITOR")
envPager = os.Getenv("PAGER")
envShell = os.Getenv("SHELL")
)
2017-08-06 08:05:46 +00:00
var (
gDefaultShell = "sh"
2017-08-06 08:05:46 +00:00
gDefaultSocketProt = "unix"
gDefaultSocketPath string
)
var (
gUser *user.User
gConfigPaths []string
gMarksPath string
gHistoryPath string
2017-08-06 08:05:46 +00:00
)
func init() {
2018-06-28 18:51:24 +00:00
if envOpener == "" {
if runtime.GOOS == "darwin" {
envOpener = "open"
} else {
envOpener = "xdg-open"
}
}
if envEditor == "" {
envEditor = "vi"
}
if envPager == "" {
envPager = "less"
}
if envShell == "" {
envShell = "sh"
}
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)
if os.Getenv("HOME") == "" {
log.Print("$HOME variable is empty or not set")
}
if os.Getenv("USER") == "" {
log.Print("$USER variable is empty or not set")
}
2017-08-06 08:05:46 +00:00
}
2017-11-19 18:55:13 +00:00
gUser = u
2017-08-06 08:05:46 +00:00
config := os.Getenv("XDG_CONFIG_HOME")
if config == "" {
config = filepath.Join(gUser.HomeDir, ".config")
}
gConfigPaths = []string{
filepath.Join("/etc", "lf", "lfrc"),
filepath.Join(config, "lf", "lfrc"),
}
2017-08-06 08:05:46 +00:00
data := os.Getenv("XDG_DATA_HOME")
if data == "" {
data = filepath.Join(gUser.HomeDir, ".local", "share")
}
gMarksPath = filepath.Join(data, "lf", "marks")
gHistoryPath = filepath.Join(data, "lf", "history")
2017-08-06 08:05:46 +00:00
gDefaultSocketPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.sock", gUser.Username))
}
2018-07-31 21:21:55 +00:00
func detachedCommand(name string, arg ...string) *exec.Cmd {
return exec.Command(name, arg...)
}
func pauseCommand() *exec.Cmd {
2018-05-20 17:30:41 +00:00
cmd := `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", cmd)
}
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...)
args = append(gOpts.shellopts, args...)
return exec.Command(gOpts.shell, args...)
}
func setDefaults() {
2018-06-28 18:51:24 +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"}
gOpts.cmds["doc"] = &execExpr{"$", "lf -doc | $PAGER"}
gOpts.keys["<f-1>"] = &callExpr{"doc", nil, 1}
}
func moveCursor(y, x int) {
fmt.Printf("\033[%d;%dH", y, x)
}
2018-06-28 21:12:54 +00:00
func isExecutable(f os.FileInfo) bool {
return f.Mode()&0111 != 0
}
2019-03-17 17:16:19 +00:00
func exportFiles(f string, fs []string) {
envFile := f
envFiles := strings.Join(fs, gOpts.filesep)
os.Setenv("f", envFile)
os.Setenv("fs", envFiles)
if len(fs) == 0 {
os.Setenv("fx", envFile)
} else {
os.Setenv("fx", envFiles)
}
}