[PATCH 5/8] ls-files: add --color to highlight based on $LS_COLORS

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx>
---
 builtin/ls-files.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index 47c3880..463280e 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -14,6 +14,8 @@
 #include "resolve-undo.h"
 #include "string-list.h"
 #include "pathspec.h"
+#include "color.h"
+#include "ls_colors.h"
 
 static int abbrev;
 static int show_deleted;
@@ -27,6 +29,7 @@ static int show_killed;
 static int show_valid_bit;
 static int line_terminator = '\n';
 static int debug_mode;
+static int use_color;
 
 static const char *prefix;
 static int max_prefix_len;
@@ -57,6 +60,33 @@ static void write_name(const char *name)
 				   stdout, line_terminator);
 }
 
+static void write_dir_entry(const struct dir_entry *ent)
+{
+	if (want_color(use_color)) {
+		static struct strbuf sb = STRBUF_INIT;
+		struct stat st;
+		int statok;
+		quote_path_relative(ent->name, prefix_len ? prefix : NULL, &sb);
+		statok = stat(ent->name, &st) == 0;
+		print_color_indicator(sb.buf, st.st_mode, 0, statok, 0);
+		fputs(sb.buf, stdout);
+		printf("%s%c", GIT_COLOR_RESET, line_terminator);
+	} else
+		write_name(ent->name);
+}
+
+static void write_ce_name(const struct cache_entry *ce)
+{
+	if (want_color(use_color)) {
+		static struct strbuf sb = STRBUF_INIT;
+		quote_path_relative(ce->name, prefix_len ? prefix : NULL, &sb);
+		print_color_indicator(sb.buf, ce->ce_mode, 1, 1, 0);
+		fputs(sb.buf, stdout);
+		printf("%s%c", GIT_COLOR_RESET, line_terminator);
+	} else
+		write_name(ce->name);
+}
+
 static void show_dir_entry(const char *tag, struct dir_entry *ent)
 {
 	int len = max_prefix_len;
@@ -68,7 +98,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
 		return;
 
 	fputs(tag, stdout);
-	write_name(ent->name);
+	write_dir_entry(ent);
 }
 
 static void show_other_files(struct dir_struct *dir)
@@ -170,7 +200,7 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)
 		       find_unique_abbrev(ce->sha1,abbrev),
 		       ce_stage(ce));
 	}
-	write_name(ce->name);
+	write_ce_name(ce);
 	if (debug_mode) {
 		const struct stat_data *sd = &ce->ce_stat_data;
 
@@ -501,6 +531,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 			N_("if any <file> is not in the index, treat this as an error")),
 		OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
 			N_("pretend that paths removed since <tree-ish> are still present")),
+		OPT__COLOR(&use_color, N_("show color")),
 		OPT__ABBREV(&abbrev),
 		OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
 		OPT_END()
@@ -548,6 +579,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 	if (require_work_tree && !is_inside_work_tree())
 		setup_work_tree();
 
+	if (want_color(use_color))
+		parse_ls_color();
+
 	parse_pathspec(&pathspec, 0,
 		       PATHSPEC_PREFER_CWD |
 		       PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
-- 
1.9.0.40.gaa8c3ea

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]