aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c11
-rw-r--r--lib/lib.h5
-rw-r--r--lib/linestack.c36
3 files changed, 44 insertions, 8 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 871eda7e..955960ca 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -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;
+}
diff --git a/lib/lib.h b/lib/lib.h
index 2338037c..f1cea22e 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -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;
}