handle user variables on all platforms
This commit is contained in:
1 parent
ae4a29e550
commit
cb577a4c3a
7 files changed
+80
-49
No files matched your search
@@ -164,7 +164,7 @@ func matchExec(s string) (matches []string, longest string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func matchFile(s string) (matches []string, longest string) {
|
func matchFile(s string) (matches []string, longest string) {
|
||||||
dir := strings.Replace(s, "~", envHome, -1)
|
dir := strings.Replace(s, "~", gUser.HomeDir, -1)
|
||||||
|
|
||||||
if !filepath.IsAbs(dir) {
|
if !filepath.IsAbs(dir) {
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ func (e *setExpr) eval(app *app, args []string) {
|
|||||||
case "ifs":
|
case "ifs":
|
||||||
gOpts.ifs = e.val
|
gOpts.ifs = e.val
|
||||||
case "previewer":
|
case "previewer":
|
||||||
gOpts.previewer = strings.Replace(e.val, "~", envHome, -1)
|
gOpts.previewer = strings.Replace(e.val, "~", gUser.HomeDir, -1)
|
||||||
case "shell":
|
case "shell":
|
||||||
gOpts.shell = e.val
|
gOpts.shell = e.val
|
||||||
case "sortby":
|
case "sortby":
|
||||||
|
|||||||
@@ -11,57 +11,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
envUser = os.Getenv("USER")
|
envPath = os.Getenv("PATH")
|
||||||
envHome = os.Getenv("HOME")
|
|
||||||
envHost = os.Getenv("HOSTNAME")
|
|
||||||
envPath = os.Getenv("PATH")
|
|
||||||
envConfig = os.Getenv("XDG_CONFIG_HOME")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gClientID int
|
gClientID int
|
||||||
|
gHostname string
|
||||||
gLastDirPath string
|
gLastDirPath string
|
||||||
gSelectionPath string
|
gSelectionPath string
|
||||||
gSocketProt string
|
gSocketProt string
|
||||||
gSocketPath string
|
gSocketPath string
|
||||||
gLogPath string
|
gLogPath string
|
||||||
gServerLogPath string
|
gServerLogPath string
|
||||||
gConfigPath string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if envUser == "" {
|
var err error
|
||||||
log.Print("$USER not set")
|
|
||||||
}
|
|
||||||
if envHome == "" {
|
|
||||||
envHome = "/home/" + envUser
|
|
||||||
}
|
|
||||||
if envHost == "" {
|
|
||||||
host, err := os.Hostname()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("hostname: %s", err)
|
|
||||||
}
|
|
||||||
envHost = host
|
|
||||||
}
|
|
||||||
if envConfig == "" {
|
|
||||||
envConfig = filepath.Join(envHome, ".config")
|
|
||||||
}
|
|
||||||
|
|
||||||
gSocketProt = gDefaultSocketProt
|
gHostname, err = os.Hostname()
|
||||||
gSocketPath = gDefaultSocketPath
|
if err != nil {
|
||||||
|
log.Printf("hostname: %s", err)
|
||||||
tmp := os.TempDir()
|
|
||||||
|
|
||||||
gClientID = 1000
|
|
||||||
gLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.%d.log", envUser, gClientID))
|
|
||||||
for _, err := os.Stat(gLogPath); err == nil; _, err = os.Stat(gLogPath) {
|
|
||||||
gClientID++
|
|
||||||
gLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.%d.log", envUser, gClientID))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gServerLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.server.log", envUser))
|
|
||||||
|
|
||||||
gConfigPath = filepath.Join(envConfig, "lf", "lfrc")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func startServer() {
|
func startServer() {
|
||||||
@@ -104,7 +74,11 @@ func main() {
|
|||||||
defer pprof.StopCPUProfile()
|
defer pprof.StopCPUProfile()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gSocketProt = gDefaultSocketProt
|
||||||
|
gSocketPath = gDefaultSocketPath
|
||||||
|
|
||||||
if *serverMode {
|
if *serverMode {
|
||||||
|
gServerLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.server.log", gUser.Username))
|
||||||
serve()
|
serve()
|
||||||
} else {
|
} else {
|
||||||
// TODO: check if the socket is working
|
// TODO: check if the socket is working
|
||||||
@@ -112,6 +86,13 @@ func main() {
|
|||||||
startServer()
|
startServer()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gClientID = 1000
|
||||||
|
gLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.%d.log", gUser.Username, gClientID))
|
||||||
|
for _, err := os.Stat(gLogPath); err == nil; _, err = os.Stat(gLogPath) {
|
||||||
|
gClientID++
|
||||||
|
gLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.%d.log", gUser.Username, gClientID))
|
||||||
|
}
|
||||||
|
|
||||||
client()
|
client()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -349,7 +349,7 @@ func (nav *nav) top() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (nav *nav) cd(wd string) error {
|
func (nav *nav) cd(wd string) error {
|
||||||
wd = strings.Replace(wd, "~", envHome, -1)
|
wd = strings.Replace(wd, "~", gUser.HomeDir, -1)
|
||||||
wd = filepath.Clean(wd)
|
wd = filepath.Clean(wd)
|
||||||
|
|
||||||
if !filepath.IsAbs(wd) {
|
if !filepath.IsAbs(wd) {
|
||||||
|
|||||||
@@ -4,14 +4,41 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
var gDefaultShell = "/bin/sh"
|
var (
|
||||||
var gDefaultSocketProt = "unix"
|
gDefaultShell = "/bin/sh"
|
||||||
var gDefaultSocketPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.sock", envUser))
|
gDefaultSocketProt = "unix"
|
||||||
|
gDefaultSocketPath string
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
gUser *user.User
|
||||||
|
gConfigPath string
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
gUser, err = user.Current()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("user: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
config := os.Getenv("XDG_CONFIG_HOME")
|
||||||
|
if config == "" {
|
||||||
|
config = filepath.Join(gUser.HomeDir, ".config")
|
||||||
|
}
|
||||||
|
|
||||||
|
gConfigPath = filepath.Join(config, "lf", "lfrc")
|
||||||
|
|
||||||
|
gDefaultSocketPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.sock", gUser.Username))
|
||||||
|
}
|
||||||
|
|
||||||
func pauseCommand() *exec.Cmd {
|
func pauseCommand() *exec.Cmd {
|
||||||
c := `echo
|
c := `echo
|
||||||
|
|||||||
+26
-3
@@ -4,12 +4,35 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var gDefaultShell = "cmd"
|
var (
|
||||||
var gDefaultSocketProt = "tcp"
|
gDefaultShell = "cmd"
|
||||||
var gDefaultSocketPath = ":12345"
|
gDefaultSocketProt = "tcp"
|
||||||
|
gDefaultSocketPath = ":12345"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
gUser *user.User
|
||||||
|
gConfigPath string
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
gUser, err = user.Current()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("user: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove domain prefix
|
||||||
|
gUser.Username = strings.Split(gUser.Username, `\`)[1]
|
||||||
|
|
||||||
|
gConfigPath = filepath.Join(gUser.HomeDir, "AppData", "Local", "lf", "lfrc")
|
||||||
|
}
|
||||||
|
|
||||||
func pauseCommand() *exec.Cmd {
|
func pauseCommand() *exec.Cmd {
|
||||||
return exec.Command("cmd", "/c", "pause")
|
return exec.Command("cmd", "/c", "pause")
|
||||||
|
|||||||
@@ -521,12 +521,12 @@ func (ui *ui) draw(nav *nav) {
|
|||||||
|
|
||||||
dir := nav.currDir()
|
dir := nav.currDir()
|
||||||
|
|
||||||
path := strings.Replace(dir.path, envHome, "~", -1)
|
path := strings.Replace(dir.path, gUser.HomeDir, "~", -1)
|
||||||
path = filepath.Clean(path)
|
path = filepath.Clean(path)
|
||||||
|
|
||||||
ui.pwdwin.printf(0, 0, termbox.AttrBold|termbox.ColorGreen, bg, "%s@%s", envUser, envHost)
|
ui.pwdwin.printf(0, 0, termbox.AttrBold|termbox.ColorGreen, bg, "%s@%s", gUser.Username, gHostname)
|
||||||
ui.pwdwin.printf(len(envUser)+len(envHost)+1, 0, fg, bg, ":")
|
ui.pwdwin.printf(len(gUser.Username)+len(gHostname)+1, 0, fg, bg, ":")
|
||||||
ui.pwdwin.printf(len(envUser)+len(envHost)+2, 0, termbox.AttrBold|termbox.ColorBlue, bg, "%s", path)
|
ui.pwdwin.printf(len(gUser.Username)+len(gHostname)+2, 0, termbox.AttrBold|termbox.ColorBlue, bg, "%s", path)
|
||||||
|
|
||||||
length := min(len(ui.wins), len(nav.dirs))
|
length := min(len(ui.wins), len(nav.dirs))
|
||||||
woff := len(ui.wins) - length
|
woff := len(ui.wins) - length
|
||||||
|
|||||||
Reference in new issue
Block a user