-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfiles.go
262 lines (223 loc) · 6.55 KB
/
files.go
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)
// generate a letter combination for a given count (a, b, ..., z, aa, ab, ..., zzzzz, ...)
func generateLetterCombination(count int) string {
const alphabet = "abcdefghijklmnopqrstuvwxyz"
base := len(alphabet)
combination := ""
// Convert count to base-26 using letters
for count >= 0 {
combination = string(alphabet[count%base]) + combination
count = count/base - 1
}
return combination
}
func parseCommand(cmd string) []string {
var args []string
var current string
var quoteChar rune
inQuotes := false
for _, r := range cmd {
switch r {
case '\'', '"':
if inQuotes && r == quoteChar {
inQuotes = false
} else if !inQuotes {
inQuotes = true
quoteChar = r
} else {
current += string(r)
}
case ' ':
if !inQuotes {
if current != "" {
args = append(args, current)
current = ""
}
} else {
current += string(r)
}
default:
current += string(r)
}
}
if current != "" {
args = append(args, current)
}
return args
}
func CreateAndOpenFile(labdir string, prefix string, extension string, editor string, displayPath string) {
// 2006-01-02 15:04:05
dir, err := os.ReadDir(labdir)
if err != nil {
log.Fatalf("failed to read directory %v", err)
}
today := time.Now().Format("060102")
todaysFilesCount := 0
for _, file := range dir {
fileName := file.Name()
if strings.Contains(fileName, today) {
todaysFilesCount++
}
}
letterCombination := generateLetterCombination(todaysFilesCount)
validExtension := extension
if len(extension) > 0 && extension[:1] == "." {
validExtension = extension[1:]
}
validPrefix := prefix
if len(prefix) > 0 {
validPrefix = prefix + "-"
}
filename := validPrefix + today + letterCombination + "." + validExtension
file := filepath.Join(labdir, filename)
createdFile, err := os.Create(file)
if err != nil {
log.Fatalf("failed to create file %v", err)
}
createdFile.Close()
var cmd *exec.Cmd
// using 'if' to save some performance for users who has single editor command
if len(strings.Fields(editor)) > 1 {
parts := parseCommand(editor)
cmd = exec.Command(parts[0], append(parts[1:], file)...)
} else {
cmd = exec.Command(editor, file)
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
if strings.Contains(err.Error(), "executable file not found") {
fmt.Printf("\n Editor %s not found. \n \033[33mSet your preferred editor in %s.lab\033[0m (examples below):\n\n\teditor=code # for VS Code\n\teditor=nvim # for Neovim\n\teditor=vim # for Vim\n\n", editor, displayPath)
os.Remove(file)
return
}
}
fileInfo, _ := os.Stat(file)
if fileInfo.Size() == 0 {
os.Remove(file)
}
}
func organizeFiles(labdir string) []os.DirEntry {
dir, err := os.ReadDir(labdir)
if err != nil {
log.Fatalf("failed to read directory %v", err)
}
var noDotLabDir []os.DirEntry
for _, file := range dir {
if file.Name() != ".lab" {
noDotLabDir = append(noDotLabDir, file)
}
}
sort.Slice(noDotLabDir, func(i, j int) bool {
infoI, _ := noDotLabDir[i].Info()
infoJ, _ := noDotLabDir[j].Info()
return infoI.ModTime().After(infoJ.ModTime())
})
return noDotLabDir
}
func ListFiles(labdir string, lifedays string, displayPath string) {
days, err := strconv.ParseFloat(strings.TrimSpace(lifedays), 64)
if err != nil {
log.Fatal("invalid lifedays value")
}
organizedFiles := organizeFiles(labdir)
const (
Reset = "\033[0m"
Green = "\033[32m"
Cyan = "\033[36m"
Magenta = "\033[35m"
Yellow = "\033[33m"
Grey = "\033[90m"
Red = "\033[31m"
)
// less than 2, because there there is already .lab file
if len(organizedFiles) == 0 {
fmt.Printf("\n\t%sYour lab is empty!%s Create a new file with: %slab <extension>%s (e.g., %slab js%s)\n\n",
Cyan, Reset, Green, Reset, Yellow, Reset)
return
}
fmt.Printf("\n To open, use: lab" + Green + " <number>\n" + Reset)
fmt.Printf(" To create: lab <extension>\n\n")
fmt.Println("\t" + Cyan + " Lab Files:" + Reset + Grey + " " + displayPath + "\n" + Reset)
for i, file := range organizedFiles {
info, _ := file.Info()
age := time.Since(info.ModTime())
var timeLeft string
daysLeft := int(float64(days) - age.Hours()/24)
hoursLeft := int(float64(days)*24 - age.Hours())
minutesLeft := int(float64(days)*24*60 - age.Minutes())
if minutesLeft < 60 {
minutesLeftStr := strconv.Itoa(minutesLeft)
timeLeft = fmt.Sprintf(Red+" %sm"+Reset, minutesLeftStr)
} else if hoursLeft < 25 {
hoursLeftStr := strconv.Itoa(hoursLeft)
timeLeft = fmt.Sprintf(Yellow+" %sh"+Reset, hoursLeftStr)
} else {
daysLeftStr := strconv.Itoa(daysLeft)
timeLeft = fmt.Sprintf(" %sd", daysLeftStr)
}
padding := 18
fmt.Printf("\t"+Green+"%5s "+Reset+"%-*s"+Grey+"%s\n", fmt.Sprintf("[%d] ", i+1), padding, file.Name(), timeLeft)
}
fmt.Println("")
}
func OpenFile(labdir string, tag string, editor string, displayPath string) {
organizedFiles := organizeFiles(labdir)
if n, err := strconv.Atoi(tag); err == nil && n <= len(organizedFiles) {
var fileName string
if n, _ := strconv.Atoi(tag); n == 0 {
fileName = ".lab"
} else {
fileName = organizedFiles[n-1].Name()
}
fullFileName := filepath.Join(labdir, fileName)
var cmd *exec.Cmd
// using 'if' to save some performance for users who has single editor command
if len(strings.Fields(editor)) > 1 {
parts := parseCommand(editor)
cmd = exec.Command(parts[0], append(parts[1:], fullFileName)...)
} else {
cmd = exec.Command(editor, fullFileName)
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
if strings.Contains(err.Error(), "executable file not found") {
fmt.Printf("\n Editor %s not found. \n \033[33mSet your preferred editor in %s.lab\033[0m (examples below):\n\n\teditor=code # for VS Code\n\teditor=nvim # for Neovim\n\teditor=vim # for Vim\n\n", editor, displayPath)
return
}
}
} else {
fmt.Printf("Invalid file index: %s (index out of range)\n", tag)
}
}
func DeleteExpiredFiles(labdir string, lifedays string) error {
organizedFiles := organizeFiles(labdir)
days, err := strconv.ParseFloat(strings.TrimSpace(lifedays), 64)
if err != nil {
return fmt.Errorf("failed to convert string to number %v", err)
}
duration := time.Duration(days * 24 * float64(time.Hour))
for _, file := range organizedFiles {
info, _ := file.Info()
if time.Since(info.ModTime()) > duration {
os.Remove(filepath.Join(labdir, file.Name()))
}
}
return nil
}