Start add basic prompt

This commit is contained in:
Shav Kinderlehrer 2023-08-09 15:05:26 -04:00
parent dd5c94c317
commit 24c94e510a
7 changed files with 63 additions and 13 deletions

View File

@ -5,4 +5,9 @@ prompt_precmd() {
export RPS1=`prim --rps1 --col $COLUMNS --row $LINES` export RPS1=`prim --rps1 --col $COLUMNS --row $LINES`
} }
prompt_preexec() {
print -P `prim --preexec --col $COLUMNS --row $LINES`
}
add-zsh-hook precmd prompt_precmd add-zsh-hook precmd prompt_precmd
add-zsh-hook preexec prompt_preexec

BIN
prim

Binary file not shown.

View File

@ -2,20 +2,18 @@ import std.stdio;
import std.getopt; import std.getopt;
import prompt.ps1; import prompt.ps1;
import prompt.preexec;
import style.color;
struct Opt { struct Opt {
bool ps1; bool ps1;
bool rps1; bool rps1;
bool preexec;
int col; int col;
int row; int row;
} }
void validate(Opt opts) {
if (!opts.col || !opts.row)
throw new Exception("--col and --row required");
}
void main(string[] argv) { void main(string[] argv) {
Opt opts; Opt opts;
@ -24,7 +22,11 @@ void main(string[] argv) {
std.getopt.config.bundling, std.getopt.config.bundling,
"ps1|p", "print PS1", &opts.ps1, "ps1|p", "print PS1", &opts.ps1,
"rps1|r", "print RPS1", &opts.rps1, "rps1|r", "print RPS1", &opts.rps1,
"preexec|x", "print preexec", &opts.preexec,
std.getopt.config.required,
"col", "terminal width", &opts.col, "col", "terminal width", &opts.col,
std.getopt.config.required,
"row", "terminal height", &opts.row, "row", "terminal height", &opts.row,
); );
@ -32,11 +34,13 @@ void main(string[] argv) {
defaultGetoptPrinter("prim", args.options); defaultGetoptPrinter("prim", args.options);
} }
try { if (opts.ps1) {
validate(opts); ps1(opts.col).setColor(Color.black).write();
} catch (Exception e) {
defaultGetoptPrinter(e.msg ~ "\n", args.options);
} }
ps1(opts.col).write(); if (opts.preexec) {
preexec(opts.col).setColor(Color.black).write();
}
write(setColor("", Color.reset));
} }

11
source/comp/hr.d Normal file
View File

@ -0,0 +1,11 @@
module comp.hr;
string hr(int col) {
string ps;
foreach (i; 0 .. col) {
ps ~= '—';
}
return ps;
}

7
source/prompt/preexec.d Normal file
View File

@ -0,0 +1,7 @@
module prompt.preexec;
import comp.hr;
string preexec(int col) {
return hr(col);
}

View File

@ -1,11 +1,14 @@
module prompt.ps1; module prompt.ps1;
import comp.hr;
import style.color;
string ps1(int col) { string ps1(int col) {
string ps; string ps;
foreach (i; 0 .. col) { ps ~= hr(col);
ps ~= '—';
} ps ~= "> ".setColor(Color.magenta);
return ps; return ps;
} }

20
source/style/color.d Normal file
View File

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