Compare commits

...

10 Commits

15 changed files with 186 additions and 94 deletions

45
.gitignore vendored
View File

@ -1,39 +1,24 @@
# Created by https://www.toptal.com/developers/gitignore/api/d,macos,linux,windows
# Edit at https://www.toptal.com/developers/gitignore?templates=d,macos,linux,windows
### D ###
# Compiled Object files
*.o
*.obj
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Compiled Static libraries
*.a
*.lib
# Executables
*.exe
# DUB
.dub .dub
docs.json docs.json
__dummy.html __dummy.html
docs/ docs/
/prim
# Code coverage prim.so
prim.dylib
prim.dll
prim.a
prim.lib
prim-test-*
*.exe
*.pdb
*.o
*.obj
*.lst *.lst
### D Patch ###
# Test Executables
*-test-*
# Comment to allow dub lock file to be version controlled as well
dub.selections.json dub.selections.json
# Created by https://www.toptal.com/developers/gitignore/api/macos,windows,linux
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,windows,linux
### Linux ### ### Linux ###
*~ *~
@ -107,5 +92,5 @@ $RECYCLE.BIN/
# Windows shortcuts # Windows shortcuts
*.lnk *.lnk
# End of https://www.toptal.com/developers/gitignore/api/d,macos,linux,windows # End of https://www.toptal.com/developers/gitignore/api/macos,windows,linux

View File

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

View File

@ -1,13 +1,9 @@
prompt_precmd() {
export PS1=$'`prim --ps1 --col "$COLUMNS" --row "$LINES" --status "$?" --pchar "*"`'
export RPS1=$'`prim --rps1 --col "$COLUMNS" --row "$LINES" --status "$?"`'
}
autoload -Uz add-zsh-hook
setopt promptsubst setopt promptsubst
prompt_precmd() {
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 $?`
}
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

@ -14,34 +14,48 @@ Opts defaultOpts() {
Opts opts; Opts opts;
opts.pathlen = 3; opts.pathlen = 3;
opts.pchar = "*";
return opts; return opts;
} }
void printHelp(GetoptResult args) {
defaultGetoptPrinter("prim prompt:\n", args.options);
writeln("\nEnvironment:\n",
"\tNO_COLOR\tsee https://no-color.org");
}
void main(string[] argv) { void main(string[] argv) {
Opts opts = defaultOpts(); Opts opts = defaultOpts();
GetoptResult args = getopt( try {
argv,
std.getopt.config.bundling,
"ps1|p", "print PS1", &opts.ps1,
"rps1|r", "print RPS1", &opts.rps1,
"preexec|x", "print preexec", &opts.preexec,
std.getopt.config.required, GetoptResult args = getopt(
"col", "terminal width", &opts.col, argv,
std.getopt.config.bundling,
"ps1|p", "print PS1", &opts.ps1,
"rps1|r", "print RPS1", &opts.rps1,
"preexec|x", "print preexec", &opts.preexec,
std.getopt.config.required, std.getopt.config.required,
"row", "terminal height", &opts.row, "col", "terminal width", &opts.col,
std.getopt.config.required, std.getopt.config.required,
"status", "previous command exit code", &opts.status, "row", "terminal height", &opts.row,
"pathlen", "set length of displayed path", &opts.pathlen, std.getopt.config.required,
); "status", "previous command exit code", &opts.status,
if (args.helpWanted) { "pathlen", "set length of displayed path", &opts.pathlen,
defaultGetoptPrinter("prim", args.options); "pchar", "override default prompt character", &opts.pchar,
);
if (args.helpWanted) {
printHelp(args);
}
} catch (Exception e) {
writeln(e.msg);
writeln("try '--help' for more information");
} }
dorun(opts); dorun(opts);
@ -53,7 +67,7 @@ void dorun(Opts opts) {
} }
if (opts.preexec) { if (opts.preexec) {
preexec(opts.col).write(); preexec(opts).write();
} }
if (opts.rps1) { 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; string ps;
foreach (i; 0 .. col) { foreach (i; 0 .. col) {
ps ~= ''; ps ~= '';
} }
return 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 ~ "/";
}

22
source/comp/ssh.d Normal file
View File

@ -0,0 +1,22 @@
module comp.ssh;
import std.process : environment;
import std.socket : Socket;
string ssh() {
string ssh = environment.get("SSH_TTY");
string username = environment.get("USER");
username = username ? username : "";
if (!ssh) {
return "";
}
auto s = new Socket();
scope (exit)
s.close();
string hostname = s.hostName;
return username ~ "@" ~ hostname;
}

View File

@ -6,6 +6,7 @@ struct Opts {
bool preexec; bool preexec;
int pathlen; int pathlen;
string pchar;
int col; int col;
int row; int row;

View File

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

View File

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

View File

@ -1,15 +1,10 @@
module prompt.rps1; 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 prim.opt;
import comp.path;
import comp.git;
import style; import style;
import style.color; import style.color;
import style.font; import style.font;
@ -17,22 +12,8 @@ import style.font;
string rps1(Opts opt) { string rps1(Opts opt) {
string ps; string ps;
string home = expandTilde("~"); ps ~= (gitBranch()).set(Font.bold).set(Color.cyan);
string path = replaceFirst(getcwd(), regex(home), "~"); ps ~= (gitStatus());
string[] splitPath = path.split("/").reverse(); return ps;
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);
} }

View File

@ -38,3 +38,16 @@ enum Bg {
def, def,
reset = 0 reset = 0
} }
enum BrightBg {
black = 100,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
def,
reset = 0
}

View File

@ -1,9 +1,22 @@
module style; module style;
import std.conv; import std.conv;
import core.stdc.stdlib : getenv;
import style.color; import style.color;
string set(string s, int code) { string set(string s, int code) {
return ("%{\x1b[" ~ to!string(code) ~ "m%}") ~ s ~ ("%{\x1b[" ~ to!string((Color.reset + 0)) ~ "m%}"); if (getenv("NO_COLOR"))
return s;
return ("%{\x1b[" ~ to!string(code) ~ "m%}") ~ s ~ (
"%{\x1b[" ~ to!string((Color.reset + 0)) ~ "m%}");
}
string set(string s, int code, bool close) {
if (getenv("NO_COLOR"))
return s;
return ("%{\x1b[" ~ to!string(code) ~ "m%}") ~ s ~ (close ? (
"%{\x1b[" ~ to!string((Color.reset + 0)) ~ "m%}") : "");
} }