f341c8c20646bd82d5d84fae32f68b760e0f1be0

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

feat(changelog): print BREAKING CHANGE messages in a prettier way

Diff

 1diff --git a/tools/changelog.sh b/tools/changelog.sh
 2index b4b8d0c9eb8e674efbbcc7edb4be4d823dd2522e..966e91c25a0cc1ee1eab90f2bae780aec73cb4aa 100755
 3--- a/tools/changelog.sh
 4+++ b/tools/changelog.sh
 5@@ -181,6 +181,12 @@ function display-release {
 6     return
 7   fi
 8 
 9+  # Get length of longest scope for padding
10+  local max_scope=0
11+  for hash in ${(k)scopes}; do
12+    max_scope=$(( max_scope < ${#scopes[$hash]} ? ${#scopes[$hash]} : max_scope ))
13+  done
14+
15   ##* Formatting functions
16 
17   # Format the hash according to output format
18@@ -220,18 +226,13 @@ function display-release {
19     #* Uses $scopes (A) and $hash from outer scope
20     local scope="${1:-${scopes[$hash]}}"
21 
22-    # Get length of longest scope for padding
23-    local max_scope=0 padding=0
24-    for hash in ${(k)scopes}; do
25-      max_scope=$(( max_scope < ${#scopes[$hash]} ? ${#scopes[$hash]} : max_scope ))
26-    done
27-
28     # If no scopes, exit the function
29     if [[ $max_scope -eq 0 ]]; then
30       return
31     fi
32 
33     # Get how much padding is required for this scope
34+    local padding=0
35     padding=$(( max_scope < ${#scope} ? 0 : max_scope - ${#scope} ))
36     padding="${(r:$padding:: :):-}"
37 
38@@ -290,11 +291,15 @@ function display-release {
39     md) fmt:header "BREAKING CHANGES ⚠" 3 ;;
40     esac
41 
42-    local hash subject
43+    local hash message
44+    local wrap_width=$(( (COLUMNS < 100 ? COLUMNS : 100) - 3 ))
45     for hash message in ${(kv)breaking}; do
46-      echo " - $(fmt:hash) $(fmt:scope)$(fmt:subject "${message}")"
47-    done | sort
48-    echo
49+      # Format the BREAKING CHANGE message by word-wrapping it at maximum 100 characters
50+      # (use $COLUMNS if smaller than 100), and adding a 3-space left padding.
51+      message="$(fmt -w $wrap_width <<< "$message" | sed 's/^/   /')"
52+      # Display hash and scope first, and then the full message with newline separators
53+      echo " - $(fmt:hash) $(fmt:scope)\n\n$(fmt:subject "$message")\n"
54+    done
55   }
56 
57   function display:type {