Add NO_COLOR support

This commit is contained in:
Shav Kinderlehrer 2023-08-10 11:50:57 -04:00
parent 3ba25af848
commit 49768a38a7
5 changed files with 16 additions and 4 deletions

View File

@ -1,5 +1,5 @@
prompt_precmd() { prompt_precmd() {
export PS1=$'`prim --ps1 --col $COLUMNS --row $LINES --status $?`' export PS1=$'`prim --ps1 --col $COLUMNS --row $LINES --status $? --pchar `'
export RPS1=$'`prim --rps1 --col $COLUMNS --row $LINES --status $?`' export RPS1=$'`prim --rps1 --col $COLUMNS --row $LINES --status $?`'
} }

View File

@ -14,6 +14,7 @@ Opts defaultOpts() {
Opts opts; Opts opts;
opts.pathlen = 3; opts.pathlen = 3;
opts.pchar = "|>";
return opts; return opts;
} }
@ -38,10 +39,13 @@ void main(string[] argv) {
"status", "previous command exit code", &opts.status, "status", "previous command exit code", &opts.status,
"pathlen", "set length of displayed path", &opts.pathlen, "pathlen", "set length of displayed path", &opts.pathlen,
"pchar", "override default prompt character", &opts.pchar,
); );
if (args.helpWanted) { if (args.helpWanted) {
defaultGetoptPrinter("prim", args.options); defaultGetoptPrinter("prim prompt:\n", args.options);
writeln("\nEnvironment:\n",
"\tNO_COLOR\tsee https://no-color.org");
} }
dorun(opts); dorun(opts);
@ -53,7 +57,7 @@ void dorun(Opts opts) {
} }
if (opts.preexec) { if (opts.preexec) {
// preexec(opts).write(); preexec(opts).write();
} }
if (opts.rps1) { if (opts.rps1) {

View File

@ -6,6 +6,7 @@ struct Opts {
bool preexec; bool preexec;
int pathlen; int pathlen;
string pchar;
int col; int col;
int row; int row;

View File

@ -21,6 +21,6 @@ string ps1(Opts opt) {
ps ~= "\n"; ps ~= "\n";
ps ~= ("'-(" ~ to!string(opt.status) ~ ") ").set(Color.black); // prompt char ps ~= ("'-(" ~ to!string(opt.status) ~ ") ").set(Color.black); // prompt char
ps ~= "|> ".set(Font.bold).set(opt.status == 0 ? Color.green : Color.red); ps ~= (opt.pchar ~ " ").set(Font.bold).set(opt.status == 0 ? Color.green : Color.red);
return ps; return ps;
} }

View File

@ -1,15 +1,22 @@
module style; module style;
import std.conv; import std.conv;
import core.stdc.stdlib : getenv;
import style.color; import style.color;
string set(string s, int code) { string set(string s, int code) {
if (getenv("NO_COLOR"))
return s;
return ("%{\x1b[" ~ to!string(code) ~ "m%}") ~ s ~ ( return ("%{\x1b[" ~ to!string(code) ~ "m%}") ~ s ~ (
"%{\x1b[" ~ to!string((Color.reset + 0)) ~ "m%}"); "%{\x1b[" ~ to!string((Color.reset + 0)) ~ "m%}");
} }
string set(string s, int code, bool close) { string set(string s, int code, bool close) {
if (getenv("NO_COLOR"))
return s;
return ("%{\x1b[" ~ to!string(code) ~ "m%}") ~ s ~ (close ? ( return ("%{\x1b[" ~ to!string(code) ~ "m%}") ~ s ~ (close ? (
"%{\x1b[" ~ to!string((Color.reset + 0)) ~ "m%}") : ""); "%{\x1b[" ~ to!string((Color.reset + 0)) ~ "m%}") : "");
} }