diff options
author | Rob Landley <rob@landley.net> | 2016-01-17 17:16:03 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2016-01-17 17:16:03 -0600 |
commit | ba86864699997b0da780e15fd6be33c8a556e927 (patch) | |
tree | a8dea4dcbac3ba9c075ef2df54a77f36ddc2d4d9 /lib | |
parent | 3b17f66c10af679f7ce3cd7b17af3b13664cde0c (diff) | |
download | toybox-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')
-rw-r--r-- | lib/lib.c | 11 | ||||
-rw-r--r-- | lib/lib.h | 5 | ||||
-rw-r--r-- | lib/linestack.c | 36 |
3 files changed, 44 insertions, 8 deletions
@@ -988,3 +988,14 @@ void create_uuid(char *uuid) // should never collide with anybody actually using a macaddr. uuid[11] |= 128; } + +char *show_uuid(char *uuid) +{ + char *out = libbuf; + int i; + + for (i=0; i<16; i++) out+=sprintf(out, "-%02x"+!(0x550&(1<<i)), uuid[i]); + *out = 0; + + return libbuf; +} @@ -195,6 +195,7 @@ void base64_init(char *p); int yesno(int def); int qstrcmp(const void *a, const void *b); void create_uuid(char *uuid); +char *show_uuid(char *uuid); #define HR_SPACE 1 // Space between number and units #define HR_B 2 // Use "B" for single byte units @@ -216,7 +217,9 @@ struct linestack *linestack_load(char *name); int crunch_str(char **str, int width, FILE *out, int (*escout)(FILE *out, int cols, char **buf)); int draw_str(char *start, int width); -int draw_rstr(char *start, int width); +int utf8len(char *str); +int utf8skip(char *str, int width); +int draw_trim(char *str, int padto, int width); // interestingtimes.c int xgettty(void); 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; } |