mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Add colors to sh(1) and ls(1).
This makes Sortix a happier place to be.
This commit is contained in:
parent
0453ff9532
commit
cd48a35074
2 changed files with 26 additions and 8 deletions
32
utils/ls.cpp
32
utils/ls.cpp
|
@ -42,6 +42,7 @@
|
||||||
bool longformat = false;
|
bool longformat = false;
|
||||||
bool showdotdot = false;
|
bool showdotdot = false;
|
||||||
bool showdotfiles = false;
|
bool showdotfiles = false;
|
||||||
|
bool colors = false;
|
||||||
|
|
||||||
pid_t childpid;
|
pid_t childpid;
|
||||||
|
|
||||||
|
@ -97,17 +98,21 @@ int sort_dirents(const void* a_void, const void* b_void)
|
||||||
return strcmp(a->d_name, b->d_name);
|
return strcmp(a->d_name, b->d_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void getentrycolor(const char** pre, const char** post, mode_t mode)
|
||||||
|
{
|
||||||
|
*pre = "";
|
||||||
|
*post = "";
|
||||||
|
if ( colors && S_ISDIR(mode) )
|
||||||
|
*pre = "\e[36m",
|
||||||
|
*post = "\e[37m";
|
||||||
|
}
|
||||||
|
|
||||||
int handleentry(const char* path, const char* name)
|
int handleentry(const char* path, const char* name)
|
||||||
{
|
{
|
||||||
bool isdotdot = strcmp(name, ".") == 0 || strcmp(name, "..") == 0;
|
bool isdotdot = strcmp(name, ".") == 0 || strcmp(name, "..") == 0;
|
||||||
bool isdotfile = !isdotdot && name[0] == '.';
|
bool isdotfile = !isdotdot && name[0] == '.';
|
||||||
if ( isdotdot && !showdotdot ) { return 0; }
|
if ( isdotdot && !showdotdot ) { return 0; }
|
||||||
if ( isdotfile && !showdotfiles ) { return 0; }
|
if ( isdotfile && !showdotfiles ) { return 0; }
|
||||||
if ( !longformat )
|
|
||||||
{
|
|
||||||
printf("%s\n", name);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
// TODO: Use openat and fstat.
|
// TODO: Use openat and fstat.
|
||||||
char* fullpath = new char[strlen(path) + 1 + strlen(name) + 1];
|
char* fullpath = new char[strlen(path) + 1 + strlen(name) + 1];
|
||||||
strcpy(fullpath, path);
|
strcpy(fullpath, path);
|
||||||
|
@ -118,8 +123,18 @@ int handleentry(const char* path, const char* name)
|
||||||
{
|
{
|
||||||
finishoutput();
|
finishoutput();
|
||||||
error(0, errno, "stat: %s", fullpath);
|
error(0, errno, "stat: %s", fullpath);
|
||||||
|
delete[] fullpath;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
delete[] fullpath;
|
||||||
|
const char* colorpre;
|
||||||
|
const char* colorpost;
|
||||||
|
getentrycolor(&colorpre, &colorpost, st.st_mode);
|
||||||
|
if ( !longformat )
|
||||||
|
{
|
||||||
|
printf("%s%s%s\n", colorpre, name, colorpost);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
char perms[11];
|
char perms[11];
|
||||||
perms[0] = '?';
|
perms[0] = '?';
|
||||||
if ( S_ISREG(st.st_mode) ) { perms[0] = '-'; }
|
if ( S_ISREG(st.st_mode) ) { perms[0] = '-'; }
|
||||||
|
@ -138,8 +153,8 @@ int handleentry(const char* path, const char* name)
|
||||||
if ( 1023 < size ) { size /= 1024UL; sizeunit = "T"; }
|
if ( 1023 < size ) { size /= 1024UL; sizeunit = "T"; }
|
||||||
if ( 1023 < size ) { size /= 1024UL; sizeunit = "P"; }
|
if ( 1023 < size ) { size /= 1024UL; sizeunit = "P"; }
|
||||||
perms[10] = 0;
|
perms[10] = 0;
|
||||||
printf("%s %ju root root %ju%s\t%s\n", perms, (uintmax_t) st.st_nlink,
|
printf("%s %ju root root %ju%s\t%s%s%s\n", perms, (uintmax_t) st.st_nlink,
|
||||||
(uintmax_t) size, sizeunit, name);
|
(uintmax_t) size, sizeunit, colorpre, name, colorpost);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -272,6 +287,9 @@ int main(int argc, char* argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( isatty(1) )
|
||||||
|
colors = true;
|
||||||
|
|
||||||
childpid = 0;
|
childpid = 0;
|
||||||
bool columnable = !longformat;
|
bool columnable = !longformat;
|
||||||
if ( columnable && isatty(1) )
|
if ( columnable && isatty(1) )
|
||||||
|
|
|
@ -273,7 +273,7 @@ void get_and_run_command()
|
||||||
| TERMMODE_ECHO;
|
| TERMMODE_ECHO;
|
||||||
settermmode(0, termmode);
|
settermmode(0, termmode);
|
||||||
|
|
||||||
printf("root@sortix %s # ", getenv_safe("PWD"));
|
printf("\e[32mroot@sortix \e[36m%s #\e[37m ", getenv_safe("PWD"));
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
||||||
const size_t commandsize = 1024;
|
const size_t commandsize = 1024;
|
||||||
|
|
Loading…
Add table
Reference in a new issue