Skip to content

Commit

Permalink
Provide a --bash option similar to --zsh
Browse files Browse the repository at this point in the history
This should address #4.
  • Loading branch information
mrkline committed Mar 11, 2015
1 parent 18ded3a commit 0943f66
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
22 changes: 19 additions & 3 deletions color.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import std.typecons : Flag;

alias UseColor = Flag!"UseColor";

alias ZshEscapes = Flag!"ZshEscapes";
enum Escapes {
none,
bash,
zsh
}

mixin(makeColorFunction("cyan", 36));
mixin(makeColorFunction("green", 32));
Expand All @@ -17,10 +21,17 @@ string makeColorFunction(string name, int code)
import std.conv : to;
return
`
string ` ~ name ~ `(ZshEscapes escapes)
string ` ~ name ~ `(Escapes escapes)
{
string ret = "\33[` ~ code.to!string ~ `m";
return escapes ? zshEscape(ret) : ret;
final switch (escapes) {
case Escapes.none:
return ret;
case Escapes.bash:
return bashEscape(ret);
case Escapes.zsh:
return zshEscape(ret);
}
}
`;
}
Expand All @@ -29,3 +40,8 @@ string zshEscape(string code)
{
return "%{" ~ code ~ "%}";
}

string bashEscape(string code)
{
return `\[` ~ code ~ `\]`;
}
4 changes: 2 additions & 2 deletions git.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct RepoStatus {
* escapes = Whether or not ZSH escapes are needed. Ignored if no colors are desired.
*
*/
string stringRepOfStatus(Duration allottedTime, UseColor colors, ZshEscapes escapes)
string stringRepOfStatus(Duration allottedTime, UseColor colors, Escapes escapes)
{
// getRepoStatus will return null if we are not in a repo
auto status = getRepoStatus(allottedTime);
Expand All @@ -44,7 +44,7 @@ string stringRepOfStatus(Duration allottedTime, UseColor colors, ZshEscapes esca

// Local function that colors a source string if the colors flag is set.
string colorText(string source,
string function(ZshEscapes) colorFunction)
string function(Escapes) colorFunction)
{
if (!colors)
return source;
Expand Down
2 changes: 1 addition & 1 deletion promptd-path.d
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void main(string[] args)
}

string versionString = q"EOS
promptd-path by Matt Kline, version 0.2
promptd-path by Matt Kline, version 0.3
Part of the promptd tool set
EOS";

Expand Down
24 changes: 20 additions & 4 deletions promptd-vcs.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void main(string[] args)
{
uint timeLimit = 500;
bool noColor;
bool zsh;
bool bash, zsh;

try {
getopt(args,
Expand All @@ -22,22 +22,34 @@ void main(string[] args)
"version|v", { writeAndSucceed(versionString); },
"time-limit|t", &timeLimit,
"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;

string vcsInfo = stringRepOfStatus(
timeLimit.msecs,
noColor ? UseColor.no : UseColor.yes,
zsh ? ZshEscapes.yes : ZshEscapes.no);
escapesToUse);

write(vcsInfo);
}

string versionString = q"EOS
promptd-vcs by Matt Kline, version 0.2
promptd-vcs by Matt Kline, version 0.3
Part of the promptd tool set
EOS";

Expand Down Expand Up @@ -67,7 +79,11 @@ Options:
--no-color
Disables colored output, which is on by default
--zsh|z
--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.
Expand Down

0 comments on commit 0943f66

Please sign in to comment.