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
195
196
197
198
199
200
201
202
203
//! Glitter
//!
//! # Usage
//!
//! Basic usage for `glit` is:
//!
//! ```
//! $ glit <FORMAT>
//! ```
//!
//! Learn more and get help with:
//!
//! ```
//! $ glit help
//! ```
//!
//! ## Setting your shell to use `glit`
//!
//! Too add a glitter format to your shell prompt if you are in a bash shell, add the
//! following snippet to your `~/.bashrc`:
//!
//! ```bash
//! # Use environment variables to store formats if you want to be able to easily
//! # change them from your shell by just doing:
//! #
//! # $ export PS1_FMT="#r;*('TODO')"
//!
//! # Format to use inside of git repositories or their sub-folders
//! export PS1_FMT="\<#m;*(\b)#m(\B(#~('..')))\(#g(\+)#r(\-))>\[#g;*(\M\A\R\D)#r;*(\m\a\u\d)]\{#m;*;_(\h('@'))}':'#y;*('\w')'\n\$ '"
//!
//! # Format to use outside of git repositories
//! export PS1_ELSE_FMT="#g(#*('\u')'@\h')':'#b;*('\w')'\$ '"
//!
//! # Prompt command which is used to set the prompt, includes some extra useful
//! # functionality such as showing the last exit code
//! __set_prompt() {
//! local EXIT="$?"
//! # Capture last command exit flag first
//!
//! # Clear out prompt
//! PS1=""
//!
//! # If the last command didn't exit 0, display the exit code
//! [ "$EXIT" -ne "0" ] && PS1+="$EXIT "
//!
//! # identify debian chroot, if one exists
//! if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
//! PS1+="${debian_chroot:+($(cat /etc/debian_chroot))}"
//! fi
//!
//! # Render the appropriate format depending on whether we are in a git repo
//! PS1+="$(glit "$PS1_FMT" -e "$PS1_ELSE_FMT")"
//! }
//!
//! export PROMPT_COMMAND=__set_prompt
//! ```
//!
//! Where the variable **PS1_FMT** contains your glitter format. Here are a few
//! examples you might want to try out on your system.
//!
//! | Example `fmt` | Result |
//! |:-----------------------------------------------------------------------------------------------------------|:------------------------------------------------------|
//! | `"\<#m;*(\b)#m(\B(#~('..')))\(#g(\+)#r(\-))>\[#g;*(\M\A\R\D)#r;*(\m\a\u\d)]\{#m;*;_(\h('@'))}"` |  |
//! | `"\(#m;*(\b)#g(\+)#r(\-))\[#g(\M\A\R\D)#r(\m\a\u\d)]\{#m;_(\h('@'))}':'"` |  |
//! | `"#g;*(\b)#y(\B(#~('..')))\[#g(\+(#~('ahead ')))]\[#r(\-(#~('behind ')))]' '#g;_(\M\A\R\D)#r;_(\m\a\u\d)"` |  |
//!
//! # Background
//!
//! Most shells provide the ability to customize the shell prompt which appears before every command.
//! On my system, the default looks like:
//!
//! ```
//! gwen@tpy12:~/Documents/dev/util/glitter$
//! ```
//!
//! Its intended to provide useful information about your shell. However, it normally does not
//! include information about git repositories, requiring the near constant use of `git status`
//! to understand the state of the repository. The solution is to set a prompt command and
//! dynamically update your shell with the information you want. `glit` is made for precisely
//! this purpose: you can provide a format, and glitter will interpret it, inserting the information
//! in the format you want.
extern crate structopt;
extern crate human_panic;
// Currently, only used to enable windows colors
extern crate yansi;
use Repository;
use ;
use PathBuf;
use StructOpt;
use ;
/// Glitter is a git repository status pretty-printing utility intended
/// for making custom shell prompts which incorporate information about
/// the current git repository, such as the branch name, number of
/// unstaged changes, and more.