Skip to content

Commit

Permalink
Use 'printf' instead 'echo' to print movie information (#242)
Browse files Browse the repository at this point in the history
Modifies the output format to align 'key:value' pairs vertically,
This change improves the readability.
  • Loading branch information
e55am authored May 4, 2024
1 parent 12f16d8 commit 87a2904
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
49 changes: 33 additions & 16 deletions movies/movies
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,47 @@ getMovieInfo()
fi
}

# Print key: value info
printKV() {
key=$1
val=$2
ROW="|%11s: %-40s|\n"
WIDTH=53

# If value too long (greater then 43 char), split it into several line
if [[ ${#val} -le $((WIDTH - 10)) ]]; then
printf "$ROW" "$key" "$val"
else
printf "$ROW" "$key"
printf "%s\n" "$val" | fmt -w 50 \
| while IFS= read -r line; do printf "| %-50s |\n" "$line"; done
fi
}

# Prints the movie information out in a human readable format
printMovieInfo()
{
echo
echo '=================================================='
echo "| Title: $title"
echo "| Year: $year"
echo "| Runtime: $runtime"
if [[ $imdbScore != "" ]]; then echo "| IMDB: $imdbScore"; fi
if [[ $tomatoScore != "" ]]; then echo "| Tomato: $tomatoScore"; fi
echo '+=====================================================+'
printKV "Title" "$title"
printKV "Year" "$year"
printKV "Runtime" "$runtime"
if [[ $imdbScore != "" ]]; then printKV "IMDB" "$imdbScore"; fi
if [[ $tomatoScore != "" ]]; then printKV "Tomato" "$tomatoScore"; fi
if $detail; then
if [[ $metacriticScore != "" ]]; then echo "| Metascore: $metacriticScore"; fi
if [[ $metacriticScore != "" ]]; then printKV "Metascore" "$metacriticScore"; fi
fi
if [[ $rated != "N/A" && $rated != "" ]]; then echo "| Rated: $rated"; fi
echo "| Genre: $genre"
echo "| Director: $director"
echo "| Actors: $actors"
if [[ $plot != "N/A" && $plot != "" ]]; then echo "| Plot: $plot"; fi
if [[ $rated != "N/A" && $rated != "" ]]; then printKV "Rated" "$rated"; fi
printKV "Genre" "$genre"
printKV "Director" "$director"
printKV "Actors" "$actors"
if [[ $plot != "N/A" && $plot != "" ]]; then printKV "Plot" "$plot"; fi
if $detail; then
if [[ $boxOffice != "" ]]; then echo "| Box Office: $boxOffice"; fi
if [[ $production != "" ]]; then echo "| Production: $production"; fi
if [[ $awards != "" ]]; then echo "| Awards: $awards"; fi
if [[ $boxOffice != "" ]]; then printKV "BoxOffice" "$boxOffice"; fi
if [[ $production != "" ]]; then printKV "Production" "$production"; fi
if [[ $awards != "" ]]; then printKV "Awards" "$awards"; fi
fi
echo '=================================================='
echo '+=====================================================+'
echo
}

Expand Down
Binary file modified movies/movies.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/movies.bats
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ fi
@test "Get information on a single movie" {
run movies Argo
[ "$status" -eq 0 ]
[ "${lines[0]}" = "==================================================" ]
[ "${lines[1]}" = "| Title: Argo" ]
[ "${lines[0]}" = "+=====================================================+" ]
[ "${lines[1]}" = "| Title: Argo |" ]
}


Expand Down

0 comments on commit 87a2904

Please sign in to comment.