Skip to content

Commit

Permalink
search filter record by title
Browse files Browse the repository at this point in the history
  • Loading branch information
ac5tin committed Aug 28, 2019
1 parent 2fc0e15 commit 7f3ee40
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ FIELD = title / username / password
/s
```

### find / search record
```
/f foobar
```
#### To reset search text
```
/f
```


### navigate pages
```
/n next page
Expand Down Expand Up @@ -77,5 +87,6 @@ go build -o bin/simplepwd
- [x] Show password in REPL
- [x] Remove record
- [x] Edit record
- [x] Search record
- [ ] Research stronger Encrypt/Decrypt
- [ ] Error handling
Binary file added abc
Binary file not shown.
Binary file modified bin/simplepwd
Binary file not shown.
Binary file modified bin/simplepwd.exe
Binary file not shown.
45 changes: 41 additions & 4 deletions repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

var page = 0
var searchTxt = ""

const printlimit int = 5

Expand Down Expand Up @@ -61,14 +62,30 @@ func cmdParse(cmd *[]string, data *[]standard) {
}
(*data)[posnum] = updated
break
case "/f":
// find/search
searchTxt = (*cmd)[1]
break
default:
break
}
}

func filter(data *[]standard) []standard {
var retme []standard
for _, v := range *data {
// search text
if strings.Contains(v.Title, searchTxt) {
retme = append(retme, v)
}
}
return retme
}

func currDataPrinter(data *[]standard) {
fmt.Printf("Page %d of %d\n", page+1, len(*data)/printlimit+1)
for i, s := range (*data)[page*printlimit:] {
filtered := filter(data)
fmt.Printf("Page %d of %d\n", page+1, len(filtered)/printlimit+1)
for i, s := range (filtered)[page*printlimit:] {
if i == printlimit {
break
}
Expand All @@ -82,6 +99,12 @@ func cls() {
cmd.Run()
}

func printFilter() {
if searchTxt != "" {
fmt.Printf("Searching : %s (Enter /f to reset)\n", searchTxt)
}
}

func printStd(content *standard, r *bufio.Reader) {
for {
cls()
Expand Down Expand Up @@ -117,6 +140,7 @@ func repl(data *[]byte) {
cls()
println("simplepwd 📝")
println("============")
printFilter()
currDataPrinter(&arrdata)
print("> ")
inputraw, err := reader.ReadString('\n')
Expand All @@ -125,14 +149,15 @@ func repl(data *[]byte) {
}
input := strings.TrimSpace(inputraw)
switch input {
// exact matches (command wiht no arguments)
case ":q", "/q", "bye", "quit", "exit":
println("Bye ~")
return
case "clear":
cls()
break
case "/s":
// save
// save/write
println("-- save --")
d, err := json.Marshal(arrdata)
if err != nil {
Expand All @@ -152,21 +177,33 @@ func repl(data *[]byte) {
page--
}
break
case "/f":
// find/search (CLEAR)
searchTxt = ""
break
default:
// parse
// first check there are spaces
// if spaces exist, probably means user wants to enter a command (not select a number)
cmd := strings.Split(input, " ")
switch len(cmd) {
case 0:
// user didnt enter anything,
break
case 1:
// user only entered 1 argument, no spaces
// probably number
i, err := strconv.Atoi(cmd[0])
if err != nil {
// no, user didnt enter a number
break
}
printStd(&arrdata[i-1], reader)
// user entered a number, print info
filteredData := filter(&arrdata)
printStd(&filteredData[i-1], reader)
break
default:
// user entered more than 1 argument, probably a command (with arguments)
cmdParse(&cmd, &arrdata)
break
}
Expand Down

0 comments on commit 7f3ee40

Please sign in to comment.