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() {
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 $?`'
}

View File

@ -14,6 +14,7 @@ Opts defaultOpts() {
Opts opts;
opts.pathlen = 3;
opts.pchar = "|>";
return opts;
}
@ -38,10 +39,13 @@ void main(string[] argv) {
"status", "previous command exit code", &opts.status,
"pathlen", "set length of displayed path", &opts.pathlen,
"pchar", "override default prompt character", &opts.pchar,
);
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);
@ -53,7 +57,7 @@ void dorun(Opts opts) {
}
if (opts.preexec) {
// preexec(opts).write();
preexec(opts).write();
}
if (opts.rps1) {

View File

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

View File

@ -21,6 +21,6 @@ string ps1(Opts opt) {
ps ~= "\n";
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;
}

View File

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