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
docs.json
__dummy.html
docs/
# Code coverage
/prim
prim.so
prim.dylib
prim.dll
prim.a
prim.lib
prim-test-*
*.exe
*.pdb
*.o
*.obj
*.lst
### D Patch ###
# Test Executables
*-test-*
# Comment to allow dub lock file to be version controlled as well
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 ###
*~
@ -107,5 +92,5 @@ $RECYCLE.BIN/
# Windows shortcuts
*.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"
],
"copyright": "Copyright © 2023, zerocool",
"dependencies": {
},
"description": "A minimal D application.",
"license": "proprietary",
"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
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 preexec prompt_preexec

BIN
prim

Binary file not shown.

View File

@ -14,34 +14,48 @@ Opts defaultOpts() {
Opts opts;
opts.pathlen = 3;
opts.pchar = "*";
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) {
Opts opts = defaultOpts();
GetoptResult args = getopt(
argv,
std.getopt.config.bundling,
"ps1|p", "print PS1", &opts.ps1,
"rps1|r", "print RPS1", &opts.rps1,
"preexec|x", "print preexec", &opts.preexec,
try {
std.getopt.config.required,
"col", "terminal width", &opts.col,
GetoptResult args = getopt(
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,
"row", "terminal height", &opts.row,
std.getopt.config.required,
"col", "terminal width", &opts.col,
std.getopt.config.required,
"status", "previous command exit code", &opts.status,
std.getopt.config.required,
"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) {
defaultGetoptPrinter("prim", args.options);
"pathlen", "set length of displayed path", &opts.pathlen,
"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);
@ -53,7 +67,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 ~ "/";
}

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;
int pathlen;
string pchar;
int col;
int row;

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,10 @@ module prompt.ps1;
import std.conv;
import prim.opt;
import comp.hr;
import comp.path;
import comp.ssh;
import style;
import style.color;
@ -12,14 +15,14 @@ import style.font;
string ps1(Opts opt) {
string ps;
// divider
ps ~= hr(opt.col).set(Color.black);
string pathstr = path(opt.pathlen).set(Color.magenta).set(Font.italic).set(Font.bold);
// 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);
ps ~= "\n";
ps ~= (",-(" ~ pathstr ~ ")".set(Color.black)).set(Color.black);
ps ~= " " ~ ssh().set(Color.cyan);
ps ~= "\n";
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;
}

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;
}

View File

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

View File

@ -1,9 +1,22 @@
module style;
import std.conv;
import core.stdc.stdlib : getenv;
import style.color;
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%}") : "");
}