forked from mrkline/promptoglyph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromptoglyph-vcs.d
194 lines (157 loc) · 5.5 KB
/
promptoglyph-vcs.d
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
182
183
184
185
186
187
188
189
190
191
192
193
194
module promptoglyph.vcs;
import std.getopt;
import std.datetime : msecs;
import std.stdio : write;
import color;
import git;
import help;
import vcs;
struct StatusStringOptions {
string prefix = "[";
string suffix = "]";
string indexedText = "✔";
string modifiedText = "±";
string untrackedText = "?";
}
void main(string[] args)
{
uint timeLimit = 500;
bool noColor;
bool bash, zsh;
StatusStringOptions stringOptions;
try {
getopt(args,
config.caseSensitive,
config.bundling,
"help|h", { writeAndSucceed(helpString); },
"version|v", { writeAndSucceed(versionString); },
"time-limit|t", &timeLimit,
"prefix|p", &stringOptions.prefix,
"indexed-text|i", &stringOptions.indexedText,
"modified-text|m", &stringOptions.modifiedText,
"untracked-text|u", &stringOptions.untrackedText,
"suffix|s", &stringOptions.suffix,
"no-color", &noColor,
"bash|b", &bash,
"zsh|z", &zsh);
}
catch (GetOptException ex) {
writeAndFail(ex.msg, "\n", helpString);
}
if (bash && zsh)
writeAndFail("Both --bash and --zsh specified. Wat.");
Escapes escapesToUse;
if (bash)
escapesToUse = Escapes.bash;
else if (zsh)
escapesToUse = Escapes.zsh;
else // Redundant (none is the default), but more explicit.
escapesToUse = Escapes.none;
const Duration allottedTime = timeLimit.msecs;
const RepoStatus* status = getRepoStatus(allottedTime);
string statusString = stringRepOfStatus(
status, stringOptions,
noColor ? UseColor.no : UseColor.yes,
escapesToUse,
allottedTime);
write(statusString);
}
/**
* Gets a string representation of the status of the Git repo
*
* Params:
* allottedTime = The amount of time given to gather Git info.
* Git status will be killed if it does not complete in this much time.
* Since this is for a shell prompt, responsiveness is important.
* colors = Whether or not colored output is desired
* escapes = Whether or not ZSH escapes are needed. Ignored if no colors are desired.
*
*/
string stringRepOfStatus(const RepoStatus* status, const ref StatusStringOptions stringOptions,
UseColor colors, Escapes escapes, Duration allottedTime)
{
import time;
if (status is null)
return "";
// Local function that colors a source string if the colors flag is set.
string colorText(string source,
string function(Escapes) colorFunction)
{
if (!colors)
return source;
else
return colorFunction(escapes) ~ source;
}
string head;
if (!status.head.empty)
head = colorText(status.head, &cyan);
string flags = " ";
if (status.flags.indexed)
flags ~= colorText(stringOptions.indexedText, &green);
if (status.flags.modified)
flags ~= colorText(stringOptions.modifiedText, &yellow); // Yellow plus/minus
if (status.flags.untracked)
flags ~= colorText(stringOptions.untrackedText, &red); // Red quesiton mark
// We don't want an extra space if there's nothing to show.
if (flags == " ")
flags = "";
string ret = head ~ flags ~
colorText(stringOptions.suffix, &resetColor);
if (pastTime(allottedTime))
ret = "T " ~ ret;
return stringOptions.prefix ~ ret;
}
string versionString = q"EOS
promptoglyph-vcs by Matt Kline, version 0.5
Part of the promptoglyph tool set
EOS";
string helpString = q"EOS
usage: promptoglyph-vcs [-t <milliseconds>]
Options:
--help, -h
Display this help text
--version, -v
Display the version info
--time-limit, -t <milliseconds>
The maximum amount of time the program can run before exiting,
in milliseconds. Defaults to 500 milliseconds.
Running "git status" can take a long time for big or complex
repositories, but since this program is for a prompt,
we can't delay an arbitrary amount of time without annoying the user.
If it takes longer than this amount of time to get the repo status,
we prematurely kill "git status" and display whatever information
was received so far. The hope is that in subsequent runs, "git status" will
complete in time since your operating system caches recently-accessed
files and directories.
--no-color
Disables colored output, which is on by default
--prefix, -p <string>
Text to prepend to the VCS information (if in a VCS directory)
--untracked-text, -u <string>
Text to display when the VCS indicates untracked files
(if in a VCS directory)
--modified-text, -m <string>
Text to display when the VCS indicates files modified since the last commit
(if in a VCS directory)
--indexed-text, -i <string>
Text to display when the VSC indicates files ready to commit
(if in a VCS directory)
--suffix, -s <string>
Text to append to the VCS information (if in a VCS directory)
--bash, -b
Used to emit additional escapes needed for color sequences in Bash prompts.
Ignored if --no-color is specified.
--zsh, -z
Used to emit additional escapes needed for color sequences in ZSH prompts.
Ignored if --no-color is specified.
promptoglyph-vcs is designed to be part of a shell prompt.
It prints a quick, symbolic look at the status of a Git repository
if you are currently in one and nothing otherwise. Output looks like
[master ✔±?]
where "master" is the current branch, ? indicates untracked files,
± indicates changed but unstaged files, and ✔ indicates files staged
in the index. If "git status" could not run in a timely manner to get this info
(see --time-limit above), a T is placed in front.
Future plans include additional info (like when merging),
and possibly Subversion and Mercurial support.
EOS";