If the argument isn't an ID, use fzf to find an ID
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
329d6d34e0
commit
3c7a1dc3c1
5 changed files with 118 additions and 20 deletions
41
cmd/edit.go
41
cmd/edit.go
|
@ -2,8 +2,10 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
@ -24,17 +26,46 @@ zet edit|e latest
|
||||||
}
|
}
|
||||||
|
|
||||||
var id int
|
var id int
|
||||||
|
var query string
|
||||||
|
|
||||||
if args[0] == "latest" {
|
if args[0] == "latest" {
|
||||||
id = lib.GetLatestZet()
|
id = lib.GetLatestZet()
|
||||||
} else {
|
|
||||||
i, err := strconv.Atoi(args[0])
|
|
||||||
|
|
||||||
if err != nil {
|
lib.EditZet(id)
|
||||||
os.Exit(1)
|
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
id = i
|
if id, err := strconv.Atoi(args[0]); err == nil {
|
||||||
|
lib.EditZet(id)
|
||||||
|
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
query = args[0]
|
||||||
|
|
||||||
|
ids := lib.SearchZets(query)
|
||||||
|
|
||||||
|
if len(ids) == 1 {
|
||||||
|
lib.EditZet(ids[0])
|
||||||
|
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
zets := lib.ParseZetList(ids)
|
||||||
|
|
||||||
|
selected, err := lib.SelectWithFzf(zets)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("No zet selected.")
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.SplitN(selected, " ", 2)
|
||||||
|
|
||||||
|
id, err = strconv.Atoi(parts[0])
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
lib.EditZet(id)
|
lib.EditZet(id)
|
||||||
|
|
41
cmd/view.go
41
cmd/view.go
|
@ -2,8 +2,10 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
@ -24,17 +26,46 @@ zet view|v latest
|
||||||
}
|
}
|
||||||
|
|
||||||
var id int
|
var id int
|
||||||
|
var query string
|
||||||
|
|
||||||
if args[0] == "latest" {
|
if args[0] == "latest" {
|
||||||
id = lib.GetLatestZet()
|
id = lib.GetLatestZet()
|
||||||
} else {
|
|
||||||
i, err := strconv.Atoi(args[0])
|
|
||||||
|
|
||||||
if err != nil {
|
fmt.Println(lib.ViewZet(id))
|
||||||
os.Exit(1)
|
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
id = i
|
if id, err := strconv.Atoi(args[0]); err == nil {
|
||||||
|
fmt.Println(lib.ViewZet(id))
|
||||||
|
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
query = args[0]
|
||||||
|
|
||||||
|
ids := lib.SearchZets(query)
|
||||||
|
|
||||||
|
if len(ids) == 1 {
|
||||||
|
fmt.Println(lib.ViewZet(ids[0]))
|
||||||
|
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
zets := lib.ParseZetList(ids)
|
||||||
|
|
||||||
|
selected, err := lib.SelectWithFzf(zets)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("No zet selected.")
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.SplitN(selected, " ", 2)
|
||||||
|
|
||||||
|
id, err = strconv.Atoi(parts[0])
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(lib.ViewZet(id))
|
fmt.Println(lib.ViewZet(id))
|
||||||
|
|
9
internal/lib/ansi.go
Normal file
9
internal/lib/ansi.go
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
package lib
|
||||||
|
|
||||||
|
import "regexp"
|
||||||
|
|
||||||
|
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)
|
||||||
|
|
||||||
|
func StripANSI(s string) string {
|
||||||
|
return ansiRegex.ReplaceAllString(s, "")
|
||||||
|
}
|
34
internal/lib/fzf.go
Normal file
34
internal/lib/fzf.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package lib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SelectWithFzf(items []string) (string, error) {
|
||||||
|
cmd := exec.Command("fzf", "--ansi")
|
||||||
|
|
||||||
|
var input bytes.Buffer
|
||||||
|
|
||||||
|
for _, item := range items {
|
||||||
|
input.WriteString(item + "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Stdin = &input
|
||||||
|
|
||||||
|
var output bytes.Buffer
|
||||||
|
cmd.Stdout = &output
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
|
err := cmd.Run()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
selected := strings.TrimSpace(output.String())
|
||||||
|
|
||||||
|
return selected, nil
|
||||||
|
}
|
|
@ -3,13 +3,10 @@ package lib
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)
|
|
||||||
|
|
||||||
type Item struct {
|
type Item struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
|
@ -19,7 +16,7 @@ func AsJSON(zets []string) (string, error) {
|
||||||
var items []Item
|
var items []Item
|
||||||
|
|
||||||
for _, entry := range zets {
|
for _, entry := range zets {
|
||||||
cleanEntry := stripANSI(entry)
|
cleanEntry := StripANSI(entry)
|
||||||
parts := strings.SplitN(cleanEntry, " ", 2)
|
parts := strings.SplitN(cleanEntry, " ", 2)
|
||||||
|
|
||||||
id, _ := strconv.Atoi(parts[0])
|
id, _ := strconv.Atoi(parts[0])
|
||||||
|
@ -42,7 +39,3 @@ func AsJSON(zets []string) (string, error) {
|
||||||
|
|
||||||
return string(jsonData), nil
|
return string(jsonData), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func stripANSI(s string) string {
|
|
||||||
return ansiRegex.ReplaceAllString(s, "")
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue