handle user variables on all platforms

This commit is contained in:
Gokcehan committed 2017-08-06 11:05:46 +03:00
1 parent ae4a29e550
commit cb577a4c3a
7 files changed
+80 -49

No files matched your search

+1 -1
View File
@@ -164,7 +164,7 @@ func matchExec(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) {
wd, err := os.Getwd()
+1 -1
View File
@@ -113,7 +113,7 @@ func (e *setExpr) eval(app *app, args []string) {
case "ifs":
gOpts.ifs = e.val
case "previewer":
gOpts.previewer = strings.Replace(e.val, "~", envHome, -1)
gOpts.previewer = strings.Replace(e.val, "~", gUser.HomeDir, -1)
case "shell":
gOpts.shell = e.val
case "sortby":
+17 -36
View File
@@ -11,57 +11,27 @@ import (
)
var (
envUser = os.Getenv("USER")
envHome = os.Getenv("HOME")
envHost = os.Getenv("HOSTNAME")
envPath = os.Getenv("PATH")
envConfig = os.Getenv("XDG_CONFIG_HOME")
envPath = os.Getenv("PATH")
)
var (
gClientID int
gHostname string
gLastDirPath string
gSelectionPath string
gSocketProt string
gSocketPath string
gLogPath string
gServerLogPath string
gConfigPath string
)
func init() {
if envUser == "" {
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")
}
var err error
gSocketProt = gDefaultSocketProt
gSocketPath = gDefaultSocketPath
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))
gHostname, err = os.Hostname()
if err != nil {
log.Printf("hostname: %s", err)
}
gServerLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.server.log", envUser))
gConfigPath = filepath.Join(envConfig, "lf", "lfrc")
}
func startServer() {
@@ -104,7 +74,11 @@ func main() {
defer pprof.StopCPUProfile()
}
gSocketProt = gDefaultSocketProt
gSocketPath = gDefaultSocketPath
if *serverMode {
gServerLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.server.log", gUser.Username))
serve()
} else {
// TODO: check if the socket is working
@@ -112,6 +86,13 @@ func main() {
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()
}
}
+1 -1
View File
@@ -349,7 +349,7 @@ func (nav *nav) top() {
}
func (nav *nav) cd(wd string) error {
wd = strings.Replace(wd, "~", envHome, -1)
wd = strings.Replace(wd, "~", gUser.HomeDir, -1)
wd = filepath.Clean(wd)
if !filepath.IsAbs(wd) {
+30 -3
View File
@@ -4,14 +4,41 @@ package main
import (
"fmt"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
)
var gDefaultShell = "/bin/sh"
var gDefaultSocketProt = "unix"
var gDefaultSocketPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.sock", envUser))
var (
gDefaultShell = "/bin/sh"
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 {
c := `echo
+26 -3
View File
@@ -4,12 +4,35 @@ import (
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
)
var gDefaultShell = "cmd"
var gDefaultSocketProt = "tcp"
var gDefaultSocketPath = ":12345"
var (
gDefaultShell = "cmd"
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 {
return exec.Command("cmd", "/c", "pause")
+4 -4
View File
@@ -521,12 +521,12 @@ func (ui *ui) draw(nav *nav) {
dir := nav.currDir()
path := strings.Replace(dir.path, envHome, "~", -1)
path := strings.Replace(dir.path, gUser.HomeDir, "~", -1)
path = filepath.Clean(path)
ui.pwdwin.printf(0, 0, termbox.AttrBold|termbox.ColorGreen, bg, "%s@%s", envUser, envHost)
ui.pwdwin.printf(len(envUser)+len(envHost)+1, 0, fg, bg, ":")
ui.pwdwin.printf(len(envUser)+len(envHost)+2, 0, termbox.AttrBold|termbox.ColorBlue, bg, "%s", path)
ui.pwdwin.printf(0, 0, termbox.AttrBold|termbox.ColorGreen, bg, "%s@%s", gUser.Username, gHostname)
ui.pwdwin.printf(len(gUser.Username)+len(gHostname)+1, 0, fg, bg, ":")
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))
woff := len(ui.wins) - length