More cd-related sugar.

Add preprocess pl func for repl.
Add basic aliases to repl.
This commit is contained in:
Sage Vaillancourt 2022-04-02 04:57:53 -04:00
parent 1a13fe4814
commit 13fa4ce54f
2 changed files with 37 additions and 2 deletions

View File

@ -10,8 +10,12 @@
(def bold "")
(def reset "")
(def esc (ch 27))
(def nl (ch 10))
(def first (fn (list) (at 0 list)))
(def up "..")
(def ~ (env "HOME"))
@ -28,10 +32,23 @@
(def clock (fn (ti) (cat (hour ti) ":" (zero ti.minute) ":" (zero ti.sec))))
(struct Alias (name value))
(def aliases (
(Alias "ls" "ls --color")
))
(def preprocess (fn (text) (
(def matches (fil (fn (a) (= text a.name)) aliases))
(def match (first matches))
(if (iserr match) text match.value)
)))
(def prompt (fn (a) (
(def ti (time))
(def d (cwd))
(cat nl
bold red "[sage] " blue (clock) " " reset cyan (cwd) nl
esc "]0; " d (ch 7)
bold red "[sage] " blue (clock) " " reset cyan d nl
bold green "pebblisp ~> " reset)
)))

View File

@ -611,6 +611,15 @@ char* getPrompt(struct Environment* env)
return ret;
}
char* preprocess(char* buf, struct Environment* env)
{
Object lambda = fetchFromEnvironment("preprocess", env);
Object buffer = nullTerminated(buf);
Object s = listEvalLambda(&lambda, &buffer, 2, env);
size_t length;
return stringObj(&s, &length);
}
void repl(struct Environment* env)
{
char* buf;
@ -621,12 +630,19 @@ void repl(struct Environment* env)
free(buf);
break;
}
buf = preprocess(buf, env);
if (buf[0] == '\0') {
free(buf);
continue;
}
add_history(buf);
if ((buf[0] == 'c' && buf[1] == 'd') || (buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0'))) {
if ((buf[0] == 'c' && buf[1] == 'd')) {
char* oldBuf = buf;
buf = malloc(sizeof(char) * strlen(buf + 6));
sprintf(buf, "(cd \"%s\")", oldBuf + 3);
free(oldBuf);
}
if ((buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0'))) {
char* oldBuf = buf;
buf = malloc(sizeof(char) * strlen(buf + 3));
sprintf(buf, "(%s)", oldBuf);
@ -705,6 +721,8 @@ int main(int argc, const char* argv[])
Object o = parseEval("(def prompt \"pebblisp::> \")", &env);
cleanObject(&o);
o = parseEval("(def preprocess (fn (text) (text)))", &env);
cleanObject(&o);
const char* const home = getenv("HOME");
char config[strlen(home) + 15];