-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathep.c
181 lines (159 loc) · 4.56 KB
/
ep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* É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:
* - 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 <errno.h>
#include <pthread.h>
#include "colors.h"
#include "info_strings.h"
#include "ep.h"
#define PROMPT " λ "
#define JOBS " ✦"
pthread_attr_t *thread_a = 0;
int main(int argc, char **argv)
{
setlocale(LC_ALL, "");
out = stdout;
outerr = stderr;
char *shell_jobs = NULL;
long long command_duration = 0;
int chroot = 0, exit_status = 0;
int c;
while((c = getopt(argc, argv, "cd:e:j:")) != -1) {
switch (c) {
case 'c':
chroot = 1;
break;
case 'd':
command_duration = strtoll(optarg, NULL, 10);
break;
case 'e':
/* won't print anything if optarg can't be parsed into a number */
exit_status = atoi(optarg);
break;
case 'j':
shell_jobs = optarg;
break;
default:
e(ERROR, "invalid command line option", 0);
return 1;
break;
}
}
pthread_attr_t attr;
do {
if (pthread_attr_init(&attr)) break;
/* stack buffer in popen/posix_spawn can get big, so we can't go much below 8KB;
* we subtract 1024 so libc TLS can still fit into a 8KB region instead of
* requiring a 12KB region. This fiddling has no effect if PAGE_SIZE>4KB */
if (pthread_attr_setstacksize(&attr, (1 << 13) - 1024)) {
pthread_attr_destroy(&attr);
break;
}
/* guarantee at least one memory page as guard */
if (pthread_attr_setguardsize(&attr, 1)) {
pthread_attr_destroy(&attr);
break;
}
thread_a = &attr;
} while(0);
/* start threads for long(er) running steps */
struct threaded_task root_lang_task = { .task = task_launch_root_lang };
int git_launched = 1;
pthread_t git_handle;
if (pthread_create(&git_handle, thread_a, git_thread, &root_lang_task)) {
e(INFO, "couldn't create git thread", errno);
git_launched = 0;
}
int pwd_lang_launched = 1;
pthread_t pwd_lang_handle;
if (pthread_create(&pwd_lang_handle, thread_a, lang_thread, NULL)) {
e(INFO, "couldn't create lang thread", errno);
pwd_lang_launched = 0;
}
if (chroot)
p("[chroot] ");
/* XXX: treat allocation failures as variables not existing in environment */
const char *home = getenv("HOME");
char *pwd = getenv("PWD");
#define cond_strdup(s) s = (s ? strdup(s) : s)
cond_strdup(home);
cond_strdup(pwd);
#undef cond_strdup
/* show we are on a different machine */
print_ssh();
use_color(bcyan, print_pwd(home, pwd));
/* git status */
void *git_info = 0;
if (git_launched) {
pthread_join(git_handle, &git_info);
if (git_info)
print_git(git_info);
}
/* programming languages */
uint64_t pwd_langs = 0, root_langs = 0;
void *tmp_mask;
/* if thread returned NULL, assume no language */
#define read_mask() (tmp_mask ? *(uint64_t *)tmp_mask : 0)
#define store_and_discard_mask(dst) do{dst = read_mask(); free(tmp_mask);}while(0)
if (pwd_lang_launched) {
pthread_join(pwd_lang_handle, &tmp_mask);
store_and_discard_mask(pwd_langs);
}
/* safe to check for launched here because we joined git_handle above */
if (root_lang_task.launched) {
pthread_join(root_lang_task.handle, &tmp_mask);
store_and_discard_mask(root_langs);
}
#undef read_mask
#undef store_and_discard_mask
print_lang(pwd_langs | root_langs);
/* we are done with git_info after langs is finished */
free_git(git_info);
/* print currently active shell jobs */
if (shell_jobs) {
int n = atoi(shell_jobs);
if (n >= 1) {
p(JOBS);
if (n > 1) {
/* jobs emoji is wide */
p(" ");
use_color(blue, p(shell_jobs));
}
}
}
/* print previous command's duration and exit status */
if (command_duration >= 5000) {
const int m = 60;
const int seconds_in_hour = m*m;
command_duration /= 1000;
long long hours = command_duration / seconds_in_hour;
long long minutes = command_duration / m - hours * m;
command_duration -= hours * seconds_in_hour + minutes * m;
char dur[256];
if (hours) {
snprintf(dur, sizeof(dur), " %lldh%lldm%llds", hours, minutes, command_duration);
} else if (minutes) {
snprintf(dur, sizeof(dur), " %lldm%llds", minutes, command_duration);
} else {
snprintf(dur, sizeof(dur), " %llds", command_duration);
}
use_color(green, p(dur));
}
/* 127 means command not found, that prints a big enough message already */
if (exit_status && exit_status != 127) {
char ex[256];
snprintf(ex, sizeof(ex), " [%d]", exit_status);
use_color(green, p(ex));
}
p(PROMPT);
}