From e2dbdd14aab47f8f5dc26d07cd650863584ffaaf Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 28 Oct 2018 01:17:48 +0200 Subject: [PATCH] Colorize ls output. --- Userland/ls.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Userland/ls.cpp b/Userland/ls.cpp index 3af8252608a..30983147d93 100644 --- a/Userland/ls.cpp +++ b/Userland/ls.cpp @@ -6,6 +6,8 @@ int main(int c, char** v) { + bool colorize = true; + DIR* dirp = opendir("."); if (!dirp) { perror("opendir failed"); @@ -58,7 +60,26 @@ int main(int c, char** v) printf(" %4u %4u", st.st_uid, st.st_gid); printf(" %10u ", st.st_size); - printf("%s%c", de->d_name, S_ISDIR(st.st_mode) ? '/' : ' '); + + const char* beginColor = ""; + const char* endColor = ""; + + if (colorize) { + if (S_ISDIR(st.st_mode)) + beginColor = "\033[34;1m"; + else if (st.st_mode & 0111) + beginColor = "\033[32;1m"; + else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) + beginColor = "\033[33;1m"; + endColor = "\033[0m"; + } + + printf("%s%s%s", beginColor, de->d_name, endColor); + + if (S_ISDIR(st.st_mode)) + printf("/"); + else if (st.st_mode & 0111) + printf("*"); printf("\n"); } return 0;