fork/gdu

git clone https://git.julia.blue/fork/gdu

addielaruefeat: copy path of selected item to clipboard (#645)011b7e8

master
3.0 KiB116 linesraw
1package tui
2
3import (
4	"os"
5	"os/exec"
6	"runtime"
7	"strings"
8
9	"github.com/pkg/errors"
10
11	"github.com/dundee/gdu/v5/pkg/fs"
12)
13
14// Operating system identifiers as reported by runtime.GOOS.
15const (
16	osDarwin  = "darwin"
17	osWindows = "windows"
18)
19
20var errClipboardUnavailable = errors.New("no clipboard tool found")
21
22// clipboardTool is an external program able to write the clipboard, together
23// with the arguments it needs to target the clipboard selection.
24type clipboardTool struct {
25	name string
26	args []string
27}
28
29var (
30	pbcopy = clipboardTool{name: "pbcopy"}
31	clip   = clipboardTool{name: "clip"}
32	xclip  = clipboardTool{name: "xclip", args: []string{"-selection", "clipboard"}}
33	xsel   = clipboardTool{name: "xsel", args: []string{"--clipboard", "--input"}}
34	wlCopy = clipboardTool{name: "wl-copy"}
35)
36
37// clipboardEnv is the part of the environment that decides which clipboard tool
38// gdu uses. It exists so the choice can be tested without a real desktop
39// session: lookPath reports which tools are installed, and wayland selects the
40// preferred Linux/BSD tool ordering.
41type clipboardEnv struct {
42	goos     string
43	wayland  bool
44	lookPath func(string) (string, error)
45}
46
47// currentClipboardEnv describes the environment gdu is actually running in.
48func currentClipboardEnv() clipboardEnv {
49	return clipboardEnv{
50		goos:     runtime.GOOS,
51		wayland:  os.Getenv("WAYLAND_DISPLAY") != "",
52		lookPath: exec.LookPath,
53	}
54}
55
56// command returns the command and arguments used to write text to the system
57// clipboard in this environment. The returned bool is false when no supported
58// tool is available, for example on a headless machine.
59func (env clipboardEnv) command() ([]string, bool) {
60	var candidates []clipboardTool
61	switch env.goos {
62	case osDarwin:
63		candidates = []clipboardTool{pbcopy}
64	case osWindows:
65		candidates = []clipboardTool{clip}
66	default:
67		if env.wayland {
68			candidates = []clipboardTool{wlCopy, xclip, xsel}
69		} else {
70			candidates = []clipboardTool{xclip, xsel, wlCopy}
71		}
72	}
73
74	for _, c := range candidates {
75		if path, err := env.lookPath(c.name); err == nil {
76			return append([]string{path}, c.args...), true
77		}
78	}
79	return nil, false
80}
81
82func copyToClipboard(text string) error {
83	argv, ok := currentClipboardEnv().command()
84	if !ok {
85		return errClipboardUnavailable
86	}
87
88	cmd := exec.Command(argv[0], argv[1:]...)
89	cmd.Stdin = strings.NewReader(text)
90	if err := cmd.Run(); err != nil {
91		return errors.Wrap(err, "running clipboard command")
92	}
93	return nil
94}
95
96// copySelectedPath copies the path of the item under the cursor to the system
97// clipboard.
98func (ui *UI) copySelectedPath() {
99	if ui.currentDir == nil {
100		return
101	}
102
103	row, column := ui.table.GetSelection()
104	cell := ui.table.GetCell(row, column)
105	if cell == nil || cell.GetReference() == nil {
106		return
107	}
108	selectedItem := cell.GetReference().(fs.Item)
109
110	if err := copyToClipboard(selectedItem.GetPath()); err != nil {
111		ui.showErr("Can't copy path to clipboard", err)
112		return
113	}
114
115	ui.showMessage(" Path copied to clipboard")
116}