From f58084d9b719f3325080ba64e1de7adfa216d0da Mon Sep 17 00:00:00 2001
From: Alexey Matveichev <github@matveichev.com>
Date: Sun, 13 Nov 2016 19:51:45 +0100
Subject: [PATCH] Messages and alignment

1. Moved wmkdep message to Makefile.
2. Added alignment wmake shell/make functions.
---
 wmake/rules/General/transform | 11 +++++-
 wmake/scripts/wmakeFunctions  | 86 +++++++++++++++++++++++++++++++++++++++++++
 wmake/src/wmkdep.l            |  6 +--
 wmake/wmakeCollect            |  4 +-
 wmake/wmakeLnInclude          |  6 ++-
 5 files changed, 104 insertions(+), 9 deletions(-)

diff --git a/wmake/rules/General/transform b/wmake/rules/General/transform
index 5ac71a6..54e4b8f 100644
--- a/wmake/rules/General/transform
+++ b/wmake/rules/General/transform
@@ -1,9 +1,17 @@
 #----------------------------*- makefile-gmake -*------------------------------
 
+# Quiet rules left column width
+WM_QUIET_LEFT_COLUMNT_DEFAULT_WIDTH = 10
+
+ALIGN_RIGHT = $(shell printf "%*s" $(if $(WM_QUIET_LEFT_COLUMN_WIDTH),$(WM_QUIET_LEFT_COLUMN_WIDTH),$(WM_QUIET_LEFT_COLUMNT_DEFAULT_WIDTH)) $(1))
+
+ALIGN_LEFT = $(shell printf "%-*s" $(if $(WM_QUIET_LEFT_COLUMN_WIDTH),$(WM_QUIET_LEFT_COLUMN_WIDTH),$(WM_QUIET_LEFT_COLUMNT_DEFAULT_WIDTH)) $(1))
+
+
 ifneq ("$(WM_QUIET)","")
     E=@
     define QUIET_MESSAGE
-        @echo "    $1: $2";
+        @echo "$(call ALIGN_RIGHT,$1): $2";
     endef
     QUIET_OP=-s
 else
@@ -23,6 +31,7 @@ $(foreach s,$(SUFFIXES),$(eval $(call DEFINE_TRANSFORM,$(s))))
 
 $(OBJECTS_DIR)/%.dep : %
 	@$(WM_SCRIPTS)/makeTargetDir $@
+	$(call QUIET_MESSAGE,wmkdep,$(<F))
 	@$(WMAKE_BIN)/wmkdep $(QUIET_OP) -I$(*D) $(LIB_HEADER_DIRS) $< | \
 		sed -e 's,^$(WM_PROJECT_DIR)/,$$(WM_PROJECT_DIR)/,' \
 			-e 's,^$(WM_THIRD_PARTY_DIR)/,$$(WM_THIRD_PARTY_DIR)/,' > $@
diff --git a/wmake/scripts/wmakeFunctions b/wmake/scripts/wmakeFunctions
index 0262350..2df9fd0 100755
--- a/wmake/scripts/wmakeFunctions
+++ b/wmake/scripts/wmakeFunctions
@@ -30,6 +30,92 @@
 #------------------------------------------------------------------------------
 
 #------------------------------------------------------------------------------
+# Align given string
+#------------------------------------------------------------------------------
+
+align()
+{
+    # Arguments:
+    # - str: string to be aligned
+    # - dir: direction of alignment (left/right)
+    # - len: length of string where alignment happens
+
+    # If neither len parameter nor WM_QUIET_COLUMN_WIDTH environment variable
+    # are set.
+    local default_column_width=10
+
+    [ "$#" -lt "2" ] && return 1
+    # Default is to align left
+    local fmt="%-*s"
+    local len=
+    local str="$1"
+    shift
+    local dir="$1"
+    case $dir in
+    right)
+        fmt="%*s"
+        ;;
+    *)
+        ;;
+    esac
+    shift
+    if [ "$#" -gt "1" ]
+    then
+        len="$1"
+    elif [ -n "$WM_QUIET_COLUMN_WIDTH" ]
+    then
+        len=$WM_QUIET_COLUMN_WIDTH
+    else
+        len=$default_column_width
+    fi
+    printf $fmt $len $str
+}
+
+
+#------------------------------------------------------------------------------
+# Right-align given string
+#------------------------------------------------------------------------------
+
+alignRight()
+{
+    # Arguments:
+    # - str: string to be aligned
+    # - len: length of string where alignment happens
+
+    [ "$#" -lt "1" ] && return 1
+    local len=
+    local str="$1"
+    shift
+    if [ "$#" -gt "1" ]
+    then
+        len="$1"
+    fi
+    align $str right $len
+}
+
+#------------------------------------------------------------------------------
+# Left-align given string
+#------------------------------------------------------------------------------
+
+alignLeft()
+{
+    # Arguments:
+    # - str: string to be aligned
+    # - len: length of string where alignment happens
+
+    [ "$#" -lt "1" ] && return 1
+    local len=
+    local str="$1"
+    shift
+    if [ "$#" -gt "1" ]
+    then
+        len="$1"
+    fi
+    align $str left $len
+}
+
+
+#------------------------------------------------------------------------------
 # Check environment variables
 #------------------------------------------------------------------------------
 
diff --git a/wmake/src/wmkdep.l b/wmake/src/wmkdep.l
index 81bede8..c89f96a 100644
--- a/wmake/src/wmkdep.l
+++ b/wmake/src/wmkdep.l
@@ -156,11 +156,7 @@ int main(int argc, char* argv[])
 
     sourceFile = strdup(argv[argc-1]);
     silent = (strncmp(argv[1], "-s", 2) == 0);
-    if (silent)
-    {
-        fprintf(stderr, "    wmkdep: %s\n", basename(sourceFile));
-    }
-    else
+    if (!silent)
     {
         fprintf
         (
diff --git a/wmake/wmakeCollect b/wmake/wmakeCollect
index 2aaa2c6..66f761d 100755
--- a/wmake/wmakeCollect
+++ b/wmake/wmakeCollect
@@ -34,6 +34,8 @@
 #-------------------------------------------------------------------------------
 Script=${0##*/}
 
+. ${0%/*}/scripts/wmakeFunctions
+
 usage() {
     exec 1>&2
     while [ "$#" -ge 1 ]; do echo "$1"; shift; done
@@ -138,7 +140,7 @@ then
     # Add the build rule for the current target
     echo "$object: $makefile" >> $file
     [ -z "$E" ] ||
-        echo -e "\t@echo \"    compiling: ${source##*/}\"" >> $file
+        echo -e "\t@echo \"$(alignRight compiling): ${source##*/}\"" >> $file
     echo -e "\t$E cd $PWD && \\" >> $file
     echo -e "\t${@:1:($#-1)} $object" >> $file
     echo >> $file
diff --git a/wmake/wmakeLnInclude b/wmake/wmakeLnInclude
index 3db3802..89862ff 100755
--- a/wmake/wmakeLnInclude
+++ b/wmake/wmakeLnInclude
@@ -40,6 +40,8 @@
 #------------------------------------------------------------------------------
 Script=${0##*/}
 
+. ${0%/*}/scripts/wmakeFunctions
+
 usage() {
     exec 1>&2
     while [ "$#" -ge 1 ]; do echo "$1"; shift; done
@@ -140,7 +142,7 @@ cd $incDir || exit 1
 
 if [ "$silentOpt" = true -o -n "$WM_QUIET" ]
 then
-    echo "    ln: $incDir" 1>&2
+    echo "$(alignRight ln): $incDir" 1>&2
 else
     echo "$Script: linking include files to $incDir" 1>&2
 fi
@@ -172,7 +174,7 @@ find .. $findOpt \
 # Cleanup local variables and functions
 #------------------------------------------------------------------------------
 
-unset Script usage
+unset Script usage WM_SILENT_LEFT_COLUMN_DEFAULT_WIDTH
 
 
 #------------------------------------------------------------------------------
-- 
2.7.4

