Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bookworm
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
all:
go install

install:
go install

build:
go build
41 changes: 0 additions & 41 deletions cmd/completions.go

This file was deleted.

1 change: 1 addition & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var delCmd = &cobra.Command{
Short: "Delete no good bookmarks.",
Args: cobra.ExactArgs(1),
Aliases: []string{"rm"},
PreRunE: prGetCfg,
ValidArgsFunction: getNamesCmp,
Run: func(cmd *cobra.Command, args []string) {
Bw.DeleteBookMark(args[0])
Expand Down
23 changes: 23 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(initCmd)
}

var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize Bookworm.",
PreRunE: prInitCfg,
Run: func(cmd *cobra.Command, args []string) {
if Bw == nil {
panic("BW object is nil")
}
fmt.Println("Successfully Initialized")
},
}
10 changes: 3 additions & 7 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cmd

import (
// "fmt"
"github.com/spf13/cobra"
"slices"
)

func init() {
Expand All @@ -13,18 +11,16 @@ func init() {
listCmd.RegisterFlagCompletionFunc("tag", getTagsCmp)
}

// Inspo `gh repo list`
var listCmd = &cobra.Command{
Use: "list",
Args: cobra.ExactArgs(0),
Short: "List your bookmarks.",
Aliases: []string{"ls"},
PreRunE: prGetCfg,
ValidArgsFunction: nonCmp,
Run: func(cmd *cobra.Command, args []string) {
for _, b := range Bw.Cfg.BookMarks {
if tagFilter == "" || slices.Contains(b.Tags, tagFilter) {
b.Println()
}
for _, b := range Bw.ListBookMarks(tagFilter) {
b.Println()
}
},
}
1 change: 1 addition & 0 deletions cmd/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var makeCmd = &cobra.Command{
Short: "Make new bookmarks.",
Args: cobra.ExactArgs(2),
Aliases: []string{"mk", "new"},
PreRunE: prGetCfg,
ValidArgsFunction: nonCmp,
Run: func(cmd *cobra.Command, args []string) {
if !internal.IsValidUrl(args[1]) {
Expand Down
5 changes: 3 additions & 2 deletions cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ var openCmd = &cobra.Command{
Use: "open",
Short: "Open a Bookmark.",
Aliases: []string{"go"},
PreRunE: prGetCfg,
Args: cobra.ExactArgs(1),
ValidArgsFunction: getNamesCmp,
Run: func(cmd *cobra.Command, args []string) {
bm, ok := Bw.Cfg.BookMarks[args[0]]
if !ok {
bm := Bw.GetBookMark(args[0])
if bm == nil {
fmt.Println("Couldn't Find BookMark!")
return
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ package cmd

import (
"fmt"

tea "github.com/charmbracelet/bubbletea"
"github.com/dandeandean/bookworm/internal"
"github.com/spf13/cobra"
"os"
)

var (
Bw = internal.Init()
// Global BookWorm
Bw *internal.BookWorm
tagFilter string
)

var tagFilter string

var rootCmd = &cobra.Command{
Use: "bookworm",
Short: "Bookworm can manage your bookmarks from the command line.",
Use: "bookworm",
Short: "Bookworm can manage your bookmarks from the command line.",
PreRunE: prGetCfg,
Run: func(cmd *cobra.Command, args []string) {
m := TeaModel()
p := tea.NewProgram(m)
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
fmt.Println("Something Horrible Happened!", err)
}
}
1 change: 1 addition & 0 deletions cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var tagCmd = &cobra.Command{
Use: "tag name tags...",
Args: cobra.MinimumNArgs(2),
Short: "Tag boomarks with tags.",
PreRunE: prGetCfg,
ValidArgsFunction: getNamesThenTagsCmp,
Run: func(cmd *cobra.Command, args []string) {
err := Bw.SetTags(args[0], args[1:])
Expand Down
73 changes: 73 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cmd

import (
"fmt"
"os"
"slices"

"github.com/dandeandean/bookworm/internal"
"github.com/spf13/cobra"
)

func prGetCfg(_ *cobra.Command, _ []string) error {
if Bw != nil {
return nil
}
var err error
Bw, err = internal.Get()
if err != nil {
fmt.Println("Couldn't get Config, please run bookworm init!")
os.Exit(2)
return err
}
return nil
}

func prInitCfg(cmd *cobra.Command, args []string) error {
var err error
Bw, err = internal.Init()
if err != nil {
fmt.Println("Failed to create config!")
os.Exit(2)
return err
}
return nil
}

func nonCmp(_ *cobra.Command, _ []string, _ string) ([]cobra.Completion, cobra.ShellCompDirective) {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

func getNamesThenTagsCmp(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
if len(args) == 0 {
return getNamesCmp(cmd, args, toComplete)
}
return getTagsCmp(cmd, args, toComplete)
}

func getTagsCmp(cmd *cobra.Command, args []string, _ string) ([]cobra.Completion, cobra.ShellCompDirective) {
prGetCfg(cmd, args)
bms := Bw.ListBookMarks("")
out := make([]string, len(bms))
for _, b := range bms {
for _, tag := range b.Tags {
if !slices.Contains(args, tag) {
out = append(out, tag)
}
}
}
return out, cobra.ShellCompDirectiveNoFileComp
}

func getNamesCmp(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
if len(args) > 0 {
return nonCmp(cmd, args, toComplete)
}
prGetCfg(cmd, args)
bms := Bw.ListBookMarks("")
out := make([]string, 0)
for _, k := range bms {
out = append(out, k.Name)
}
return out, cobra.ShellCompDirectiveNoFileComp
}
15 changes: 6 additions & 9 deletions cmd/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import (
)

type model struct {
choices []internal.BookMark // items on the to-do list
cursor int // which to-do list item our cursor is pointing at
selected map[int]struct{} // which to-do items are selected
choices []*internal.BookMark // items on the to-do list
cursor int // which to-do list item our cursor is pointing at
selected map[int]struct{} // which to-do items are selected
}

func TeaModel() model {
choices := make([]internal.BookMark, 0)
for _, bm := range Bw.Cfg.BookMarks {
choices = append(choices, *bm)
}
choices := Bw.ListBookMarks("")

return model{
choices: choices,
selected: make(map[int]struct{}),
Expand Down Expand Up @@ -67,12 +65,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
delete(m.selected, m.cursor)
} else {
m.selected[m.cursor] = struct{}{}
internal.OpenURL(m.choices[m.cursor].Link)
err := internal.OpenURL(m.choices[m.cursor].Link)
if err != nil {
panic(err)
}
Bw.SetLastOpened(m.choices[m.cursor])
Bw.SetLastOpened(*m.choices[m.cursor])
return m, tea.Quit
}
}
Expand Down
Loading