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

[PATCH 03/19] ls_colors.c: add a function to color a file name

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Nov 30, 2014, 08:55 UTC
Message-ID
<1417337767-4505-4-git-send-email-pclouds@gmail.com>
In-Reply-To
<1417337767-4505-1-git-send-email-pclouds@gmail.com>

The new function is based on print_color_indicator() from commit 7326d1f1a67edf21947ae98194f98c38b6e9e527 in coreutils.git.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 color.h     |  2 ++
 ls_colors.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
diff --git a/color.h b/color.h
index 3eaa5bd..b6904a3 100644
--- a/color.h
+++ b/color.h
@@ -94,5 +94,7 @@ void color_print_strbuf(FILE *fp, const char *color, const struct strbuf *sb);
 int color_is_nil(const char *color);
 
 void parse_ls_color(void);
+void color_filename(struct strbuf *sb, const char *name,
+		    const char *display_name, mode_t mode, int linkok);
 
 #endif /* COLOR_H */
diff --git a/ls_colors.c b/ls_colors.c
index 3e35ffe..a58da9e 100644
--- a/ls_colors.c
+++ b/ls_colors.c
@@ -422,3 +422,69 @@ void parse_ls_color(void)
 		color_symlink_as_referent = 1;
 	git_config(ls_colors_config, NULL);
 }
+
+void color_filename(struct strbuf *sb, const char *name,
+		    const char *display_name, mode_t mode, int linkok)
+{
+	int type;
+	struct color_ext_type *ext;	/* Color extension */
+
+	if (S_ISREG (mode)) {
+		type = LS_FL;
+		if ((mode & S_ISUID) != 0)
+			type = LS_SU;
+		else if ((mode & S_ISGID) != 0)
+			type = LS_SG;
+		else if ((mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0)
+			type = LS_EX;
+	} else if (S_ISDIR (mode)) {
+		if ((mode & S_ISVTX) && (mode & S_IWOTH))
+			type = LS_TW;
+		else if ((mode & S_IWOTH) != 0)
+			type = LS_OW;
+		else if ((mode & S_ISVTX) != 0)
+			type = LS_ST;
+		else
+			type = LS_DI;
+	} else if (S_ISLNK (mode))
+		type = (!linkok && *ls_colors[LS_OR]) ? LS_OR : LS_LN;
+	else if (S_ISFIFO (mode))
+		type = LS_PI;
+	else if (S_ISSOCK (mode))
+		type = LS_SO;
+	else if (S_ISBLK (mode))
+		type = LS_BD;
+	else if (S_ISCHR (mode))
+		type = LS_CD;
+#ifdef S_ISDOOR
+	else if (S_ISDOOR (mode))
+		type = LS_DO;
+#endif
+	else
+		/* Classify a file of some other type as C_ORPHAN.  */
+		type = LS_OR;
+
+	/* Check the file's suffix only if still classified as C_FILE.  */
+	ext = NULL;
+	if (type == LS_FL) {
+		/* Test if NAME has a recognized suffix.  */
+		size_t len = strlen(name);
+		const char *p = name + len;		/* Pointer to final \0.  */
+		for (ext = color_ext_list; ext != NULL; ext = ext->next) {
+			if (ext->ext.len <= len &&
+			    !strncmp(p - ext->ext.len, ext->ext.string, ext->ext.len))
+				break;
+		}
+	}
+
+	if (display_name)
+		name = display_name;
+	if (ext)
+		strbuf_addf(sb, "\033[%.*sm%s%s",
+			    (int)ext->seq.len, ext->seq.string,
+			    name, GIT_COLOR_RESET);
+	else if (*ls_colors[type])
+		strbuf_addf(sb, "%s%s%s", ls_colors[type], name, GIT_COLOR_RESET);
+	else
+		strbuf_addstr(sb, name);
+}
-- 
2.2.0.60.gb7b3c64
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 4 of 28 in “Add git-list-files”
  1. 00/19 Add git-list-filesNguyễn Thái Ngọc Duy, Nov 30, 2014
  2. 01/19 ls_colors.c: add $LS_COLORS parsing codeNguyễn Thái Ngọc Duy, Nov 30, 2014
  3. 02/19 ls_colors.c: parse color.ls.* from config fileNguyễn Thái Ngọc Duy, Nov 30, 2014
  4. 03/19 ls_colors.c: add a function to color a file nameNguyễn Thái Ngọc Duy, Nov 30, 2014
  5. 04/19 ls_colors.c: highlight submodules like directoriesNguyễn Thái Ngọc Duy, Nov 30, 2014
  6. 05/19 ls-files: buffer full item in strbuf before printingNguyễn Thái Ngọc Duy, Nov 30, 2014
  7. 06/19 ls-files: add --color to highlight file namesNguyễn Thái Ngọc Duy, Nov 30, 2014
  8. 07/19 ls-files: add --columnNguyễn Thái Ngọc Duy, Nov 30, 2014
  9. 08/19 ls-files: support --max-depthNguyễn Thái Ngọc Duy, Nov 30, 2014
  10. 09/19 Add git-list-files, a user friendly version of ls-files and moreNguyễn Thái Ngọc Duy, Nov 30, 2014
  11. Eric SunshineDec 2, 2014
  12. Duy NguyenDec 2, 2014
  13. 10/19 list-files: -u does not imply showing stagesNguyễn Thái Ngọc Duy, Nov 30, 2014
  14. 11/19 list-files: add -R/--recursive short for --max-depth=-1Nguyễn Thái Ngọc Duy, Nov 30, 2014
  15. 12/19 list-files: add -1 short for --no-columnNguyễn Thái Ngọc Duy, Nov 30, 2014
  16. 13/19 list-files: add -t backNguyễn Thái Ngọc Duy, Nov 30, 2014
  17. 14/19 list-files: sort output and remove duplicatesNguyễn Thái Ngọc Duy, Nov 30, 2014
  18. 15/19 list-files: do not show duplicate cached entriesNguyễn Thái Ngọc Duy, Nov 30, 2014
  19. 16/19 list-files: show directories as well as filesNguyễn Thái Ngọc Duy, Nov 30, 2014
  20. 17/19 list-files: add -F/--classifyNguyễn Thái Ngọc Duy, Nov 30, 2014
  21. 18/19 list-files -F: show submodules with the new indicator '&'Nguyễn Thái Ngọc Duy, Nov 30, 2014
  22. 19/19 list-files: -M aka diff-cachedNguyễn Thái Ngọc Duy, Nov 30, 2014
  23. Junio C HamanoDec 1, 2014
  24. Duy NguyenDec 2, 2014
  25. Jeff KingDec 2, 2014
  26. Duy NguyenDec 2, 2014
  27. Jeff KingDec 2, 2014
  28. Michael J GruberDec 2, 2014

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.