From a12c58a800126ef18eb9b99bb12dbf6d3cd47916 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 6 Feb 2024 08:05:05 -0300 Subject: [PATCH] better formatting and colors. --- download.go | 10 +++++----- helpers.go | 24 +++++++++++++++--------- init.go | 8 ++++---- send.go | 15 +++++++++------ 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/download.go b/download.go index 5cd5c39..548cd9a 100644 --- a/download.go +++ b/download.go @@ -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") @@ -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 } @@ -75,7 +75,7 @@ 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}} @@ -83,7 +83,7 @@ var download = &cli.Command{ 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 } @@ -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) diff --git a/helpers.go b/helpers.go index e6bc8b0..92dfa08 100644 --- a/helpers.go +++ b/helpers.go @@ -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 @@ -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 @@ -216,7 +222,7 @@ 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) @@ -224,7 +230,7 @@ func ask(msg string, defaultValue string, shouldAskAgain func(answer string) boo 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, diff --git a/init.go b/init.go index e7fa7ad..ab2ea10 100644 --- a/init.go +++ b/init.go @@ -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) } } diff --git a/send.go b/send.go index 745cfc2..d8e1b89 100644 --- a/send.go +++ b/send.go @@ -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" @@ -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) @@ -180,16 +182,16 @@ 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) @@ -197,7 +199,7 @@ var send = &cli.Command{ } 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 } @@ -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 } @@ -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? ") {