diff --git a/src/config.go b/src/config.go index 7893e16..d0485e9 100644 --- a/src/config.go +++ b/src/config.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "os/user" "regexp" "strings" ) @@ -10,6 +11,7 @@ import ( type Config struct { pattern *regexp.Regexp patternFunc func(string) bool + patternFuncInfo string program string programArgs []string filter bool @@ -25,6 +27,7 @@ func NewConfig() *Config { patternFunc: func(_ string) bool { return true }, + patternFuncInfo: "", program: "", programArgs: []string{}, filter: false, @@ -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 { @@ -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]) diff --git a/src/main.go b/src/main.go index c067da7..244f0ff 100644 --- a/src/main.go +++ b/src/main.go @@ -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") @@ -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 != "" { diff --git a/test.sh b/test.sh index 7e4e9e0..8cc3c76 100755 --- a/test.sh +++ b/test.sh @@ -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