fork/gdu
git clone https://git.julia.blue/fork/gdu
011b7e8
master
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 ( 16osDarwin = "darwin" 17osWindows = "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 { 25name string 26args []string 27} 28 29var ( 30pbcopy = clipboardTool {name :"pbcopy" } 31clip = clipboardTool {name :"clip" } 32xclip = clipboardTool {name :"xclip" ,args : []string {"-selection" ,"clipboard" }} 33xsel = clipboardTool {name :"xsel" ,args : []string {"--clipboard" ,"--input" }} 34wlCopy = 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 { 42goos string 43wayland bool 44lookPath func (string ) (string ,error ) 45} 46 47// currentClipboardEnv describes the environment gdu is actually running in. 48func currentClipboardEnv ()clipboardEnv { 49return clipboardEnv { 50goos :runtime .GOOS , 51wayland :os .Getenv ("WAYLAND_DISPLAY" )!= "" , 52lookPath :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 ) { 60var candidates []clipboardTool 61switch env .goos { 62case osDarwin : 63candidates = []clipboardTool {pbcopy } 64case osWindows : 65candidates = []clipboardTool {clip } 66default : 67if env .wayland { 68candidates = []clipboardTool {wlCopy ,xclip ,xsel } 69 }else { 70candidates = []clipboardTool {xclip ,xsel ,wlCopy } 71 } 72 } 73 74for _ ,c := range candidates { 75if path ,err := env .lookPath (c .name );err == nil { 76return append ([]string {path },c .args ... ),true 77 } 78 } 79return nil ,false 80} 81 82func copyToClipboard (text string )error { 83argv ,ok := currentClipboardEnv ().command () 84if ! ok { 85return errClipboardUnavailable 86 } 87 88cmd := exec .Command (argv [0 ],argv [1 :]... ) 89cmd .Stdin = strings .NewReader (text ) 90if err := cmd .Run ();err != nil { 91return errors .Wrap (err ,"running clipboard command" ) 92 } 93return nil 94} 95 96// copySelectedPath copies the path of the item under the cursor to the system 97// clipboard. 98func (ui * UI )copySelectedPath () { 99if ui .currentDir == nil { 100return 101 } 102 103row ,column := ui .table .GetSelection () 104cell := ui .table .GetCell (row ,column ) 105if cell == nil || cell .GetReference ()== nil { 106return 107 } 108selectedItem := cell .GetReference ().(fs.Item ) 109 110if err := copyToClipboard (selectedItem .GetPath ());err != nil { 111ui .showErr ("Can't copy path to clipboard" ,err ) 112return 113 } 114 115ui .showMessage (" Path copied to clipboard" ) 116}