Skip to content

Commit 807c221

Browse files
committed
Add ZSH support
1 parent a3db780 commit 807c221

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

Cargo.lock

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[package]
22
name = "gitprompt-rs"
3-
version = "0.1.2"
3+
version = "0.2.0"
44
authors = ["Dan Elkouby <streetwalkermc@gmail.com>"]
55

66
[dependencies]
7+
"lazy_static" = "1.1.0"

README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,9 @@ A very simple Git prompt written in Rust
66

77
Just add `$(gitprompt-rs)` to your shell prompt.
88

9-
If you're using ZSH, add `setopt promptsubst` to your zshrc, set the `PROMPT`
10-
variable with single quotes `'`, and use the following function to wrap the
11-
output:
12-
```shell
13-
function pr_git() {
14-
# Wrap SGR sequences with %{ and %} to avoid confusing zsh's length calculation
15-
gitprompt-rs | perl -p -e 's/(\[.+?m)/%{\1%}/g'
16-
}
17-
```
18-
This isn't built into the program for compatibility with other shells while
19-
keeping things as simple as possible, and the overhead of calling Perl isn't
20-
that big anyway.
9+
If you're using ZSH, you will want to use `$(gitprompt-rs zsh)` in order to
10+
insert the appropriate escapes in the output, otherwise, it will miscalculate
11+
the length of your prompt and go crazy.
2112

2213
The prompt looks like this: `(master↑4↓7|+2~3-5x6•8)`. The information on
2314
display is as follows:

src/main.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5+
use std::env;
56
use std::error::Error;
67
use std::process;
78

9+
#[macro_use]
10+
extern crate lazy_static;
11+
812
struct GitStatus {
913
branch: Option<String>,
1014
ahead: i64,
@@ -17,20 +21,45 @@ struct GitStatus {
1721
untracked: i64,
1822
}
1923

24+
lazy_static! {
25+
static ref ZSH_ESCAPE: bool = {
26+
match env::args().nth(1) {
27+
Some(arg) => arg == "zsh",
28+
_ => false,
29+
}
30+
};
31+
}
32+
33+
fn zsh_escape_start() {
34+
if *ZSH_ESCAPE {
35+
print!("%{{");
36+
}
37+
}
38+
39+
fn zsh_escape_end() {
40+
if *ZSH_ESCAPE {
41+
print!("%}}");
42+
}
43+
}
44+
2045
fn color(c: i32) {
46+
zsh_escape_start();
2147
if c >= 0 {
2248
print!("\x1b[38;5;{}m", c);
2349
} else {
2450
print!("\x1b[39m");
2551
}
52+
zsh_escape_end();
2653
}
2754

2855
fn bold(b: bool) {
56+
zsh_escape_start();
2957
if b {
3058
print!("\x1b[1m");
3159
} else {
3260
print!("\x1b[22m");
3361
}
62+
zsh_escape_end();
3463
}
3564

3665
fn parse_porcelain2(data: String) -> Option<GitStatus> {

0 commit comments

Comments
 (0)