add colour to ls

master
Drake 3 years ago
parent e1afd2d22d
commit 989bced475
No known key found for this signature in database
GPG Key ID: 9B83455BD94F12A3

@ -0,0 +1,80 @@
/*
* This is free and unencumbered software released into the public domain.
*
* For more information, please refer to <https://unlicense.org>
*/
//https://gist.github.com/RabaDabaDoba/145049536f815903c79944599c6f952a
//Regular text
#define BLK "\e[0;30m"
#define RED "\e[0;31m"
#define GRN "\e[0;32m"
#define YEL "\e[0;33m"
#define BLU "\e[0;34m"
#define MAG "\e[0;35m"
#define CYN "\e[0;36m"
#define WHT "\e[0;37m"
//Regular bold text
#define BBLK "\e[1;30m"
#define BRED "\e[1;31m"
#define BGRN "\e[1;32m"
#define BYEL "\e[1;33m"
#define BBLU "\e[1;34m"
#define BMAG "\e[1;35m"
#define BCYN "\e[1;36m"
#define BWHT "\e[1;37m"
//Regular underline text
#define UBLK "\e[4;30m"
#define URED "\e[4;31m"
#define UGRN "\e[4;32m"
#define UYEL "\e[4;33m"
#define UBLU "\e[4;34m"
#define UMAG "\e[4;35m"
#define UCYN "\e[4;36m"
#define UWHT "\e[4;37m"
//Regular background
#define BLKB "\e[40m"
#define REDB "\e[41m"
#define GRNB "\e[42m"
#define YELB "\e[43m"
#define BLUB "\e[44m"
#define MAGB "\e[45m"
#define CYNB "\e[46m"
#define WHTB "\e[47m"
//High intensty background
#define BLKHB "\e[0;100m"
#define REDHB "\e[0;101m"
#define GRNHB "\e[0;102m"
#define YELHB "\e[0;103m"
#define BLUHB "\e[0;104m"
#define MAGHB "\e[0;105m"
#define CYNHB "\e[0;106m"
#define WHTHB "\e[0;107m"
//High intensty text
#define HBLK "\e[0;90m"
#define HRED "\e[0;91m"
#define HGRN "\e[0;92m"
#define HYEL "\e[0;93m"
#define HBLU "\e[0;94m"
#define HMAG "\e[0;95m"
#define HCYN "\e[0;96m"
#define HWHT "\e[0;97m"
//Bold high intensity text
#define BHBLK "\e[1;90m"
#define BHRED "\e[1;91m"
#define BHGRN "\e[1;92m"
#define BHYEL "\e[1;93m"
#define BHBLU "\e[1;94m"
#define BHMAG "\e[1;95m"
#define BHCYN "\e[1;96m"
#define BHWHT "\e[1;97m"
//Reset
#define reset "\e[0m"

@ -6,6 +6,10 @@
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "ansi-colour.h"
/*
ls - print all files and directories in working directory
@ -26,6 +30,19 @@ int cmp(const void *a, const void *b){
return strcmp(*(const char **)a, *(const char **)b);
}
//https://stackoverflow.com/a/4553076
bool isRegularFile(const char *path)
{
struct stat path_stat;
lstat(path, &path_stat);
return S_ISREG(path_stat.st_mode);
}
int printBool(bool cond) {
printf("%s\n", cond ? "true" : "false");
return 0;
}
//lots o' code ~~stolen~~ borrowed from https://pubs.opengroup.org/onlinepubs/9699919799/functions/readdir.html
int main(int argc, char** argv) {
bool colour = false;
@ -83,10 +100,26 @@ int main(int argc, char** argv) {
do {
char* dirname = dp->d_name;
if (!startsWithChar(dirname, '.') || showdot == true) {
len += 1 + strlen(dirname) + strlen(" ");
out = (char*) realloc(out, len);
strcat(out, dirname);
strcat(out, " ");
if (colour == false) {
len += 1 + strlen(dirname) + strlen(" ");
out = (char*) realloc(out, len);
strcat(out, dirname);
strcat(out, " ");
} else {
if (isRegularFile(dirname) == true) {
len += 1 + strlen(dirname) + strlen(" ");
out = (char*) realloc(out, len);
strcat(out, dirname);
strcat(out, " ");
} else {
len += 1 + strlen(dirname) + strlen(" ") + strlen(BLU) + strlen(reset);
out = (char*) realloc(out, len);
strcat(out, BLU);
strcat(out, dirname);
strcat(out, reset);
strcat(out, " ");
}
}
}
} while ((dp = readdir(dirp)) != NULL);
char *word, *words[strlen(out)/2+1];

Loading…
Cancel
Save