Add path parsing

This commit is contained in:
Shav Kinderlehrer 2023-08-09 16:33:14 -04:00
parent 85f830ce47
commit db53ee6c3b
5 changed files with 37 additions and 6 deletions

BIN
prim

Binary file not shown.

View File

@ -10,9 +10,17 @@ import prompt.preexec;
import style; import style;
import style.color; import style.color;
void main(string[] argv) { Opts defaultOpts() {
Opts opts; Opts opts;
opts.pathlen = 3;
return opts;
}
void main(string[] argv) {
Opts opts = defaultOpts();
GetoptResult args = getopt( GetoptResult args = getopt(
argv, argv,
std.getopt.config.bundling, std.getopt.config.bundling,
@ -28,6 +36,8 @@ void main(string[] argv) {
std.getopt.config.required, std.getopt.config.required,
"status", "previous command exit code", &opts.status, "status", "previous command exit code", &opts.status,
"pathlen", "set length of displayed path", &opts.pathlen,
); );
if (args.helpWanted) { if (args.helpWanted) {

View File

@ -5,6 +5,8 @@ struct Opts {
bool rps1; bool rps1;
bool preexec; bool preexec;
int pathlen;
int col; int col;
int row; int row;

View File

@ -1,6 +1,12 @@
module prompt.rps1; module prompt.rps1;
import std.conv; 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;
@ -11,9 +17,22 @@ import style.font;
string rps1(Opts opt) { string rps1(Opts opt) {
string ps; string ps;
// previous command status string home = expandTilde("~");
ps ~= ("(" ~ to!string(opt.status) ~ ") ").set(Color.black); string path = replaceFirst(getcwd(), regex(home), "~");
return ps; 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);
}

View File

@ -5,5 +5,5 @@ import std.conv;
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[0m%}"); return ("%{\x1b[" ~ to!string(code) ~ "m%}") ~ s ~ ("%{\x1b[" ~ to!string((Color.reset + 0)) ~ "m%}");
} }