| 1 | //go:build !darwin && !windows |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "bufio" |
| 7 | "context" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | type linuxDesktopEntry struct { |
| 16 | Path string |
| 17 | ID string |
| 18 | Name string |
| 19 | Icon string |
| 20 | } |
| 21 | |
| 22 | func linuxExecutable(names ...string) string { |
| 23 | for _, name := range names { |
| 24 | if path, err := exec.LookPath(name); err == nil { |
| 25 | return path |
| 26 | } |
| 27 | } |
| 28 | return "" |
| 29 | } |
| 30 | |
| 31 | func linuxDesktopEntries() []linuxDesktopEntry { |
| 32 | home, _ := os.UserHomeDir() |
| 33 | roots := []string{"/usr/share/applications", "/usr/local/share/applications"} |
| 34 | if home != "" { |
| 35 | roots = append([]string{filepath.Join(home, ".local", "share", "applications")}, roots...) |
| 36 | } |
| 37 | var entries []linuxDesktopEntry |
| 38 | for _, root := range roots { |
| 39 | files, err := os.ReadDir(root) |
| 40 | if err != nil { |
| 41 | continue |
| 42 | } |
| 43 | for _, file := range files { |
| 44 | if file.IsDir() || !strings.HasSuffix(strings.ToLower(file.Name()), ".desktop") { |
| 45 | continue |
| 46 | } |
| 47 | path := filepath.Join(root, file.Name()) |
| 48 | if entry, ok := parseLinuxDesktopEntry(path); ok { |
| 49 | entries = append(entries, entry) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return entries |
| 54 | } |
| 55 | |
| 56 | func parseLinuxDesktopEntry(path string) (linuxDesktopEntry, bool) { |
| 57 | file, err := os.Open(path) |
| 58 | if err != nil { |
| 59 | return linuxDesktopEntry{}, false |
| 60 | } |
| 61 | defer file.Close() |
| 62 | entry := linuxDesktopEntry{Path: path, ID: strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))} |
| 63 | inDesktopEntry := false |
| 64 | hidden := false |
| 65 | entryType := "" |
| 66 | scanner := bufio.NewScanner(file) |
| 67 | for scanner.Scan() { |
| 68 | line := strings.TrimSpace(scanner.Text()) |
| 69 | if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { |
| 70 | inDesktopEntry = line == "[Desktop Entry]" |
| 71 | continue |
| 72 | } |
| 73 | if !inDesktopEntry || line == "" || strings.HasPrefix(line, "#") { |
| 74 | continue |
| 75 | } |
| 76 | key, value, found := strings.Cut(line, "=") |
| 77 | if !found { |
| 78 | continue |
| 79 | } |
| 80 | switch strings.TrimSpace(key) { |
| 81 | case "Name": |
| 82 | entry.Name = strings.TrimSpace(value) |
| 83 | case "Icon": |
| 84 | entry.Icon = strings.TrimSpace(value) |
| 85 | case "Type": |
| 86 | entryType = strings.TrimSpace(value) |
| 87 | case "Hidden": |
| 88 | hidden = strings.EqualFold(strings.TrimSpace(value), "true") |
| 89 | } |
| 90 | } |
| 91 | if scanner.Err() != nil || hidden || (entryType != "" && entryType != "Application") || entry.Name == "" { |
| 92 | return linuxDesktopEntry{}, false |
| 93 | } |
| 94 | return entry, true |
| 95 | } |
| 96 | |
| 97 | func normalizeLinuxAppName(value string) string { |
| 98 | value = strings.ToLower(value) |
| 99 | return strings.NewReplacer(" ", "", "-", "", "_", "", ".", "").Replace(value) |
| 100 | } |
| 101 | |
| 102 | func findLinuxDesktopEntry(entries []linuxDesktopEntry, aliases ...string) (linuxDesktopEntry, bool) { |
| 103 | for _, alias := range aliases { |
| 104 | want := normalizeLinuxAppName(alias) |
| 105 | for _, entry := range entries { |
| 106 | if normalizeLinuxAppName(entry.ID) == want || normalizeLinuxAppName(entry.Name) == want { |
| 107 | return entry, true |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | for _, alias := range aliases { |
| 112 | want := normalizeLinuxAppName(alias) |
| 113 | if len(want) < 5 { |
| 114 | continue |
| 115 | } |
| 116 | for _, entry := range entries { |
| 117 | if strings.Contains(normalizeLinuxAppName(entry.ID), want) || strings.Contains(normalizeLinuxAppName(entry.Name), want) { |
| 118 | return entry, true |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | return linuxDesktopEntry{}, false |
| 123 | } |
| 124 | |
| 125 | func linuxDefaultDirectoryDesktopEntry(entries []linuxDesktopEntry) (linuxDesktopEntry, bool) { |
| 126 | xdgMime := linuxExecutable("xdg-mime") |
| 127 | if xdgMime != "" { |
| 128 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 129 | out, err := exec.CommandContext(ctx, xdgMime, "query", "default", "inode/directory").Output() |
| 130 | cancel() |
| 131 | if err == nil { |
| 132 | id := strings.TrimSpace(string(out)) |
| 133 | if entry, ok := findLinuxDesktopEntry(entries, id, strings.TrimSuffix(id, ".desktop")); ok { |
| 134 | return entry, true |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | return findLinuxDesktopEntry(entries, "org.gnome.Nautilus", "org.kde.dolphin", "thunar") |
| 139 | } |
| 140 | |
| 141 | func resolveLinuxDesktopIcon(icon string) string { |
| 142 | icon = strings.TrimSpace(icon) |
| 143 | if icon == "" { |
| 144 | return "" |
| 145 | } |
| 146 | if filepath.IsAbs(icon) { |
| 147 | if info, err := os.Stat(icon); err == nil && !info.IsDir() { |
| 148 | return icon |
| 149 | } |
| 150 | return "" |
| 151 | } |
| 152 | ext := filepath.Ext(icon) |
| 153 | name := strings.TrimSuffix(icon, ext) |
| 154 | exts := []string{".png", ".svg", ".webp", ".jpg"} |
| 155 | if ext != "" { |
| 156 | exts = []string{ext} |
| 157 | } |
| 158 | home, _ := os.UserHomeDir() |
| 159 | iconRoots := []string{"/usr/share/icons", "/usr/local/share/icons"} |
| 160 | if home != "" { |
| 161 | iconRoots = append([]string{filepath.Join(home, ".local", "share", "icons"), filepath.Join(home, ".icons")}, iconRoots...) |
| 162 | } |
| 163 | sizes := []string{"64x64", "128x128", "48x48", "256x256", "512x512", "scalable"} |
| 164 | for _, root := range iconRoots { |
| 165 | for _, size := range sizes { |
| 166 | for _, suffix := range exts { |
| 167 | patterns := []string{ |
| 168 | filepath.Join(root, "hicolor", size, "apps", name+suffix), |
| 169 | filepath.Join(root, "*", size, "apps", name+suffix), |
| 170 | } |
| 171 | for _, pattern := range patterns { |
| 172 | matches, _ := filepath.Glob(pattern) |
| 173 | for _, match := range matches { |
| 174 | if info, err := os.Stat(match); err == nil && !info.IsDir() { |
| 175 | return match |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | for _, root := range []string{"/usr/share/pixmaps", "/usr/local/share/pixmaps"} { |
| 183 | for _, suffix := range exts { |
| 184 | path := filepath.Join(root, name+suffix) |
| 185 | if info, err := os.Stat(path); err == nil && !info.IsDir() { |
| 186 | return path |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | return "" |
| 191 | } |
| 192 | |
| 193 | func platformExternalOpenerSpecs() []externalOpenerSpec { |
| 194 | entries := linuxDesktopEntries() |
| 195 | gio := linuxExecutable("gio") |
| 196 | var specs []externalOpenerSpec |
| 197 | add := func(id, name, kind, mode string, executables, desktopAliases []string) { |
| 198 | target := linuxExecutable(executables...) |
| 199 | entry, hasEntry := findLinuxDesktopEntry(entries, desktopAliases...) |
| 200 | launchMode := mode |
| 201 | if target == "" && hasEntry && gio != "" { |
| 202 | target = entry.Path |
| 203 | launchMode = "gio" |
| 204 | } |
| 205 | if target == "" { |
| 206 | return |
| 207 | } |
| 208 | iconSource := "" |
| 209 | if hasEntry { |
| 210 | iconSource = resolveLinuxDesktopIcon(entry.Icon) |
| 211 | } |
| 212 | specs = append(specs, externalOpenerSpec{ |
| 213 | View: ExternalOpenerView{ID: id, Name: name, Kind: kind}, |
| 214 | Target: target, |
| 215 | LaunchMode: launchMode, |
| 216 | IconSource: iconSource, |
| 217 | }) |
| 218 | } |
| 219 | |
| 220 | add("vscode", "VS Code", externalOpenerEditor, "path", []string{"code"}, []string{"code", "visual-studio-code", "com.visualstudio.code"}) |
| 221 | add("vscode-insiders", "VS Code Insiders", externalOpenerEditor, "path", []string{"code-insiders"}, []string{"code-insiders", "visual-studio-code-insiders"}) |
| 222 | add("cursor", "Cursor", externalOpenerEditor, "path", []string{"cursor"}, []string{"cursor", "com.todesktop.230313mzl4w4u92"}) |
| 223 | if target := linuxExecutable("xdg-open"); target != "" { |
| 224 | name := "File Manager" |
| 225 | iconSource := "" |
| 226 | if entry, ok := linuxDefaultDirectoryDesktopEntry(entries); ok { |
| 227 | name = entry.Name |
| 228 | iconSource = resolveLinuxDesktopIcon(entry.Icon) |
| 229 | } |
| 230 | specs = append(specs, externalOpenerSpec{ |
| 231 | View: ExternalOpenerView{ID: "file-manager", Name: name, Kind: externalOpenerFileManager}, |
| 232 | Target: target, |
| 233 | LaunchMode: "path", |
| 234 | IconSource: iconSource, |
| 235 | }) |
| 236 | } |
| 237 | add("ghostty", "Ghostty", externalOpenerTerminal, "ghostty", []string{"ghostty"}, []string{"com.mitchellh.ghostty", "ghostty"}) |
| 238 | add("gnome-terminal", "GNOME Terminal", externalOpenerTerminal, "gnome-terminal", []string{"gnome-terminal"}, []string{"org.gnome.Terminal", "gnome-terminal"}) |
| 239 | add("konsole", "Konsole", externalOpenerTerminal, "konsole", []string{"konsole"}, []string{"org.kde.konsole", "konsole"}) |
| 240 | add("kitty", "Kitty", externalOpenerTerminal, "kitty", []string{"kitty"}, []string{"kitty"}) |
| 241 | add("alacritty", "Alacritty", externalOpenerTerminal, "alacritty", []string{"alacritty"}, []string{"Alacritty", "alacritty"}) |
| 242 | add("terminal", "Terminal", externalOpenerTerminal, "cwd", []string{"x-terminal-emulator"}, []string{"terminal"}) |
| 243 | add("android-studio", "Android Studio", externalOpenerEditor, "path", []string{"android-studio", "studio", "studio.sh"}, []string{"android-studio", "com.google.AndroidStudio"}) |
| 244 | add("goland", "GoLand", externalOpenerEditor, "path", []string{"goland", "goland.sh"}, []string{"jetbrains-goland", "goland"}) |
| 245 | add("pycharm", "PyCharm", externalOpenerEditor, "path", []string{"pycharm", "pycharm.sh"}, []string{"jetbrains-pycharm", "pycharm"}) |
| 246 | add("intellij-idea", "IntelliJ IDEA", externalOpenerEditor, "path", []string{"idea", "idea.sh", "intellij-idea"}, []string{"jetbrains-idea", "intellij-idea"}) |
| 247 | add("webstorm", "WebStorm", externalOpenerEditor, "path", []string{"webstorm", "webstorm.sh"}, []string{"jetbrains-webstorm", "webstorm"}) |
| 248 | add("datagrip", "DataGrip", externalOpenerEditor, "path", []string{"datagrip", "datagrip.sh"}, []string{"jetbrains-datagrip", "datagrip"}) |
| 249 | add("codebuddy", "CodeBuddy", externalOpenerEditor, "path", []string{"codebuddy"}, []string{"codebuddy", "com.tencent.codebuddy"}) |
| 250 | add("windsurf", "Windsurf", externalOpenerEditor, "path", []string{"windsurf"}, []string{"windsurf", "com.exafunction.windsurf"}) |
| 251 | add("zed", "Zed", externalOpenerEditor, "path", []string{"zed"}, []string{"dev.zed.Zed", "zed"}) |
| 252 | add("sublime-text", "Sublime Text", externalOpenerEditor, "path", []string{"subl", "sublime_text"}, []string{"sublime_text", "sublime-text"}) |
| 253 | add("kiro", "Kiro", externalOpenerEditor, "path", []string{"kiro"}, []string{"kiro", "dev.kiro.desktop"}) |
| 254 | return specs |
| 255 | } |
| 256 | |
| 257 | func platformExternalOpenerIconDataURL(spec externalOpenerSpec) string { |
| 258 | return externalOpenerIconFileDataURL(spec.IconSource) |
| 259 | } |
| 260 | |
| 261 | func linuxExternalOpenerCommand(spec externalOpenerSpec, path string) (*exec.Cmd, error) { |
| 262 | target := spec.Target |
| 263 | var args []string |
| 264 | switch spec.LaunchMode { |
| 265 | case "path": |
| 266 | args = []string{path} |
| 267 | case "gio": |
| 268 | args = []string{"launch", spec.Target, path} |
| 269 | target = linuxExecutable("gio") |
| 270 | case "ghostty", "gnome-terminal": |
| 271 | args = []string{"--working-directory=" + path} |
| 272 | case "konsole": |
| 273 | args = []string{"--workdir", path} |
| 274 | case "kitty": |
| 275 | args = []string{"--directory", path} |
| 276 | case "alacritty": |
| 277 | args = []string{"--working-directory", path} |
| 278 | } |
| 279 | if target == "" { |
| 280 | return nil, os.ErrNotExist |
| 281 | } |
| 282 | cmd := exec.Command(target, args...) |
| 283 | cmd.Dir = path |
| 284 | return cmd, nil |
| 285 | } |
| 286 | |
| 287 | func launchPlatformExternalOpener(spec externalOpenerSpec, path string) error { |
| 288 | cmd, err := linuxExternalOpenerCommand(spec, path) |
| 289 | if err != nil { |
| 290 | return err |
| 291 | } |
| 292 | return startDetachedExternalOpener(cmd) |
| 293 | } |
| 294 |