Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom select prompt to delete, update commands #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions cmd/brag/delete/delete.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package delete

import (
"cronicle/utils"
"fmt"
"strconv"
"cronicle/utils/entries"

"github.com/spf13/cobra"
)

func New() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [ID!]",
Use: "delete <ID>",
Short: "delete a brag entry",
Long: "delete a brag entry",
Run: run,
Expand All @@ -20,14 +18,5 @@ func New() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) {
files := utils.GetAllFiles("brag")

n, err := strconv.Atoi(args[0])
if err != nil || n == 0 || n > len(files) {
fmt.Printf("Invalid argument")
return
}

utils.DeleteFile(files[n-1].Name(), "brag")
utils.ListFiles("brag")
entries.DeleteEntry(args, "brag")
}
16 changes: 2 additions & 14 deletions cmd/brag/update/update.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package update

import (
"cronicle/utils"
"fmt"
"strconv"
"cronicle/utils/entries"

"github.com/spf13/cobra"
)
Expand All @@ -20,15 +18,5 @@ func New() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) {
files := utils.GetAllFiles("brag")

n, err := strconv.Atoi(args[0])
if err != nil || n == 0 || n > len(files) {
fmt.Printf("Invalid argument")
return
}

path := utils.GetPath([]string{"brag", files[n-1].Name()})

utils.EditFile(path)
entries.EditEntry(args, "brag")
}
17 changes: 3 additions & 14 deletions cmd/daily/delete/delete.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package delete

import (
"cronicle/utils"
"fmt"
"strconv"
"cronicle/utils/entries"

"github.com/spf13/cobra"
)

func New() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [ID!]",
Use: "delete <ID>",
Short: "delete a daily file",
Long: "delete a daily file",
Run: run,
Expand All @@ -20,14 +18,5 @@ func New() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) {
files := utils.GetAllFiles("daily")

n, err := strconv.Atoi(args[0])
if err != nil || n == 0 || n > len(files) {
fmt.Printf("Invalid argument")
return
}

utils.DeleteFile(files[n-1].Name(), "daily")
utils.ListFiles("daily")
entries.DeleteEntry(args, "daily")
}
18 changes: 3 additions & 15 deletions cmd/daily/update/update.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package update

import (
"cronicle/utils"
"fmt"
"strconv"
"cronicle/utils/entries"

"github.com/spf13/cobra"
)

func New() *cobra.Command {
cmd := &cobra.Command{
Use: "update [ID!]",
Use: "update <ID>",
Short: "update a daily entry",
Long: "update a daily entry",
Run: run,
Expand All @@ -20,15 +18,5 @@ func New() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) {
files := utils.GetAllFiles("daily")

n, err := strconv.Atoi(args[0])
if err != nil || n == 0 || n > len(files) {
fmt.Printf("Invalid argument")
return
}

path := utils.GetPath([]string{"daily", files[n-1].Name()})

utils.EditFile(path)
entries.EditEntry(args, "daily")
}
16 changes: 2 additions & 14 deletions cmd/todo/complete/complete.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package complete

import (
"cronicle/utils"
"cronicle/utils/todo"
"fmt"
"strconv"

"github.com/spf13/cobra"
)

func New() *cobra.Command {
cmd := &cobra.Command{
Use: "complete [ID!]",
Use: "complete <ID>",
Short: "complete a todo entry",
Long: "complete a todo entry",
Run: run,
Expand All @@ -21,14 +18,5 @@ func New() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) {
files := utils.GetAllFiles("todo")

n, err := strconv.Atoi(args[0])
if err != nil || n == 0 || n > len(files) {
fmt.Printf("Invalid argument")
return
}

todo.MarkCompleted(files[n-1])
todo.ListTodos()
todo.CompleteTodo()
}
19 changes: 3 additions & 16 deletions cmd/todo/delete/delete.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package delete

import (
"cronicle/utils"
"cronicle/utils/todo"
"fmt"
"strconv"
"cronicle/utils/entries"

"github.com/spf13/cobra"
)

func New() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [ID!]",
Use: "delete <ID>",
Short: "delete a todo entry",
Long: "delete a todo entry",
Run: run,
Expand All @@ -21,15 +18,5 @@ func New() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) {

files := utils.GetAllFiles("todo")

n, err := strconv.Atoi(args[0])
if err != nil || n == 0 || n > len(files) {
fmt.Printf("Invalid argument")
return
}

utils.DeleteFile(files[n-1].Name(), "todo")
todo.ListTodos()
entries.DeleteEntry(args, "todo")
}
20 changes: 5 additions & 15 deletions cmd/todo/update/update.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package update

import (
"cronicle/utils"
"fmt"
"strconv"
"cronicle/utils/entries"
"log"

"github.com/spf13/cobra"
)

func New() *cobra.Command {
cmd := &cobra.Command{
Use: "update [ID!]",
Use: "update <ID>",
Short: "update a todo entry",
Long: "update a todo entry",
Run: run,
Expand All @@ -20,15 +19,6 @@ func New() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) {
files := utils.GetAllFiles("todo")

n, err := strconv.Atoi(args[0])
if err != nil || n == 0 || n > len(files) {
fmt.Printf("Invalid argument")
return
}

path := utils.GetPath([]string{"todo", files[n-1].Name()})

utils.EditFile(path)
log.Println(args)
entries.EditEntry(args, "todo")
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ require (
github.com/charmbracelet/lipgloss v0.5.0
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f // indirect
github.com/google/uuid v1.3.0
github.com/google/uuid v1.3.0 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03 // indirect
github.com/sergi/go-diff v1.0.0 // indirect
github.com/spf13/cobra v1.3.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ github.com/charmbracelet/lipgloss v0.4.0/go.mod h1:vmdkHvce7UzX6xkyf4cca8WlwdQ5R
github.com/charmbracelet/lipgloss v0.5.0 h1:lulQHuVeodSgDez+3rGiuxlPVXSnhth442DATR2/8t8=
github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
Expand Down Expand Up @@ -284,6 +285,8 @@ github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i
github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand Down Expand Up @@ -541,6 +544,7 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
4 changes: 4 additions & 0 deletions ui/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ const (
ERROR_CLOSE_FILE = "Darn, error closing file: %w"
ERROR_LIST_FILE = "Darn, error listing files: %w"
ERROR_DELETE_FILE = "Darn, error deleting file: %w"
ERROR_PROMPT = "Darn, error with prompt: %w"
)

const MaxLengthDisplayOption = 20
const MaxLengthDetails = 50
76 changes: 76 additions & 0 deletions utils/entries/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package entries
import (
"cronicle/ui/constants"
"cronicle/utils"
"cronicle/utils/prompts"
"errors"
"fmt"
"io/fs"
"log"
"os"
"strings"
Expand Down Expand Up @@ -85,3 +87,77 @@ func composeEntry(w utils.WriteParams, t string) string {

return output.String()
}

func EditEntry(args []string, t string) {
files := utils.GetAllFiles(t)
var id int

if len(args) > 0 {
// user has given an arguement
argId := utils.GetIdFromArg(args, files)
if argId == -1 {
fmt.Printf("Invalid argument")
return
}
id = argId
} else {
// user selects from options
id = GetIdFromOptions(files, t, "update")
}

path := utils.GetPath([]string{t, files[id].Name()})

utils.EditFile(path)
}

func DeleteEntry(args []string, t string) {
files := utils.GetAllFiles(t)
var id int

if len(args) > 0 {
// user has given an arguement
argId := utils.GetIdFromArg(args, files)
if argId == -1 {
fmt.Printf("Invalid argument")
return
}
id = argId
} else {
// user selects from options
id = GetIdFromOptions(files, t, "delete")
}

utils.DeleteFile(files[id].Name(), t)
}

func GetIdFromOptions(files []fs.FileInfo, t string, action string) int {
var id int
if t == "todo" {
id = GetIdFromTodoOptions(files, t, action)
} else {
id = GetIdFromEntryOptions(files, t, action)
}
return id
}

func GetIdFromEntryOptions(files []fs.FileInfo, t string, action string) int {
options := prompts.GetEntryDisplayOptions(t, files)
i, err := prompts.SelectEntry(options, action)

if err != nil {
log.Fatal(constants.ERROR_PROMPT, err)
}

return i
}

func GetIdFromTodoOptions(files []fs.FileInfo, t string, action string) int {
options := prompts.GetTodoDisplayOptions(files)
i, err := prompts.SelectTodo(options, action)

if err != nil {
log.Fatal(constants.ERROR_PROMPT, err)
}

return i
}
Loading