Skip to content

Commit

Permalink
better formatting and colors.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Feb 6, 2024
1 parent 29fcc3e commit a12c58a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
10 changes: 5 additions & 5 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var download = &cli.Command{
id := getRepositoryID()
pk := getRepositoryPublicKey()
if pk == "" || id == "" {
fmt.Fprintf(os.Stderr, "no repository id and pubkey found on `git config`, this command will only work with specific naddr or nevent patches.\n")
logf("no repository id and pubkey found on `git config`, this command will only work with specific naddr or nevent patches.\n")
}

limit := c.Int("limit")
Expand All @@ -59,7 +59,7 @@ var download = &cli.Command{
if arg != "" {
prefix, data, err := nip19.Decode(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid argument '%s': %s\n", arg, err)
logf("invalid argument '%s': %s\n", arg, err)
continue
}

Expand All @@ -75,15 +75,15 @@ var download = &cli.Command{
case "nevent":
ep := data.(nostr.EventPointer)
if ep.Kind != 0 && ep.Kind != PatchKind {
fmt.Fprintf(os.Stderr, "invalid argument %s: expected an encoded kind %d or nothing\n", arg, PatchKind)
logf("invalid argument %s: expected an encoded kind %d or nothing\n", arg, PatchKind)
continue
}
filter = nostr.Filter{IDs: []string{ep.ID}}
relays = append(relays, ep.Relays...)
case "naddr":
ep := data.(nostr.EntityPointer)
if ep.Kind != RepoAnnouncementKind {
fmt.Fprintf(os.Stderr, "invalid argument %s: expected an encoded kind %d\n", arg, RepoAnnouncementKind)
logf("invalid argument %s: expected an encoded kind %d\n", arg, RepoAnnouncementKind)
continue
}

Expand Down Expand Up @@ -120,7 +120,7 @@ var download = &cli.Command{
fileName := base + "/" + fmt.Sprintf("%s [%s] %s",
ie.CreatedAt.Time().Format(time.DateOnly), nevent[65:], subject)
if _, err := os.Stat(fileName); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "- downloaded patch %s from %s, saved as '%s'\n",
logf("- downloaded patch %s from %s, saved as '%s'\n",
ie.Event.ID, npub, color.New(color.Underline).Sprint(fileName))
if err := os.WriteFile(fileName, []byte(ie.Event.Content), 0644); err != nil {
return fmt.Errorf("failed to write '%s': %w", fileName, err)
Expand Down
24 changes: 15 additions & 9 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (

var subjectRegex = regexp.MustCompile(`(?m)^Subject: (.*)$`)

func logf(str string, args ...any) {
fmt.Fprintf(os.Stderr, fmt.Sprintf(str, args...))
}

func isPiped() bool {
stat, _ := os.Stdin.Stat()
return stat.Mode()&os.ModeCharDevice == 0
Expand Down Expand Up @@ -139,28 +143,30 @@ func git(args ...string) (string, error) {
func sprintRepository(repo *nostr.Event) string {
res := ""
npub, _ := nip19.EncodePublicKey(repo.PubKey)
res += "\nauthor: " + npub
res += "\nid: " + (*repo.Tags.GetFirst([]string{"d", ""}))[1]
res += "\n author: " + npub
res += "\n id: " + (*repo.Tags.GetFirst([]string{"d", ""}))[1]
res += "\n"
// TODO: more stuff
return res
return color.New(color.Bold).Sprint(res)
}

func sprintPatch(patch *nostr.Event) string {
res := ""
npub, _ := nip19.EncodePublicKey(patch.PubKey)
res += "\nid: " + patch.ID
res += "\nauthor: " + npub
res += "\n id: " + patch.ID
res += "\n author: " + npub

aTag := patch.Tags.GetFirst([]string{"a", ""})
if aTag != nil {
target := strings.Split((*aTag)[1], ":")
targetId := target[2]
targetNpub, _ := nip19.EncodePublicKey(target[1])
res += "\ntarget repo: " + targetId
res += "\ntarget author: " + targetNpub
res += "\n target repo: " + targetId
res += "\n target author: " + targetNpub
}
// TODO: more stuff

res = color.New(color.Bold).Sprint(res)
res += "\n\n" + patch.Content
// TODO: colors
return res
Expand Down Expand Up @@ -216,15 +222,15 @@ func promptDecrypt(ncryptsec1 string) (string, error) {

func ask(msg string, defaultValue string, shouldAskAgain func(answer string) bool) (string, error) {
return _ask(&readline.Config{
Prompt: color.YellowString(msg),
Prompt: color.CyanString(msg),
InterruptPrompt: "^C",
DisableAutoSaveHistory: true,
}, msg, defaultValue, shouldAskAgain)
}

func askPassword(msg string, shouldAskAgain func(answer string) bool) (string, error) {
config := &readline.Config{
Prompt: color.YellowString(msg),
Prompt: color.CyanString(msg),
InterruptPrompt: "^C",
DisableAutoSaveHistory: true,
EnableMask: true,
Expand Down
8 changes: 4 additions & 4 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,16 @@ var initRepo = &cli.Command{
relays := c.StringSlice("relay")
successRelays := make([]string, 0, len(relays))
for _, r := range relays {
fmt.Fprintf(os.Stderr, "publishing to %s...", r)
logf("publishing to %s...", r)
if relay, err := pool.EnsureRelay(r); err == nil {
if err := relay.Publish(ctx, evt); err != nil {
fmt.Fprintf(os.Stderr, " failed: %s\n", err)
logf(" failed: %s\n", err)
} else {
fmt.Fprintf(os.Stderr, "done\n")
logf("done\n")
successRelays = append(successRelays, r)
}
} else {
fmt.Fprintf(os.Stderr, " failed: %s\n", err)
logf(" failed: %s\n", err)
}
}

Expand Down
15 changes: 9 additions & 6 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"github.com/fatih/color"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/urfave/cli/v3"
Expand Down Expand Up @@ -168,6 +169,7 @@ var send = &cli.Command{
// publish all the patches
for _, evt := range events {
if bunker != nil {
logf(color.YellowString("signing event with bunker..."))
err = bunker.SignEvent(ctx, evt)
if err != nil {
return fmt.Errorf("error signing event with bunker: %w", err)
Expand All @@ -180,24 +182,24 @@ var send = &cli.Command{
}

goodRelays := make([]string, 0, len(targetRelays))
fmt.Fprintf(os.Stderr, "\nwill publish event\n%s", sprintPatch(evt))
logf("\n%s", sprintPatch(evt))
if confirm("proceed to publish the event? ") {
for _, r := range targetRelays {
relay, err := pool.EnsureRelay(r)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to connect to '%s': %s\n", r, err)
logf("failed to connect to '%s': %s\n", r, err)
continue
}
if err := relay.Publish(ctx, *evt); err != nil {
fmt.Fprintf(os.Stderr, "failed to publish to '%s': %s\n", r, err)
logf("failed to publish to '%s': %s\n", r, err)
continue
}
goodRelays = append(goodRelays, relay.URL)
}
}
if len(goodRelays) == 0 {
fmt.Println(evt)
fmt.Fprintln(os.Stderr, "didn't publish the event")
logf(color.RedString("didn't publish the event\n"))
continue
}

Expand All @@ -216,7 +218,7 @@ func getAndApplyTargetRepository(
extraRelays []string,
) (patchRelays []string, err error) {
if c.Bool("dangling") {
fmt.Fprintf(os.Stderr, "this patch won't target any specific repository")
logf("this patch won't target any specific repository")
return nil, nil
}

Expand Down Expand Up @@ -264,7 +266,8 @@ func getAndApplyTargetRepository(
return nil, fmt.Errorf("couldn't find event for %s", filter)
}

fmt.Fprintf(os.Stderr, "found upstream repository %s\n%s\n", target, sprintRepository(repo.Event))
logf("%s %s\n%s\n", color.YellowString("found upstream repository"),
target, sprintRepository(repo.Event))

if stored != target {
if confirm("store it as your main upstream target? ") {
Expand Down

0 comments on commit a12c58a

Please sign in to comment.