diff --git a/dub.json b/dub.json index 52e2da0..49ed42b 100644 --- a/dub.json +++ b/dub.json @@ -3,7 +3,9 @@ "zerocool" ], "copyright": "Copyright © 2023, zerocool", + "dependencies": { + }, "description": "A minimal D application.", "license": "proprietary", "name": "prim" -} \ No newline at end of file +} diff --git a/hook.zsh b/hook.zsh index 31aa142..9715796 100644 --- a/hook.zsh +++ b/hook.zsh @@ -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 diff --git a/prim b/prim index eec52df..f7d980a 100755 Binary files a/prim and b/prim differ diff --git a/source/app.d b/source/app.d index 60542d6..68bf114 100644 --- a/source/app.d +++ b/source/app.d @@ -53,7 +53,7 @@ void dorun(Opts opts) { } if (opts.preexec) { - preexec(opts.col).write(); + // preexec(opts).write(); } if (opts.rps1) { diff --git a/source/comp/git.d b/source/comp/git.d new file mode 100644 index 0000000..4f68e95 --- /dev/null +++ b/source/comp/git.d @@ -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 ""; +} diff --git a/source/comp/hr.d b/source/comp/hr.d index fa4d32b..76f7098 100644 --- a/source/comp/hr.d +++ b/source/comp/hr.d @@ -4,7 +4,7 @@ string hr(int col) { string ps; foreach (i; 0 .. col) { - ps ~= '—'; + ps ~= '─'; } return ps; diff --git a/source/comp/path.d b/source/comp/path.d new file mode 100644 index 0000000..b8f9914 --- /dev/null +++ b/source/comp/path.d @@ -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 ~ "/"; + +} diff --git a/source/prompt/preexec.d b/source/prompt/preexec.d index 3904a2b..52a5cb2 100644 --- a/source/prompt/preexec.d +++ b/source/prompt/preexec.d @@ -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 ""; } diff --git a/source/prompt/ps1.d b/source/prompt/ps1.d index 5b8bafa..d1bc1b3 100644 --- a/source/prompt/ps1.d +++ b/source/prompt/ps1.d @@ -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; } diff --git a/source/prompt/rps1.d b/source/prompt/rps1.d index 9ec5ac7..075707e 100644 --- a/source/prompt/rps1.d +++ b/source/prompt/rps1.d @@ -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; }