This commit is contained in:
= 2022-04-02 11:51:58 +01:00
commit 343568afda
2 changed files with 37 additions and 2 deletions

View File

@ -10,8 +10,12 @@
(def bold "") (def bold "")
(def reset "") (def reset "")
(def esc (ch 27))
(def nl (ch 10)) (def nl (ch 10))
(def first (fn (list) (at 0 list)))
(def up "..") (def up "..")
(def ~ (env "HOME")) (def ~ (env "HOME"))
@ -28,10 +32,23 @@
(def clock (fn (ti) (cat (hour ti) ":" (zero ti.minute) ":" (zero ti.sec)))) (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 prompt (fn (a) (
(def ti (time)) (def ti (time))
(def d (cwd))
(cat nl (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) bold green "pebblisp ~> " reset)
))) )))

View File

@ -613,6 +613,15 @@ char* getPrompt(struct Environment* env)
return ret; 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) void repl(struct Environment* env)
{ {
char* buf; char* buf;
@ -623,12 +632,19 @@ void repl(struct Environment* env)
free(buf); free(buf);
break; break;
} }
buf = preprocess(buf, env);
if (buf[0] == '\0') { if (buf[0] == '\0') {
free(buf); free(buf);
continue; continue;
} }
add_history(buf); 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; char* oldBuf = buf;
buf = malloc(sizeof(char) * strlen(buf + 3)); buf = malloc(sizeof(char) * strlen(buf + 3));
sprintf(buf, "(%s)", oldBuf); sprintf(buf, "(%s)", oldBuf);
@ -707,6 +723,8 @@ int main(int argc, const char* argv[])
Object o = parseEval("(def prompt \"pebblisp::> \")", &env); Object o = parseEval("(def prompt \"pebblisp::> \")", &env);
cleanObject(&o); cleanObject(&o);
o = parseEval("(def preprocess (fn (text) (text)))", &env);
cleanObject(&o);
const char* const home = getenv("HOME"); const char* const home = getenv("HOME");
char config[strlen(home) + 15]; char config[strlen(home) + 15];