aboutsummaryrefslogtreecommitdiff
path: root/lib/linestack.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2016-01-17 17:16:03 -0600
committerRob Landley <rob@landley.net>2016-01-17 17:16:03 -0600
commitba86864699997b0da780e15fd6be33c8a556e927 (patch)
treea8dea4dcbac3ba9c075ef2df54a77f36ddc2d4d9 /lib/linestack.c
parent3b17f66c10af679f7ce3cd7b17af3b13664cde0c (diff)
downloadtoybox-ba86864699997b0da780e15fd6be33c8a556e927.tar.gz
Extend utf8 fontmetrics so ps can use them.
Also, I forgot to check in uuid_show() last time.
Diffstat (limited to 'lib/linestack.c')
-rw-r--r--lib/linestack.c36
1 files changed, 29 insertions, 7 deletions
diff --git a/lib/linestack.c b/lib/linestack.c
index 83b0e276..241a437d 100644
--- a/lib/linestack.c
+++ b/lib/linestack.c
@@ -127,13 +127,35 @@ int draw_str(char *start, int width)
return crunch_str(&start, width, stdout, 0);
}
-// Write width chars at end of string to stdout with standard escapes
-int draw_rstr(char *start, int width)
+// Return utf8 columns
+int utf8len(char *str)
{
- char *s = start;
- int len = crunch_str(&s, INT_MAX, 0, 0);
+ return crunch_str(&str, INT_MAX, 0, 0);
+}
+
+// Return bytes used by (up to) this many columns
+int utf8skip(char *str, int width)
+{
+ char *s = str;
+
+ crunch_str(&s, width, 0, 0);
+
+ return s-str;
+}
+
+// Print utf8 to stdout with standard escapes,trimmed to width and padded
+// out to padto. If padto<0 left justify. Returns columns printed
+int draw_trim(char *str, int padto, int width)
+{
+ int apad = abs(padto), len = utf8len(str);
+
+ if (padto<0 && len>width) str += utf8skip(str, len-width);
+ if (len>width) len = width;
+
+ // Left pad if right justified
+ if (padto>0 && apad>len) printf("%*s", apad-len, "");
+ crunch_str(&str, len, stdout, 0);
+ if (padto<0 && apad>len) printf("%*s", apad-len, "");
- s = start;
- if (len > width) crunch_str(&s, len-width, 0, 0);
- return crunch_str(&s, width, stdout, 0);
+ return (apad > len) ? apad : len;
}