From aa71717c4ce38f28259454f0c92a6038b33e2c91 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Sat, 2 Nov 2013 21:21:03 +0100 Subject: [PATCH] Improve printf to handle minimum width --- kernel/src/console.cpp | 21 +++++++++++++++++++++ kernel/src/shell.cpp | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/kernel/src/console.cpp b/kernel/src/console.cpp index a7b31fb2..a0302c9f 100644 --- a/kernel/src/console.cpp +++ b/kernel/src/console.cpp @@ -136,6 +136,14 @@ void k_printf(const char* fmt, ...){ } else { ch = *(fmt++); + std::size_t min_width = 0; + while(ch >= '0' && ch <= '9'){ + min_width = 10 * min_width + (ch - '0'); + ch = *(fmt++); + } + + auto prev = current_column; + if(ch == 'd'){ auto arg = va_arg(va, std::size_t); k_print(arg); @@ -188,6 +196,19 @@ void k_printf(const char* fmt, ...){ auto arg = va_arg(va, const char*); k_print(arg); } + + if(min_width > 0){ + auto width = current_column - prev; + + if(min_width > width){ + min_width -= width; + + while(min_width > 0){ + k_print(' '); + --min_width; + } + } + } } } diff --git a/kernel/src/shell.cpp b/kernel/src/shell.cpp index 7475ec70..1dc4534a 100644 --- a/kernel/src/shell.cpp +++ b/kernel/src/shell.cpp @@ -278,7 +278,7 @@ void disks_command(const char*){ for(std::size_t i = 0; i < number_of_disks(); ++i){ auto& descriptor = drive(i); - k_printf("%h %h %s\n", descriptor.controller, descriptor.drive, descriptor.present ? "Yes" : "No"); + k_printf("%12h %8h %s\n", descriptor.controller, descriptor.drive, descriptor.present ? "Yes" : "No"); } }