feat(output): implement configurable color and emoji output

Add new output formatting system with flags for color and emoji control:
- Introduce OutputConfig and Writer structs for flexible output handling
- Add --colors and --emoji/--no-emoji global flags
- Refactor commands to use new Writer for consistent formatting
- Separate error content from presentation for better flexibility
This commit is contained in:
Yar Kravtsov
2025-08-03 14:33:44 +03:00
parent 57839c795e
commit 7f10e1ce8a
19 changed files with 1269 additions and 237 deletions

View File

@@ -1,7 +1,10 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/yarlson/lnk/internal/core"
)
@@ -14,8 +17,8 @@ func newPullCmd() *cobra.Command {
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
host, _ := cmd.Flags().GetString("host")
lnk := core.NewLnk(core.WithHost(host))
w := GetWriter(cmd)
restored, err := lnk.Pull()
if err != nil {
@@ -23,31 +26,47 @@ func newPullCmd() *cobra.Command {
}
if len(restored) > 0 {
var successMsg string
if host != "" {
printf(cmd, "⬇️ \033[1;32mSuccessfully pulled changes (host: %s)\033[0m\n", host)
successMsg = fmt.Sprintf("Successfully pulled changes (host: %s)", host)
} else {
printf(cmd, "⬇️ \033[1;32mSuccessfully pulled changes\033[0m\n")
successMsg = "Successfully pulled changes"
}
printf(cmd, " 🔗 Restored \033[1m%d symlink", len(restored))
symlinkText := fmt.Sprintf("Restored %d symlink", len(restored))
if len(restored) > 1 {
printf(cmd, "s")
symlinkText += "s"
}
printf(cmd, "\033[0m:\n")
symlinkText += ":"
w.Writeln(Message{Text: successMsg, Emoji: "⬇️", Color: ColorBrightGreen, Bold: true}).
WriteString(" ").
Writeln(Link(symlinkText))
for _, file := range restored {
printf(cmd, " ✨ \033[36m%s\033[0m\n", file)
w.WriteString(" ").
Writeln(Sparkles(file))
}
printf(cmd, "\n 🎉 Your dotfiles are synced and ready!\n")
w.WritelnString("").
WriteString(" ").
Writeln(Message{Text: "Your dotfiles are synced and ready!", Emoji: "🎉"})
} else {
var successMsg string
if host != "" {
printf(cmd, "⬇️ \033[1;32mSuccessfully pulled changes (host: %s)\033[0m\n", host)
successMsg = fmt.Sprintf("Successfully pulled changes (host: %s)", host)
} else {
printf(cmd, "⬇️ \033[1;32mSuccessfully pulled changes\033[0m\n")
successMsg = "Successfully pulled changes"
}
printf(cmd, " ✅ All symlinks already in place\n")
printf(cmd, " 🎉 Everything is up to date!\n")
w.Writeln(Message{Text: successMsg, Emoji: "⬇️", Color: ColorBrightGreen, Bold: true}).
WriteString(" ").
Writeln(Success("All symlinks already in place")).
WriteString(" ").
Writeln(Message{Text: "Everything is up to date!", Emoji: "🎉"})
}
return nil
return w.Err()
},
}