-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
153 lines (131 loc) · 3.23 KB
/
Copy pathmain.go
File metadata and controls
153 lines (131 loc) · 3.23 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/atotto/clipboard"
)
const (
colorReset = "\033[0m" // Reset to default color
colorGreen = "\033[32m" // Green for success
colorYellow = "\033[33m" // Yellow for warnings
colorRed = "\033[31m" // Red for errors
)
// Log functions with colors
func logSuccess(message string) {
fmt.Printf("%s[+]%s %s\n", colorGreen, colorReset, message)
}
func logWarning(message string) {
fmt.Printf("%s[!]%s %s\n", colorYellow, colorReset, message)
}
func logError(prefix string, err error) {
fmt.Printf("%s[X]%s %s: %s\n", colorRed, colorReset, prefix, err)
}
// Main function
func main() {
// Check for missing dependencies on Linux
checkEnvironment()
var copyContent bool
flag.BoolVar(©Content, "c", false, "Copy the content of the file to clipboard")
flag.Parse()
path := "."
if flag.NArg() >= 1 {
path = flag.Arg(0)
}
// Check if the path exists
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
logError("The path does not exist", err)
} else {
logError("Error", err)
}
return
}
// It's a relative path
if !filepath.IsAbs(path) {
absolutePath, err := filepath.Abs(path)
if err != nil {
logError("Error converting to absolute path", err)
return
}
path = absolutePath
}
// Check if it's a file or a directory
if info.IsDir() {
path += string(filepath.Separator)
}
// Check if the -c flag is provided
if copyContent {
copyContent2Clip(path)
} else {
copyPath2Clip(path)
}
}
func checkEnvironment() {
switch runtime.GOOS {
case "linux":
checkLinuxEnvironment()
case "darwin":
// mac os has by default pbcopy installed
case "windows":
// windows use their own system calls.
default:
logError("Unsupported operating system", fmt.Errorf(runtime.GOOS))
}
}
func checkLinuxEnvironment() {
// Check if xclip is installed
if _, err := exec.LookPath("xclip"); err != nil {
logWarning("xclip is not installed. Install it using your package manager.")
}
// Check if DISPLAY is set
display := os.Getenv("DISPLAY")
if display == "" {
logWarning("DISPLAY is not set. Clipboard operations might fail.")
fmt.Println("In case it fails, set DISPLAY to 0. Run: \033[0mexport DISPLAY=:0\033[0m")
}
}
func copyPath2Clip(path string) {
copy2Clip(path)
logSuccess(fmt.Sprintf("Copied path of: %s", path))
}
func copyContent2Clip(path string) {
// Check if the path is a directory
info, err := os.Stat(path)
if err != nil {
logError("Failed to read file info", err)
return
}
if info.IsDir() {
logWarning("Cannot copy content of a directory.")
return
}
content, err := os.ReadFile(path)
if err != nil {
logError("Failed to read file content", err)
return
}
copy2Clip(string(content))
logSuccess(fmt.Sprintf("Copied content of: %s", path))
}
func copy2Clip(data string) {
err := clipboard.WriteAll(data)
if err != nil {
logError("Failed to write to clipboard", err)
return
}
}
func displayUsage() {
fmt.Println("")
fmt.Println("pathclip v0.4")
fmt.Println("")
fmt.Println("Copy the absolute location of a file to clipboard")
fmt.Println("Usage: ptc {file}")
fmt.Println("")
fmt.Println("Copy the content of the file to clipboard")
fmt.Println("Usage: ptc [-c] {file}")
}