Skip to content

Commit cee9fa9

Browse files
committed
Refactor listFavouriteEpisode to display a table for better readability
1 parent cee1c71 commit cee9fa9

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

spongebob-cli/main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func favouriteEpisode(episodeNumber int) {
115115
}
116116
}
117117

118-
func listFavouriteEpisode() {
118+
func listFavouriteEpisodes() {
119119
file, err := os.Open("favourites.json")
120120
if err != nil {
121121
if errors.Is(err, os.ErrNotExist) {
@@ -134,10 +134,15 @@ func listFavouriteEpisode() {
134134
return
135135
}
136136

137-
fmt.Println("Your Favourite Episodes")
138-
for key, value := range favouriteEpisodes {
139-
fmt.Printf("Episode %d : %s \n", key, value)
137+
table := tablewriter.NewWriter(os.Stdout)
138+
table.SetHeader([]string{"Episode", "Number"})
139+
table.SetAutoWrapText(false)
140+
141+
for i, title := range favouriteEpisodes {
142+
table.Append([]string{title, fmt.Sprintf("%d", i+1)})
140143
}
144+
145+
table.Render()
141146
}
142147

143148
var favouriteEpisodesJson = make(map[int]string)
@@ -242,7 +247,7 @@ func main() {
242247
listEpisodes(episodesTitles)
243248
}
244249
if *listFavourites {
245-
listFavouriteEpisode()
250+
listFavouriteEpisodes()
246251
return
247252
}
248253
if *addFavouriteEpisode != 0 {

0 commit comments

Comments
 (0)