| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"cmp" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"os/exec" |
| 8 |
"os/user" |
| 9 |
"path/filepath" |
| 10 |
"slices" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"golang.org/x/sys/windows" |
| 14 |
) |
| 15 |
|
| 16 |
var ( |
| 17 |
envOpener = os.Getenv("OPENER") |
| 18 |
envEditor = os.Getenv("VISUAL") |
| 19 |
envPager = os.Getenv("PAGER") |
| 20 |
envShell = os.Getenv("SHELL") |
| 21 |
) |
| 22 |
|
| 23 |
var envPathExts []string |
| 24 |
|
| 25 |
var ( |
| 26 |
gDefaultShell = "cmd" |
| 27 |
gDefaultShellFlag = "/c" |
| 28 |
gDefaultSocketPath string |
| 29 |
gDefaultHiddenFiles []string |
| 30 |
) |
| 31 |
|
| 32 |
var ( |
| 33 |
gUser *user.User |
| 34 |
gConfigPaths []string |
| 35 |
gColorsPaths []string |
| 36 |
gIconsPaths []string |
| 37 |
gFilesPath string |
| 38 |
gTagsPath string |
| 39 |
gMarksPath string |
| 40 |
gHistoryPath string |
| 41 |
) |
| 42 |
|
| 43 |
func init() { |
| 44 |
if envOpener == "" { |
| 45 |
envOpener = `start ""` |
| 46 |
} |
| 47 |
|
| 48 |
if envEditor == "" { |
| 49 |
envEditor = cmp.Or(os.Getenv("EDITOR"), "notepad") |
| 50 |
} |
| 51 |
|
| 52 |
if envPager == "" { |
| 53 |
envPager = "more" |
| 54 |
} |
| 55 |
|
| 56 |
if envShell == "" { |
| 57 |
envShell = "cmd" |
| 58 |
} |
| 59 |
|
| 60 |
homeDir, err := os.UserHomeDir() |
| 61 |
if err != nil { |
| 62 |
panic(err) |
| 63 |
} |
| 64 |
|
| 65 |
username := os.Getenv("USERNAME") |
| 66 |
if username == "" { |
| 67 |
panic("$USERNAME variable is empty or not set") |
| 68 |
} |
| 69 |
|
| 70 |
gUser = &user.User{ |
| 71 |
HomeDir: homeDir, |
| 72 |
Username: username, |
| 73 |
} |
| 74 |
|
| 75 |
config := cmp.Or( |
| 76 |
os.Getenv("LF_CONFIG_HOME"), |
| 77 |
os.Getenv("XDG_CONFIG_HOME"), |
| 78 |
os.Getenv("APPDATA"), |
| 79 |
) |
| 80 |
|
| 81 |
gConfigPaths = []string{ |
| 82 |
filepath.Join(os.Getenv("ProgramData"), "lf", "lfrc"), |
| 83 |
filepath.Join(config, "lf", "lfrc"), |
| 84 |
} |
| 85 |
|
| 86 |
gColorsPaths = []string{ |
| 87 |
filepath.Join(os.Getenv("ProgramData"), "lf", "colors"), |
| 88 |
filepath.Join(config, "lf", "colors"), |
| 89 |
} |
| 90 |
|
| 91 |
gIconsPaths = []string{ |
| 92 |
filepath.Join(os.Getenv("ProgramData"), "lf", "icons"), |
| 93 |
filepath.Join(config, "lf", "icons"), |
| 94 |
} |
| 95 |
|
| 96 |
data := cmp.Or( |
| 97 |
os.Getenv("LF_DATA_HOME"), |
| 98 |
os.Getenv("XDG_DATA_HOME"), |
| 99 |
os.Getenv("LOCALAPPDATA"), |
| 100 |
) |
| 101 |
|
| 102 |
gFilesPath = filepath.Join(data, "lf", "files") |
| 103 |
gMarksPath = filepath.Join(data, "lf", "marks") |
| 104 |
gTagsPath = filepath.Join(data, "lf", "tags") |
| 105 |
gHistoryPath = filepath.Join(data, "lf", "history") |
| 106 |
|
| 107 |
runtimeDir := os.TempDir() |
| 108 |
gDefaultSocketPath = filepath.Join(runtimeDir, "lf.sock") |
| 109 |
|
| 110 |
s := cmp.Or(os.Getenv("PATHEXT"), ".COM;.EXE;.BAT;.CMD") |
| 111 |
for ext := range strings.SplitSeq(s, ";") { |
| 112 |
if ext == "" { |
| 113 |
continue |
| 114 |
} |
| 115 |
envPathExts = append(envPathExts, strings.ToLower(ext)) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func detachedCommand(name string, arg ...string) *exec.Cmd { |
| 120 |
cmd := exec.Command(name, arg...) |
| 121 |
cmd.SysProcAttr = &windows.SysProcAttr{CreationFlags: 8} |
| 122 |
return cmd |
| 123 |
} |
| 124 |
|
| 125 |
func shellCommand(s string, args []string) *exec.Cmd { |
| 126 |
// Windows CMD requires special handling to deal with quoted arguments |
| 127 |
if strings.ToLower(gOpts.shell) == "cmd" { |
| 128 |
var builder strings.Builder |
| 129 |
builder.WriteString(s) |
| 130 |
for _, arg := range args { |
| 131 |
fmt.Fprintf(&builder, ` "%s"`, arg) |
| 132 |
} |
| 133 |
shellOpts := strings.Join(gOpts.shellopts, " ") |
| 134 |
cmdline := fmt.Sprintf(`%s %s %s "%s"`, gOpts.shell, shellOpts, gOpts.shellflag, builder.String()) |
| 135 |
|
| 136 |
cmd := exec.Command(gOpts.shell) |
| 137 |
cmd.SysProcAttr = &windows.SysProcAttr{CmdLine: cmdline} |
| 138 |
return cmd |
| 139 |
} |
| 140 |
|
| 141 |
args = append([]string{gOpts.shellflag, s}, args...) |
| 142 |
args = append(gOpts.shellopts, args...) |
| 143 |
return exec.Command(gOpts.shell, args...) |
| 144 |
} |
| 145 |
|
| 146 |
func shellSetPG(_ *exec.Cmd) { |
| 147 |
} |
| 148 |
|
| 149 |
func shellKill(cmd *exec.Cmd) error { |
| 150 |
return cmd.Process.Kill() |
| 151 |
} |
| 152 |
|
| 153 |
func setDefaults() { |
| 154 |
gOpts.cmds["open"] = &execExpr{"&", "%OPENER% %f%"} |
| 155 |
gOpts.nkeys["e"] = &execExpr{"$", "%EDITOR% %f%"} |
| 156 |
gOpts.vkeys["e"] = &execExpr{"$", "%EDITOR% %f%"} |
| 157 |
gOpts.nkeys["i"] = &execExpr{"!", "%PAGER% %f%"} |
| 158 |
gOpts.vkeys["i"] = &execExpr{"!", "%PAGER% %f%"} |
| 159 |
gOpts.nkeys["w"] = &execExpr{"$", "%SHELL%"} |
| 160 |
gOpts.vkeys["w"] = &execExpr{"$", "%SHELL%"} |
| 161 |
|
| 162 |
gOpts.cmds["help"] = &execExpr{"!", "%lf% -doc | %PAGER%"} |
| 163 |
gOpts.nkeys["<f-1>"] = &callExpr{"help", nil, 1} |
| 164 |
gOpts.vkeys["<f-1>"] = &callExpr{"help", nil, 1} |
| 165 |
|
| 166 |
gOpts.cmds["maps"] = &execExpr{"!", `%lf% -remote "query %id% maps" | %PAGER%`} |
| 167 |
gOpts.cmds["nmaps"] = &execExpr{"!", `%lf% -remote "query %id% nmaps" | %PAGER%`} |
| 168 |
gOpts.cmds["vmaps"] = &execExpr{"!", `%lf% -remote "query %id% vmaps" | %PAGER%`} |
| 169 |
gOpts.cmds["cmaps"] = &execExpr{"!", `%lf% -remote "query %id% cmaps" | %PAGER%`} |
| 170 |
gOpts.cmds["cmds"] = &execExpr{"!", `%lf% -remote "query %id% cmds" | %PAGER%`} |
| 171 |
} |
| 172 |
|
| 173 |
func setUserUmask() {} |
| 174 |
|
| 175 |
func isExecutable(f os.FileInfo) bool { |
| 176 |
ext := filepath.Ext(f.Name()) |
| 177 |
if ext == "" { |
| 178 |
return false |
| 179 |
} |
| 180 |
return slices.Contains(envPathExts, strings.ToLower(ext)) |
| 181 |
} |
| 182 |
|
| 183 |
func isHidden(f os.FileInfo, path string, hiddenfiles []string) bool { |
| 184 |
ptr, err := windows.UTF16PtrFromString(filepath.Join(path, f.Name())) |
| 185 |
if err != nil { |
| 186 |
return false |
| 187 |
} |
| 188 |
attrs, err := windows.GetFileAttributes(ptr) |
| 189 |
if err != nil { |
| 190 |
return false |
| 191 |
} |
| 192 |
|
| 193 |
if attrs&windows.FILE_ATTRIBUTE_HIDDEN != 0 { |
| 194 |
return true |
| 195 |
} |
| 196 |
|
| 197 |
hidden := false |
| 198 |
for _, pattern := range hiddenfiles { |
| 199 |
if matchPattern(strings.TrimPrefix(pattern, "!"), f.Name(), path) { |
| 200 |
hidden = !strings.HasPrefix(pattern, "!") |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return hidden |
| 205 |
} |
| 206 |
|
| 207 |
func userName(_ os.FileInfo) string { |
| 208 |
return "" |
| 209 |
} |
| 210 |
|
| 211 |
func groupName(_ os.FileInfo) string { |
| 212 |
return "" |
| 213 |
} |
| 214 |
|
| 215 |
func linkCount(_ os.FileInfo) string { |
| 216 |
return "" |
| 217 |
} |
| 218 |
|
| 219 |
func errCrossDevice(err error) bool { |
| 220 |
return err.(*os.LinkError).Err.(windows.Errno) == windows.ERROR_NOT_SAME_DEVICE |
| 221 |
} |
| 222 |
|
| 223 |
func quoteString(s string) string { |
| 224 |
// Windows CMD requires special handling to deal with quoted arguments |
| 225 |
if strings.ToLower(gOpts.shell) == "cmd" { |
| 226 |
return fmt.Sprintf(`"%s"`, s) |
| 227 |
} |
| 228 |
return s |
| 229 |
} |
| 230 |
|
| 231 |
func shellEscape(s string) string { |
| 232 |
for _, r := range s { |
| 233 |
if strings.ContainsRune(" !%&'()+,;=[]^`{}~", r) { |
| 234 |
return fmt.Sprintf(`"%s"`, s) |
| 235 |
} |
| 236 |
} |
| 237 |
return s |
| 238 |
} |
| 239 |
|
| 240 |
func shellUnescape(s string) string { |
| 241 |
return strings.ReplaceAll(s, `"`, "") |
| 242 |
} |