Add styles

This commit is contained in:
Shav Kinderlehrer 2023-08-09 16:01:15 -04:00
parent 24c94e510a
commit 85f830ce47
10 changed files with 118 additions and 26 deletions

View File

@ -1,12 +1,12 @@
setopt promptsubst
prompt_precmd() {
export PS1=`prim --ps1 --col $COLUMNS --row $LINES`
export RPS1=`prim --rps1 --col $COLUMNS --row $LINES`
export PS1=`prim --ps1 --col $COLUMNS --row $LINES --status $?`
export RPS1=`prim --rps1 --col $COLUMNS --row $LINES --status $?`
}
prompt_preexec() {
print -P `prim --preexec --col $COLUMNS --row $LINES`
print -P `prim --preexec --col $COLUMNS --row $LINES --status $?`
}
add-zsh-hook precmd prompt_precmd

BIN
prim

Binary file not shown.

View File

@ -1,21 +1,17 @@
import std.stdio;
import std.getopt;
import prim.opt;
import prompt.ps1;
import prompt.rps1;
import prompt.preexec;
import style;
import style.color;
struct Opt {
bool ps1;
bool rps1;
bool preexec;
int col;
int row;
}
void main(string[] argv) {
Opt opts;
Opts opts;
GetoptResult args = getopt(
argv,
@ -26,21 +22,31 @@ void main(string[] argv) {
std.getopt.config.required,
"col", "terminal width", &opts.col,
std.getopt.config.required,
"row", "terminal height", &opts.row,
std.getopt.config.required,
"status", "previous command exit code", &opts.status,
);
if (args.helpWanted) {
defaultGetoptPrinter("prim", args.options);
}
dorun(opts);
}
void dorun(Opts opts) {
if (opts.ps1) {
ps1(opts.col).setColor(Color.black).write();
ps1(opts).write();
}
if (opts.preexec) {
preexec(opts.col).setColor(Color.black).write();
preexec(opts.col).write();
}
write(setColor("", Color.reset));
if (opts.rps1) {
rps1(opts).write();
}
}

12
source/opt.d Normal file
View File

@ -0,0 +1,12 @@
module prim.opt;
struct Opts {
bool ps1;
bool rps1;
bool preexec;
int col;
int row;
int status;
}

View File

@ -2,6 +2,9 @@ module prompt.preexec;
import comp.hr;
import style;
import style.color;
string preexec(int col) {
return hr(col);
return hr(col).set(Color.black);
}

View File

@ -1,14 +1,25 @@
module prompt.ps1;
import comp.hr;
import style.color;
import std.conv;
string ps1(int col) {
import prim.opt;
import comp.hr;
import style;
import style.color;
import style.font;
string ps1(Opts opt) {
string ps;
ps ~= hr(col);
// divider
ps ~= hr(opt.col).set(Color.black);
ps ~= "> ".setColor(Color.magenta);
// previous command status
ps ~= ("(" ~ to!string(opt.status) ~ ") ").set(Color.black);
// prompt char
ps ~= "|> ".set(Font.bold).set(opt.status == 0 ? Color.green : Color.red);
return ps;
}

19
source/prompt/rps1.d Normal file
View File

@ -0,0 +1,19 @@
module prompt.rps1;
import std.conv;
import prim.opt;
import style;
import style.color;
import style.font;
string rps1(Opts opt) {
string ps;
// previous command status
ps ~= ("(" ~ to!string(opt.status) ~ ") ").set(Color.black);
return ps;
}

View File

@ -1,7 +1,5 @@
module style.color;
import std.conv;
enum Color {
black = 30,
red,
@ -15,6 +13,28 @@ enum Color {
reset = 0
}
string setColor(string s, int code) {
return "%{\x1b[" ~ to!string(code) ~ "m%}" ~ s;
enum Bright {
black = 90,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
def,
reset = 0
}
enum Bg {
black = 40,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
def,
reset = 0
}

12
source/style/font.d Normal file
View File

@ -0,0 +1,12 @@
module style.font;
enum Font {
bold = 1,
dim,
italic,
underline,
blinking,
hidden,
strikethrough,
reset = 0
}

9
source/style/package.d Normal file
View File

@ -0,0 +1,9 @@
module style;
import std.conv;
import style.color;
string set(string s, int code) {
return ("%{\x1b[" ~ to!string(code) ~ "m%}") ~ s ~ ("%{\x1b[0m%}");
}