Fix rendering issues + add git comp

This commit is contained in:
Shav Kinderlehrer 2023-08-10 10:44:06 -04:00
parent db53ee6c3b
commit 26ba41d08f
10 changed files with 88 additions and 41 deletions

View File

@ -3,7 +3,9 @@
"zerocool"
],
"copyright": "Copyright © 2023, zerocool",
"dependencies": {
},
"description": "A minimal D application.",
"license": "proprietary",
"name": "prim"
}
}

View File

@ -1,13 +1,14 @@
setopt promptsubst
prompt_precmd() {
export PS1=`prim --ps1 --col $COLUMNS --row $LINES --status $?`
export RPS1=`prim --rps1 --col $COLUMNS --row $LINES --status $?`
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 --status $?`
# print -P `prim --preexec --col $COLUMNS --row $LINES --status $?`
}
autoload -Uz add-zsh-hook
setopt promptsubst
add-zsh-hook precmd prompt_precmd
add-zsh-hook preexec prompt_preexec

BIN
prim

Binary file not shown.

View File

@ -53,7 +53,7 @@ void dorun(Opts opts) {
}
if (opts.preexec) {
preexec(opts.col).write();
// preexec(opts).write();
}
if (opts.rps1) {

28
source/comp/git.d Normal file
View File

@ -0,0 +1,28 @@
module comp.git;
import std.stdio;
import std.file : dirEntries, SpanMode;
import std.process;
import std.string : strip;
string gitBranch() {
auto result = execute(["git", "rev-parse", "--abbrev-ref", "HEAD"]);
if (result.status != 0)
return null;
return result.output.strip();
}
string gitStatus() {
auto result = execute(["git", "status", "--porcelain"]);
if (result.status != 0)
return null;
if (result.output.length >= 1) {
return "*";
}
return "";
}

View File

@ -4,7 +4,7 @@ string hr(int col) {
string ps;
foreach (i; 0 .. col) {
ps ~= '';
ps ~= '';
}
return ps;

33
source/comp/path.d Normal file
View File

@ -0,0 +1,33 @@
module comp.path;
import std.conv;
import std.regex;
import std.array;
import std.file : getcwd;
import std.path : expandTilde;
import std.algorithm : reverse;
string path(int pathlen) {
string ps;
string home = expandTilde("~");
string path = replaceFirst(getcwd(), regex(home), "~");
string[] splitPath = path.split("/").reverse();
string[] revSplitPath;
for (int i = 0; i < pathlen; i++) {
if (i >= splitPath.length)
break;
revSplitPath ~= splitPath[i];
}
splitPath = revSplitPath.reverse();
ps ~= splitPath.join("/");
return ps ~ "/";
}

View File

@ -1,10 +1,11 @@
module prompt.preexec;
import prim.opt;
import comp.hr;
import style;
import style.color;
string preexec(int col) {
return hr(col).set(Color.black);
string preexec(Opts opt) {
return "";
}

View File

@ -3,7 +3,9 @@ module prompt.ps1;
import std.conv;
import prim.opt;
import comp.hr;
import comp.path;
import style;
import style.color;
@ -12,14 +14,13 @@ import style.font;
string ps1(Opts opt) {
string ps;
// divider
ps ~= hr(opt.col).set(Color.black);
string pathstr = path(opt.pathlen);
// previous command status
ps ~= ("(" ~ to!string(opt.status) ~ ") ").set(Color.black);
ps ~= "\n";
ps ~= (",-(" ~ pathstr ~ ")").set(Color.black);
ps ~= "\n";
// prompt char
ps ~= ("'-(" ~ to!string(opt.status) ~ ") ").set(Color.black); // prompt char
ps ~= "|> ".set(Font.bold).set(opt.status == 0 ? Color.green : Color.red);
return ps;
}

View File

@ -1,15 +1,10 @@
module prompt.rps1;
import std.conv;
import std.regex;
import std.array;
import std.file : getcwd;
import std.path : expandTilde;
import std.algorithm : reverse;
import prim.opt;
import comp.path;
import comp.git;
import style;
import style.color;
import style.font;
@ -17,22 +12,8 @@ import style.font;
string rps1(Opts opt) {
string ps;
string home = expandTilde("~");
string path = replaceFirst(getcwd(), regex(home), "~");
ps ~= (gitBranch()).set(Font.bold).set(Color.cyan);
ps ~= (gitStatus());
string[] splitPath = path.split("/").reverse();
string[] revSplitPath;
for (int i = 0; i < opt.pathlen; i++) {
if (i >= splitPath.length)
break;
revSplitPath ~= splitPath[i];
}
splitPath = revSplitPath.reverse();
ps ~= splitPath.join("/");
return ps.set(Font.italic).set(Color.yellow);
return ps;
}