diff --git a/hook.zsh b/hook.zsh index 999b689..31aa142 100644 --- a/hook.zsh +++ b/hook.zsh @@ -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 diff --git a/prim b/prim index 9fbb91d..9d75862 100755 Binary files a/prim and b/prim differ diff --git a/source/app.d b/source/app.d index ca934fa..d1b18b9 100644 --- a/source/app.d +++ b/source/app.d @@ -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(); + } } diff --git a/source/opt.d b/source/opt.d new file mode 100644 index 0000000..1236ac9 --- /dev/null +++ b/source/opt.d @@ -0,0 +1,12 @@ +module prim.opt; + +struct Opts { + bool ps1; + bool rps1; + bool preexec; + + int col; + int row; + + int status; +} diff --git a/source/prompt/preexec.d b/source/prompt/preexec.d index eb2d365..3904a2b 100644 --- a/source/prompt/preexec.d +++ b/source/prompt/preexec.d @@ -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); } diff --git a/source/prompt/ps1.d b/source/prompt/ps1.d index 9bf8021..5b8bafa 100644 --- a/source/prompt/ps1.d +++ b/source/prompt/ps1.d @@ -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; } diff --git a/source/prompt/rps1.d b/source/prompt/rps1.d new file mode 100644 index 0000000..5ee5d2c --- /dev/null +++ b/source/prompt/rps1.d @@ -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; +} + diff --git a/source/style/color.d b/source/style/color.d index 3d788b8..8576e56 100644 --- a/source/style/color.d +++ b/source/style/color.d @@ -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 } diff --git a/source/style/font.d b/source/style/font.d new file mode 100644 index 0000000..ae605b9 --- /dev/null +++ b/source/style/font.d @@ -0,0 +1,12 @@ +module style.font; + +enum Font { + bold = 1, + dim, + italic, + underline, + blinking, + hidden, + strikethrough, + reset = 0 +} diff --git a/source/style/package.d b/source/style/package.d new file mode 100644 index 0000000..76aa044 --- /dev/null +++ b/source/style/package.d @@ -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%}"); +}