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
|
package main
import (
"bufio"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"slices"
"github.com/alexflint/go-arg"
"github.com/ayyansea/uptfs/internal/config"
"github.com/ayyansea/uptfs/internal/filter"
"github.com/ayyansea/uptfs/internal/token"
)
var args struct {
ConfigFile string `arg:"-c" help:"path to config file" default:""`
Verbose bool `arg:"-v" help:"toggle verbose (debug) mode"`
}
func errExit(err error) {
fmt.Println(err)
os.Exit(1)
}
func main() {
arg.MustParse(&args)
if args.Verbose {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
slog.Debug("uptfs started")
var configFilePath string
var err error
if args.ConfigFile != "" {
configFilePath, err = filepath.Abs(args.ConfigFile)
slog.Debug("config file path: " + configFilePath)
}
if err != nil {
errExit(err)
}
var config config.Config
config.LoadConfig(configFilePath)
var inputString string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
inputString = inputString + scanner.Text()
}
if inputString == "" {
err := errors.New("the input string is empty")
fmt.Printf("%v\n", err)
os.Exit(1)
}
additionalDelimeters := []string{",", ".", " "}
var delimeterString string
for _, element := range additionalDelimeters {
if element == " " {
element = "<space>"
}
delimeterString = delimeterString + element + " "
}
slog.Debug("delimeters: " + delimeterString)
tempword := ""
var tokenlist token.LinkedTokenList
for index, character := range inputString {
slog.Debug("current character: " + string(character))
if slices.Contains(additionalDelimeters, string(character)) {
slog.Debug("current character is a delimeter")
if len(tempword) != 0 {
slog.Debug("word is longer than 0 characters, filtering it")
for _, filterName := range config.Filters {
_, ok := filter.FilterList[filterName]
if ok {
slog.Debug("using filter " + filterName)
currentfilter := filter.FilterList[filterName]()
tempword = currentfilter.Filter(tempword)
} else {
slog.Debug("there's no filter named " + filterName)
continue
}
}
slog.Debug("filtered word: " + tempword)
tokenlist.AddToken(tempword)
slog.Debug("adding delimeter")
tokenlist.AddToken(string(character))
slog.Debug("resetting current word to an empty string")
tempword = ""
continue
}
slog.Debug("current word is a zero-length string")
slog.Debug("adding delimeter")
tokenlist.AddToken(string(character))
tempword = ""
continue
}
slog.Debug("current character is not a delimeter, adding to word")
tempword = tempword + string(character)
if index == len(inputString)-1 {
slog.Debug("string ended, filtering current word")
for _, filterName := range config.Filters {
_, ok := filter.FilterList[filterName]
if ok {
slog.Debug("using filter " + filterName)
currentfilter := filter.FilterList[filterName]()
tempword = currentfilter.Filter(tempword)
} else {
slog.Debug("there's no filter named " + filterName)
continue
}
}
slog.Debug("filtered word: " + tempword)
tokenlist.AddToken(tempword)
tempword = ""
}
}
result := ""
for current := tokenlist.GetHead(); current != nil; current = current.GetNextToken() {
result = result + current.GetContent()
}
fmt.Println(result)
slog.Debug("uptfs finished")
}
|