aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2026-04-30 09:43:55 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2026-04-30 09:44:22 -0400
commitac35ca7947fd83ba09efa06e0a723a33f8fa4d19 (patch)
tree9d2bc017d9fca40d17b69d0fbe2b0fe0c033bcd0 /src/lib
parent700b1f697771bfb09b93733559381d68abf00169 (diff)
downloadstasis-ac35ca7947fd83ba09efa06e0a723a33f8fa4d19.tar.gz
Move center_text function into utils.c/utils.h
* Remove 'v' prefix * Print version the same way in the indexer
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/core/include/utils.h1
-rw-r--r--src/lib/core/utils.c33
2 files changed, 34 insertions, 0 deletions
diff --git a/src/lib/core/include/utils.h b/src/lib/core/include/utils.h
index 22835ee..3bd0785 100644
--- a/src/lib/core/include/utils.h
+++ b/src/lib/core/include/utils.h
@@ -490,4 +490,5 @@ int get_random_bytes(char *result, size_t maxlen);
*/
int non_format_len(const char *s);
+char *center_text(const char *s, size_t maxwidth);
#endif //STASIS_UTILS_H
diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c
index 4f06d14..f18a2e5 100644
--- a/src/lib/core/utils.c
+++ b/src/lib/core/utils.c
@@ -1239,3 +1239,36 @@ int non_format_len(const char *s) {
return len;
}
+char *center_text(const char *s, const size_t maxwidth) {
+ if (maxwidth < 2) {
+ SYSERROR("%s", "maximum width must be greater than 0");
+ return NULL;
+ }
+
+ if (maxwidth % 2 != 0) {
+ SYSERROR("maximum width (%zu) must be even", maxwidth);
+ return NULL;
+ }
+
+ const size_t s_len = strlen(s);
+ if (s_len + 1 > maxwidth) {
+ SYSERROR("length of input string (%zu) exceeds maximum width (%zu)", s_len, maxwidth);
+ return NULL;
+ }
+
+ char *result = calloc(maxwidth + 1, sizeof(*result));
+ if (!result) {
+ SYSERROR("%s", "unable to allocate bytes for centered text string");
+ return NULL;
+ }
+ const size_t middle = (maxwidth / 2) - s_len / 2;
+ size_t i = 0;
+ for (; i < middle; i++) {
+ result[i] = ' ';
+ }
+ strncpy(&result[i], s, maxwidth - middle - 1);
+ result[maxwidth] = '\0';
+
+ return result;
+}
+