aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElias Fleckenstein <eliasfleckenstein@web.de>2021-06-07 16:06:45 +0200
committerElias Fleckenstein <eliasfleckenstein@web.de>2021-06-07 16:06:45 +0200
commitd8da3f4fb0b3d5e7ea6f4c65b16b81166c34ca19 (patch)
tree3c8f3c51b867db98e0acb4ef7e91f8aa2d3e4d71
parentf3451cbbdd70e0dda15b01a1920f486169c14d0f (diff)
downloadttfe-d8da3f4fb0b3d5e7ea6f4c65b16b81166c34ca19.tar.xz
Center numbers
-rw-r--r--main.c18
-rw-r--r--main.h1
2 files changed, 18 insertions, 1 deletions
diff --git a/main.c b/main.c
index d209b14..f2b4642 100644
--- a/main.c
+++ b/main.c
@@ -399,6 +399,22 @@ void merge_west(board *b) {
}
}
+void center_print(uint n, int width)
+{
+ char s[20] = {'\0'};
+ int len;
+ sprintf(s, "%u", n);
+ len = strlen(s);
+ if (len >= width) {
+ printf("%s", s);
+ } else {
+ int remaining = width - len;
+ int spaces_right = remaining / 2;
+ int spaces_left = remaining - spaces_right;
+ printf("%*s%s%*s", spaces_left, "", s, spaces_right, "");
+ }
+}
+
void print_sep(const char *left, const char *right, const char *cross, const char *line)
{
printf("%s", left);
@@ -422,7 +438,7 @@ void print_board_line(board *b, int l) {
if(n == 0)
printf(" ");
else
- printf("%6u", n);
+ center_print(n, 6);
if(i == 3)
printf("\u2503");
diff --git a/main.h b/main.h
index 9068990..24020d6 100644
--- a/main.h
+++ b/main.h
@@ -8,6 +8,7 @@
#include <time.h>
#include <unistd.h>
#include <termios.h>
+#include <string.h>
typedef struct board board;
typedef unsigned int uint;