-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapp_shell.go
More file actions
100 lines (94 loc) · 2.93 KB
/
app_shell.go
File metadata and controls
100 lines (94 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
// app_shell.go ── 跨平台调起系统默认应用打开文件 / 在 Finder/Explorer
// 中显示文件位置。给 OpenJailbreakOverrideFile / RevealJailbreakOverrideFolder
// 等 App 方法使用。
import (
"fmt"
"os/exec"
"runtime"
)
// openPathWithSystem 用系统默认应用打开文件。
// - macOS: `open <path>` → 用 Finder 注册的默认 app(.md 一般是 TextEdit)
// - Windows: `cmd /c start "" <path>`
// - Linux: 优先 xdg-open → gio open → kde-open5 → exo-open(每个发行版默认不同)
//
// 不阻塞等待编辑器退出(用 Start 而非 Run),即返回。
func openPathWithSystem(path string) error {
if path == "" {
return fmt.Errorf("empty path")
}
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.Command("open", path)
case "windows":
cmd = exec.Command("cmd", "/c", "start", "", path)
default:
opener := pickLinuxOpener()
if opener == "" {
return fmt.Errorf("Linux 没有可用的文件打开器(缺 xdg-open / gio / kde-open5 / exo-open)。装一个:sudo apt install xdg-utils")
}
cmd = exec.Command(opener, path)
// gio 调用方式是 `gio open <path>`
if opener == "gio" {
cmd = exec.Command(opener, "open", path)
}
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("open failed: %w", err)
}
return nil
}
// revealPathInFileManager 在文件管理器中选中并显示文件位置。
// - macOS: `open -R <path>` → Finder 高亮该文件
// - Windows: `explorer /select,<path>`
// - Linux: nautilus / dolphin / nemo 等都各异,统一退化为打开父目录
func revealPathInFileManager(path string) error {
if path == "" {
return fmt.Errorf("empty path")
}
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.Command("open", "-R", path)
case "windows":
cmd = exec.Command("explorer", "/select,"+path)
default:
// Linux: 没有标准 "reveal" 命令,退化打开父目录
dir := path
if i := lastSepIndex(path); i > 0 {
dir = path[:i]
}
opener := pickLinuxOpener()
if opener == "" {
return fmt.Errorf("Linux 没有可用的文件管理器命令")
}
cmd = exec.Command(opener, dir)
if opener == "gio" {
cmd = exec.Command(opener, "open", dir)
}
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("reveal failed: %w", err)
}
return nil
}
// pickLinuxOpener 按 Linux 发行版常见优先级挑一个可用的文件打开器。
// xdg-utils 在大多数桌面发行版默认装;服务器/极简发行版 / WSL 可能没装。
// 返回空字符串表示一个都没找到,调用方应给用户友好提示。
func pickLinuxOpener() string {
for _, c := range []string{"xdg-open", "gio", "kde-open5", "kde-open", "exo-open", "gnome-open"} {
if _, err := exec.LookPath(c); err == nil {
return c
}
}
return ""
}
func lastSepIndex(s string) int {
for i := len(s) - 1; i >= 0; i-- {
if s[i] == '/' || s[i] == '\\' {
return i
}
}
return -1
}