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
18 changes: 12 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,32 @@ var (
Bw *internal.BookWorm
tagFilter string
verboseMode bool
noFzf bool
)

var rootCmd = &cobra.Command{
Use: "bookworm",
Short: "Bookworm can manage your bookmarks from the command line.",
PreRunE: prGetCfg,
RunE: func(cmd *cobra.Command, args []string) error {
m := TeaModel()
p := tea.NewProgram(m)
if _, err := p.Run(); err != nil {
if verboseMode {
fmt.Println("Failed to run bubbletea!")
if Bw.Cfg.FzfIntegration && !noFzf {
return Bw.FzfOpen("")
} else {
m := TeaModel()
p := tea.NewProgram(m)
if _, err := p.Run(); err != nil {
if verboseMode {
fmt.Println("Failed to run bubbletea!")
}
return err
}
return err
}
return nil
},
}

func init() {
rootCmd.Flags().BoolVar(&noFzf, "no-fzf", false, "skip the fzf integration")
rootCmd.PersistentFlags().BoolVarP(&verboseMode, "verbose", "v", false, "Enable verbose output")
rootCmd.SilenceErrors = true
if verboseMode {
Expand Down
40 changes: 40 additions & 0 deletions internal/fzf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package internal

import (
"errors"
"fmt"
"os"
"os/exec"
)

// Uses Fzf to open a BookMark
// Inspo https://github.com/zk-org/zk/blob/dev/internal/adapter/fzf/fzf.go
func (b BookWorm) FzfOpen(tagFilter string) error {
fzfExec, err := exec.LookPath("fzf")
if err != nil {
return err
}
fzf := exec.Command(fzfExec)
fzf.Stderr = os.Stderr
stdinPhony, err := fzf.StdinPipe()
if err != nil {
return err
}
for _, s := range b.ListBookMarks(tagFilter) {
fmt.Fprintln(stdinPhony, s.Name)
}
fzfStdOut, err := fzf.Output()
if err != nil {
return err
}
bm := b.GetBookMark(string(fzfStdOut))
if bm == nil {
// The line ending doesn't work... sometimes
bm = b.GetBookMark(string(fzfStdOut[:len(fzfStdOut)-1]))
if bm == nil {
return errors.New("Bookmark not in bookworm & newline trick didn't work")
}
}
return OpenURL(bm.Link)
}

16 changes: 12 additions & 4 deletions internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ var (
dirPerms = os.FileMode(0700)
configPerms = os.FileMode(0666)
dbPerms = os.FileMode(0600)
verboseMode = false
)

type Config struct {
DbPath string `json:"dbpath"`
LastOpened string `json:"lastopened"`
DbPath string `json:"dbpath"`
LastOpened string `json:"lastopened"`
FzfIntegration bool `json:"fzf"`
}

func (c *Config) writeConfig(pathTo string) error {
Expand Down Expand Up @@ -123,9 +125,15 @@ func initConfig(pathTo string) (*Config, error) {
if err != nil {
return nil, err
}
var fzfThere = false
fzf, _ := exec.LookPath("fzf")
if fzf != "" {
fzfThere = true
}
cfg := &Config{
DbPath: getDbPath(pathTo),
LastOpened: "nothing... yet",
DbPath: getDbPath(pathTo),
LastOpened: "nothing... yet",
FzfIntegration: fzfThere,
}
err = cfg.writeConfig(pathTo)
return cfg, err
Expand Down