-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommands.go
87 lines (73 loc) · 1.98 KB
/
commands.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
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/wyne/fasder/logger"
)
// Initialization
func Init(args []string) {
for _, initializer := range args {
switch initializer {
case "auto":
// TODO: Support other shells
fmt.Println(ZshHook())
case "zsh-hook":
fmt.Println(ZshHook())
case "aliases":
fmt.Println(Aliases())
fmt.Println(fzfAliases())
}
}
}
// Sanitize command from shell hooks before processing
func Sanitize(args []string) {
// Concatenate all arguments into a single string
input := strings.Join(args, " ")
// First, handle the command substitution: `$(...)` becomes `...`
// This regex matches the command substitution and replaces it.
reCommandSubstitution := regexp.MustCompile(`([^\\])\$\([^\)]*\)`)
input = reCommandSubstitution.ReplaceAllString(input, "$1")
// Then, replace special characters with a space: `|&;<>$`{}`
reSpecialChars := regexp.MustCompile(`([^\\])[|&;<>$` + "`" + `{}]+`)
input = reSpecialChars.ReplaceAllString(input, "$1 ")
fmt.Printf("%s", input)
}
// Process command from shell hooks
func Proc(args []string) {
cwd, err := os.Getwd()
if err != nil {
logger.Log.Println("Error getting working directory:", err)
return
}
// TODO: ignores
// TODO: blacklists
// TODO: shifts?
Add(fmt.Sprintf("%s %s", cwd, strings.Join(args, " ")))
}
func Add(args string) {
var validPaths []string
// Iterate over the arguments and validate paths
for _, arg := range strings.Split(args, " ") {
if _, err := os.Stat(arg); err == nil {
validPaths = append(validPaths, arg)
}
}
// Convert paths to absolute form and simplify
var absolutePaths []string
for _, path := range validPaths {
absPath, err := filepath.Abs(path)
if err != nil {
fmt.Printf("Error converting path to absolute form: %v\n", err)
continue
}
// Simplify the path
cleanPath := filepath.Clean(absPath)
absolutePaths = append(absolutePaths, cleanPath)
}
for _, path := range absolutePaths {
AddToStore(path)
}
}