Initial commit.

This commit is contained in:
Érico Rolim 2021-02-22 00:38:13 -03:00
commit e779146066
6 changed files with 111 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
ep
compile_commands.json
.cache/

13
Makefile Normal file
View File

@ -0,0 +1,13 @@
LANGUAGE = PORTUGUESE
CFLAGS = -g -Og -pipe -Ipo/ -D$(LANGUAGE)
# defaults to cproc because "why not?"
CC = cproc
PREFIX = ${HOME}/.local
bindir = $(PREFIX)/bin
all: ep
install: ep
install -m755 $< $(bindir)/ep

86
ep.c Normal file
View File

@ -0,0 +1,86 @@
/* Érico's prompt
*
* I decided I wanted to write my own prompt, so I didn't have to deal with
* configuring existing ones.
*
* Liberties taken:
* - will crash with segfault if allocations fail; NULL is special only when meaningful beyond allocation
* - assumes stdio is reasonably buffered, so multiple fputs calls aren't expensive
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <locale.h>
#include "info_strings.h"
#define PROMPT " ➜ "
/* function to print string */
FILE *out;
static inline void p(const char *s) { fputs(s, out); }
/* print current dir in fish style */
const int fish_style_dir = 1;
int main(int argc, char **argv)
{
setlocale(LC_ALL, "");
out = stdout;
const char *home = getenv("HOME");
const char *hostname = getenv("HOSTNAME");
/* show we are on a different machine */
if (hostname) {
p(hostname);
p(" ");
}
/* deal with printing current path */
char *pwd = getcwd(NULL, 0);
char *rpwd = NULL;
if (!pwd || !(rpwd = realpath(pwd, NULL))) {
/* getcwd or realpath failed */
p(unknowndir);
} else {
/* strip HOME out if possible */
if (home) {
size_t l = strlen(home);
if (!strncmp(home, rpwd, l)) {
/* found HOME in pwd */
p("~");
rpwd += l;
if (*rpwd) {
if (fish_style_dir) {
/* short and sweet way of malloc-ing enough memory */
char *frpwd = strdup(rpwd);
/* rpwd starts with a slash */
const char *c = rpwd, *co;
char *n = frpwd;
for (; c; co = c, c = strchr(co+1, '/')) {
*n++ = '/';
*n++ = *(c+1);
}
/* copy last path completely */
strcpy(--n, co+1);
p(frpwd);
} else {
p(rpwd);
}
}
} else {
/* HOME wasn't in rpwd */
p(rpwd);
}
} else {
/* HOME is unset */
p(rpwd);
}
}
p(PROMPT);
}

7
po/info_strings.h Normal file
View File

@ -0,0 +1,7 @@
#if defined(PORTUGUESE)
# include "strings_pt.h"
#elif defined(ENGLISH)
# include "strings_en.h"
#else
# error "No language selected!"
#endif

1
po/strings_en.h Normal file
View File

@ -0,0 +1 @@
const char *unknowndir = "[unknown]";

1
po/strings_pt.h Normal file
View File

@ -0,0 +1 @@
const char *unknowndir = "[desconhecido]";