@@ -6,11 +6,11 @@ import (
66 "fmt"
77 "io"
88 "sort"
9- "strings"
109
1110 "github.com/walker1211/histprune/internal/cli"
1211 "github.com/walker1211/histprune/internal/history"
1312 "github.com/walker1211/histprune/internal/prune"
13+ "github.com/walker1211/histprune/internal/render"
1414 "github.com/walker1211/histprune/internal/report"
1515 "github.com/walker1211/histprune/internal/storage"
1616)
@@ -61,8 +61,7 @@ func (a App) analyze(ctx context.Context, opts cli.AnalyzeOptions) error {
6161 if opts .JSON {
6262 return writeJSON (a .stdout , summary )
6363 }
64- _ , err = io .WriteString (a .stdout , renderAnalyzeText (summary ))
65- return err
64+ return writeAnalyzeText (a .stdout , summary )
6665 })
6766}
6867
@@ -107,8 +106,7 @@ func (a App) prune(ctx context.Context, opts cli.PruneOptions) error {
107106 _ , err = fmt .Fprintln (a .stdout , text )
108107 return err
109108 }
110- _ , err = io .WriteString (a .stdout , report .Text (summary ))
111- return err
109+ return report .WriteText (a .stdout , summary )
112110 })
113111}
114112
@@ -168,8 +166,7 @@ func (a App) restore(ctx context.Context, opts cli.RestoreOptions) error {
168166 if err != nil {
169167 return err
170168 }
171- _ , err = fmt .Fprintf (a .stdout , "Restored: %s\n Current backup: %s\n " , target , currentBackup )
172- return err
169+ return writeRestoreText (a .stdout , target , currentBackup )
173170 })
174171}
175172
@@ -186,7 +183,8 @@ func (a App) version(ctx context.Context) error {
186183func (a App ) run (fn func () error ) error {
187184 err := fn ()
188185 if err != nil {
189- _ , _ = fmt .Fprintf (a .stderr , "error: %v\n " , err )
186+ content := render .NewContent (a .stderr )
187+ content .Writef ("error: %v\n " , err )
190188 }
191189 return err
192190}
@@ -293,23 +291,30 @@ func formatName(format history.Format) string {
293291 }
294292}
295293
296- func renderAnalyzeText ( summary analyzeSummary ) string {
297- var b strings. Builder
298- fmt . Fprintf ( & b , "Scanned: %d\n " , summary .Scanned )
299- b .WriteString ("Formats:\n " )
294+ func writeAnalyzeText ( w io. Writer , summary analyzeSummary ) error {
295+ content := render . NewContent ( w )
296+ content . Writef ( "Scanned: %d\n " , summary .Scanned )
297+ content .WriteString ("Formats:\n " )
300298 for _ , name := range []string {"zsh_extended" , "plain" , "malformed" } {
301- fmt . Fprintf ( & b , " %s: %d\n " , name , summary .Formats [name ])
299+ content . Writef ( " %s: %d\n " , name , summary .Formats [name ])
302300 }
303- fmt . Fprintf ( & b , "Duplicate commands: %d\n " , summary .DuplicateCommands )
304- b .WriteString ("Top commands:\n " )
301+ content . Writef ( "Duplicate commands: %d\n " , summary .DuplicateCommands )
302+ content .WriteString ("Top commands:\n " )
305303 if len (summary .TopCommands ) == 0 {
306- b .WriteString (" (none)\n " )
307- return b . String ()
304+ content .WriteString (" (none)\n " )
305+ return content . Err ()
308306 }
309307 for _ , top := range summary .TopCommands {
310- fmt . Fprintf ( & b , " %s: %d\n " , top .Command , top .Count )
308+ content . Writef ( " %s: %d\n " , top .Command , top .Count )
311309 }
312- return b .String ()
310+ return content .Err ()
311+ }
312+
313+ func writeRestoreText (w io.Writer , restoredPath , currentBackupPath string ) error {
314+ content := render .NewContent (w )
315+ content .Writef ("Restored: %s\n " , restoredPath )
316+ content .Writef ("Current backup: %s\n " , currentBackupPath )
317+ return content .Err ()
313318}
314319
315320func writeJSON (w io.Writer , payload any ) error {
0 commit comments