git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v16 03/14] utf8: add function to align a string into given strbuf

From
Karthik Nayak <karthik.188@gmail.com>
Date
Sep 5, 2015, 18:52 UTC
Message-ID
<1441479135-5285-4-git-send-email-Karthik.188@gmail.com>
In-Reply-To
<1441479135-5285-1-git-send-email-Karthik.188@gmail.com>

Add strbuf_utf8_align() which will align a given string into a strbuf as per given align_type and width. If the width is greater than the string length then no alignment is performed.

Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 utf8.c | 21 +++++++++++++++++++++
 utf8.h | 15 +++++++++++++++
 2 files changed, 36 insertions(+)
diff --git a/utf8.c b/utf8.c
index 28e6d76..00e10c8 100644
--- a/utf8.c
+++ b/utf8.c
@@ -644,3 +644,24 @@ int skip_utf8_bom(char **text, size_t len)
 	*text += strlen(utf8_bom);
 	return 1;
 }
+
+void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
+		       const char *s)
+{
+	int slen = strlen(s);
+	int display_len = utf8_strnwidth(s, slen, 0);
+	int utf8_compensation = slen - display_len;
+
+	if (display_len >= width) {
+		strbuf_addstr(buf, s);
+		return;
+	}
+
+	if (position == ALIGN_LEFT)
+		strbuf_addf(buf, "%-*s", width + utf8_compensation, s);
+	else if (position == ALIGN_MIDDLE) {
+		int left = (width - display_len) / 2;
+		strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s);
+	} else if (position == ALIGN_RIGHT)
+		strbuf_addf(buf, "%*s", width + utf8_compensation, s);
+}
diff --git a/utf8.h b/utf8.h
index 5a9e94b..7930b44 100644
--- a/utf8.h
+++ b/utf8.h
@@ -55,4 +55,19 @@ int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);
  */
 int is_hfs_dotgit(const char *path);
 
+typedef enum {
+	ALIGN_LEFT,
+	ALIGN_MIDDLE,
+	ALIGN_RIGHT
+} align_type;
+
+/*
+ * Align the string given and store it into a strbuf as per the
+ * 'position' and 'width'. If the given string length is larger than
+ * 'width' than then the input string is not truncated and no
+ * alignment is done.
+ */
+void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
+		       const char *s);
+
 #endif
-- 
2.5.1
Previous: Karthik NayakNext: Karthik Nayak
Message 4 of 30 in “port tag.c to use ref-filter APIs”
  1. 00/14 port tag.c to use ref-filter APIsKarthik Nayak, Sep 5, 2015
  2. 01/14 ref-filter: move `struct atom_value` to ref-filter.cKarthik Nayak, Sep 5, 2015
  3. 02/14 ref-filter: introduce ref_formatting_state and ref_formatting_stackKarthik Nayak, Sep 5, 2015
  4. 03/14 utf8: add function to align a string into given strbufKarthik Nayak, Sep 5, 2015
  5. 04/14 ref-filter: introduce handler function for each atomKarthik Nayak, Sep 5, 2015
  6. 05/14 ref-filter: introduce match_atom_name()Karthik Nayak, Sep 5, 2015
  7. Eric SunshineSep 6, 2015
  8. Karthik NayakSep 7, 2015
  9. 06/14 ref-filter: implement an `align` atomKarthik Nayak, Sep 5, 2015
  10. Eric SunshineSep 6, 2015
  11. Karthik NayakSep 7, 2015
  12. 07/14 ref-filter: add option to filter out tags, branches and remotesKarthik Nayak, Sep 5, 2015
  13. 08/14 ref-filter: add support for %(contents:lines=X)Karthik Nayak, Sep 5, 2015
  14. 09/14 ref-filter: add support to sort by versionKarthik Nayak, Sep 5, 2015
  15. 10/14 ref-filter: add option to match literal patternKarthik Nayak, Sep 5, 2015
  16. 11/14 tag.c: use 'ref-filter' data structuresKarthik Nayak, Sep 5, 2015
  17. 12/14 tag.c: use 'ref-filter' APIsKarthik Nayak, Sep 5, 2015
  18. 13/14 tag.c: implement '--format' optionKarthik Nayak, Sep 5, 2015
  19. 14/14 tag.c: implement '--merged' and '--no-merged' optionsKarthik Nayak, Sep 5, 2015
  20. Matthieu MoySep 6, 2015
  21. Karthik NayakSep 6, 2015
  22. Junio C HamanoSep 7, 2015
  23. Matthieu MoySep 7, 2015
  24. Karthik NayakSep 7, 2015
  25. Matthieu MoySep 7, 2015
  26. Karthik NayakSep 8, 2015
  27. Junio C HamanoSep 8, 2015
  28. Karthik NayakSep 9, 2015
  29. Junio C HamanoSep 9, 2015
  30. Junio C HamanoSep 8, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.