Skip to content

Commit

Permalink
Added time and user matching
Browse files Browse the repository at this point in the history
  • Loading branch information
terminationshock committed Jun 25, 2024
1 parent ad87842 commit 7751c0e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import (
"fmt"
"os"
"os/user"
"regexp"
"strings"
)

type Config struct {
pattern *regexp.Regexp
patternFunc func(string) bool
patternFuncInfo string
program string
programArgs []string
filter bool
Expand All @@ -25,6 +27,7 @@ func NewConfig() *Config {
patternFunc: func(_ string) bool {
return true
},
patternFuncInfo: "",
program: "",
programArgs: []string{},
filter: false,
Expand Down Expand Up @@ -57,12 +60,15 @@ func NewConfig() *Config {
inputPattern = "^.*$"
case "--git-commit-hash":
inputPattern = "\\b[0-9a-f]{7,40}\\b"
case "--time":
inputPattern = "(?:0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?"
case "--filename":
inputPattern = "[^\\s:]+"
config.patternFunc = func(p string) bool {
stat, err := os.Stat(p)
return err == nil && !stat.IsDir()
}
config.patternFuncInfo = "valid file"
case "--filename-lineno":
inputPattern = "[^\\s:]+:[1-9][0-9]*"
config.patternFunc = func(p string) bool {
Expand All @@ -74,12 +80,21 @@ func NewConfig() *Config {
stat, err := os.Stat(filename)
return err == nil && !stat.IsDir()
}
config.patternFuncInfo = "valid file"
case "--dirname":
inputPattern = "[^\\s:]+"
config.patternFunc = func(p string) bool {
stat, err := os.Stat(p)
return err == nil && stat.IsDir()
}
config.patternFuncInfo = "valid directory"
case "--user":
inputPattern = "[^\\s]+"
config.patternFunc = func(p string) bool {
u, err := user.Lookup(p)
return err == nil && u.Username == p
}
config.patternFuncInfo = "valid user"
case "--completion":
if len(os.Args) > 3 {
printCompletion(os.Args[2], os.Args[3])
Expand Down
14 changes: 10 additions & 4 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ func PrintHelp() {
fmt.Println("\nKeywords to replace PATTERN:")
fmt.Println("\n --line Match the whole line")
fmt.Println(" --git-commit-hash Match a Git commit hash")
fmt.Println(" --time Match a time as [H]H:MM[:SS]")
fmt.Println(" --filename Match the name of an existing file")
fmt.Println(" --filename-lineno Match the name of an existing file and a line number,")
fmt.Println(" separated by a colon")
fmt.Println(" --dirname Match the name of an existing directory")
fmt.Println(" --user Match the name of an existing user")
fmt.Println("\nOther keyword OPTIONS:")
fmt.Println("\n --show-output Show the output (both stdout and stderr) of COMMAND")
fmt.Println(" --ignore-error Ignore any error occurring during the execution of COMMAND")
Expand Down Expand Up @@ -269,16 +271,20 @@ func (pageList *PageList) setStatus(exitStatus string) {
if config.pattern != nil {
numMatches := pageList.itemList.NumMatches()
if numMatches > 1 {
info += fmt.Sprintf("%d matches with %s%s", numMatches, config.pattern, space)
info += fmt.Sprintf("%d matches with %s", numMatches, config.pattern)
} else if numMatches == 1 {
info += fmt.Sprintf("1 match with %s%s", config.pattern, space)
info += fmt.Sprintf("1 match with %s", config.pattern)
} else {
info += fmt.Sprintf("No match with %s%s", config.pattern, space)
info += fmt.Sprintf("No match with %s", config.pattern)
}
}

if config.patternFuncInfo != "" {
info += fmt.Sprintf(" as %s", config.patternFuncInfo)
}

index := pageList.list.GetCurrentItem()
info += fmt.Sprintf("Line %d of %d", index + 1, pageList.list.GetItemCount())
info += fmt.Sprintf("%sLine %d of %d", space, index + 1, pageList.list.GetItemCount())
if config.program != "" && pageList.itemList.Get(index).HasMatch() {
info += space + pageList.itemList.Get(index).PrintCommand()
if exitStatus != "" {
Expand Down
14 changes: 13 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,24 @@ function run() {
echo "test1" > test/EXPECT_$1
diff test/RESULT_$1 test/EXPECT_$1
;;
34)
echo -e "test1 0:12\ntest2 23:59:59\n" | ./lisst --time > test/RESULT_$1
test $? -ne 0 && exit 1
echo -e "test1 [::-][::r]0:12[::-]\ntest2 [::-][::r]23:59:59[::-]" > test/EXPECT_$1
diff test/RESULT_$1 test/EXPECT_$1
;;
35)
echo -e "test1 foobar\ntest2 $USER\n" | ./lisst --user > test/RESULT_$1
test $? -ne 0 && exit 1
echo -e "test1 foobar\ntest2 [::-][::r]$USER[::-]" > test/EXPECT_$1
diff test/RESULT_$1 test/EXPECT_$1
;;
esac
}

if [ $# -eq 0 ]; then
result=0
for i in {1..33}; do
for i in {1..35}; do
echo "Test $i"
run $i || { result=1; echo " FAILED"; }
done
Expand Down

0 comments on commit 7751c0e

Please sign in to comment.