-
Notifications
You must be signed in to change notification settings - Fork 11
/
eshell-git-prompt.el
713 lines (612 loc) · 27.2 KB
/
eshell-git-prompt.el
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
;;; eshell-git-prompt.el --- Some Eshell prompt for Git users -*- lexical-binding: t; -*-
;; Copyright (C) 2015-2017 Chunyang Xu
;; Author: Chunyang Xu <[email protected]>
;; URL: https://github.com/xuchunyang/eshell-git-prompt
;; Package-Requires: ((emacs "24.1") (cl-lib "0.5") (dash "2.11.0"))
;; Keywords: eshell git
;; Version: 0.1.3
;; Created: 09/11/2015
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides some themes of Emacs Shell (Eshell) prompt.
;;
;; Usage:
;; In Eshell, type ~use-theme~ to list and preview available themes, then
;; type ~use-theme name~ to choose a theme.
;;
;; You can also choose a theme in your init file by using
;; ~eshell-git-prompt-use-theme~, then Eshell will use theme at the
;; startup. For example, put the following in you init file
;;
;; TODO
;; - [ ] For `eshell-prompt-regexp' hack, replace '$' with '' ('\x06')
;; - [ ] Make it easier to make new theme (that is, improve API)
;; - [ ] Test with recent version Emacs and in text-base Emacs
;;
;; Note
;; 1 You must kill all Eshell buffers and re-enter Eshell to make your new
;; prompt take effect.
;; 2 You must set `eshell-prompt-regexp' to FULLY match your Eshell prompt,
;; sometimes it is impossible, especially when you don't want use any special
;; characters (e.g., '$') to state the end of your Eshell prompt
;; 3 If you set `eshell-prompt-function' or `eshell-prompt-regexp' incorrectly,
;; Eshell may crashes, if it happens, you can M-x `eshell-git-prompt-use-theme'
;; to revert to the default Eshell prompt, then read Note #1.
;;; Code:
(require 'cl-lib)
(require 'dash)
(declare-function eshell/pwd "em-dirs" (&rest args))
(declare-function eshell-printn "esh-io" (object))
(eval-when-compile
(defvar eshell-last-command-status)
(defvar eshell-prompt-function)
(defvar eshell-prompt-regexp))
;;; * Customization
(defcustom eshell-git-prompt-themes
'((robbyrussell
eshell-git-prompt-robbyrussell
eshell-git-prompt-robbyrussell-regexp)
(git-radar
eshell-git-prompt-git-radar
eshell-git-prompt-git-radar-regexp)
(powerline
eshell-git-prompt-powerline
eshell-git-prompt-powerline-regexp)
(multiline
eshell-git-prompt-multiline
eshell-git-prompt-multiline-regexp)
(multiline2
eshell-git-prompt-multiline2
eshell-git-prompt-multiline2-regexp)
;; Only a single $
(simple
eshell-git-prompt-simple
eshell-git-prompt-simple-regexp)
(default
eshell-git-prompt-default-func
eshell-git-prompt-default-regexp))
"A list of all available themes.
You can add your own theme to this list, then run
`eshell-git-prompt-use-theme' to use it."
:group 'eshell-prompt
:type '(repeat (list symbol symbol symbol)))
(defface eshell-git-prompt-exit-success-face
'((((class color) (background light)) :foreground "forest green")
(((class color) (background dark)) :foreground "green"))
"Face for the success char"
:group 'eshell-faces)
(defface eshell-git-prompt-exit-fail-face
'((((class color) (background light)) :foreground "red")
(((class color) (background dark)) :foreground "red"))
"Face for the fail char"
:group 'eshell-faces)
(defface eshell-git-prompt-directory-face
'((((class color) (background light)) :foreground "steel blue")
(((class color) (background dark)) :foreground "cyan"))
"Face for the directory"
:group 'eshell-faces)
(defface eshell-git-prompt-modified-face
'((((class color) (background light)) :foreground "dark orange")
(((class color) (background dark)) :foreground "red"))
"Face for the git 'modified' char"
:group 'eshell-faces)
(defface eshell-git-prompt-add-face
'((((class color) (background light)) :foreground "dim gray")
(((class color) (background dark)) :foreground "white"))
"Face for the git 'add' char"
:group 'eshell-faces)
(defface eshell-git-prompt-branch-face
'((((class color) (background light)) :foreground "dim gray")
(((class color) (background dark)) :foreground "dark gray"))
"Face for the git branch"
:group 'eshell-faces)
(defface eshell-git-prompt-robyrussell-git-face
'((((class color) (background light)) :foreground "blue")
(((class color) (background dark)) :foreground "blue"))
"Face for the git indicator"
:group 'eshell-faces)
(defface eshell-git-prompt-robyrussell-branch-face
'((((class color) (background light)) :foreground "red")
(((class color) (background dark)) :foreground "red"))
"Face for the git branch"
:group 'eshell-faces)
(defface eshell-git-prompt-robyrussell-git-dirty-face
'((((class color) (background light)) :foreground "dark orange")
(((class color) (background dark)) :foreground "yellow"))
"Face for the git dirty"
:group 'eshell-faces)
(defface eshell-git-prompt-powerline-dir-face
'((t :background "steel blue"))
"Face for directory name in eshell git prompt theme `powerline`"
:group 'eshell-faces)
(defface eshell-git-prompt-powerline-clean-face
'((t :background "forest green"))
"Face for git branch (clean) in eshell git prompt theme `powerline`"
:group 'eshell-faces)
(defface eshell-git-prompt-powerline-not-clean-face
'((t :background "indian red"))
"Face for git branch (not clean) in eshell git prompt theme `powerline`"
:group 'eshell-faces)
(defface eshell-git-prompt-multiline-secondary-face
'((((class color) (background light)) :foreground "light gray")
(((class color) (background dark)) :foreground "dim gray"))
"Face for secondary part in eshell git prompt theme `multiline`. e.g. separator, horizontal line, date."
:group 'eshell-faces)
(defface eshell-git-prompt-multiline-command-face
'((((class color) (background light)) :foreground "slate blue")
(((class color) (background dark)) :foreground "gold"))
"Face for command user typed in eshell git prompt theme `multiline`."
:group 'eshell-faces)
(defface eshell-git-prompt-multiline-sign-face
'((t :foreground "deep pink"))
"Face for prompt sign in eshell git prompt theme `multiline`."
:group 'eshell-faces)
(defface eshell-git-prompt-multiline2-secondary-face
'((t (:foreground "#51afef" :weight ultra-bold)))
"Face for secondary part in eshell git prompt theme `multiline`. e.g. separator, horizontal line, date."
:group 'eshell-faces)
(defface eshell-git-prompt-multiline2-usr-face
'((t (:foreground "magenta" :weight ultra-bold)))
"Face for prompt sign in eshell git prompt theme `multiline`."
:group 'eshell-faces)
(defface eshell-git-prompt-multiline2-dir-face
'((t (:foreground "white" :weight ultra-bold)))
"Face for prompt sign in eshell git prompt theme `multiline`."
:group 'eshell-faces)
(defface eshell-git-prompt-multiline2-host-face
'((t (:foreground "green" :weight ultra-bold)))
"Face for prompt sign in eshell git prompt theme `multiline`."
:group 'eshell-faces)
(defface eshell-git-prompt-multiline2-git-face
'((t (:foreground "red" :weight ultra-bold)))
"Face for prompt sign in eshell git prompt theme `multiline`."
:group 'eshell-faces)
(defface eshell-git-prompt-multiline2-fail-face
'((t (:foreground "red" :weight ultra-bold)))
"Face for prompt sign in eshell git prompt theme `multiline`."
:group 'eshell-faces)
(defface eshell-git-prompt-multiline2-command-face
'((((class color) (background light)) (:foreground "slate blue" :weight ultra-bold))
(((class color) (background dark)) (:foreground "gold" :weight ultra-bold)))
"Face for command user typed in eshell git prompt theme `multiline`."
:group 'eshell-faces)
;;; * Internal
(defmacro with-face (str &rest properties)
"Add face PROPERTIES to STR."
(declare (indent 1))
`(propertize ,str 'face (list ,@properties)))
(defun eshell-git-prompt---str-read-only (str)
"Make STR read-only and return the string."
(add-text-properties 0 (length str)
'(read-only t
front-sticky (read-only)
rear-nonsticky (read-only))
str)
str)
(defmacro eshell-git-prompt-str-append (str suffix)
"Append STR with SUFFIX and set the value of symbol STR to the result."
(if (symbolp str)
(list 'setq str (list 'concat str suffix))
(list 'cl-callf 'concat str suffix)))
(cl-defun eshell-git-prompt--git-root-dir
(&optional (directory default-directory))
"Return Git root directory name if exist, otherwise, return nil."
(let ((root (locate-dominating-file directory ".git")))
(and root (file-name-as-directory root))))
(cl-defun eshell-git-prompt--shorten-directory-name
(&optional (directory default-directory))
"Return only current directory name (without ending slash).
DIRECTORY must end with slash.
For example:
\"~/foo/bar\" => \"bar\"
\"~\" => \"~\"
\"/\" => \"/\""
(let ((dir (abbreviate-file-name directory)))
(if (> (length dir) 1)
(file-name-nondirectory (substring dir 0 -1))
dir)))
(defun eshell-git-prompt--slash-str (str)
"Make sure STR is ended with one slash, return it."
(if (string-suffix-p "/" str)
str
(concat str "/")))
(defun eshell-git-prompt-last-command-status ()
"Return Eshell last command execution status.
When Eshell just launches, `eshell-last-command-status' is not defined yet,
return 0 (i.e., success)."
(if (not (boundp 'eshell-last-command-status))
0
eshell-last-command-status))
(defun eshell-git-prompt-exit-success-p ()
"Replacement of `eshell-exit-success-p'.
Should return nil only if a external command fails, otherwise return non-nil.
It is unable to distinguish if a elisp command fails in practice, for example,
`eshell/cd' return nil whether it successes or not."
(= (eshell-git-prompt-last-command-status) 0))
(defconst eshell-git-prompt---git-global-arguments
'("--no-pager" "--literal-pathspecs" "-c" "core.preloadindex=true")
"Global git arguments.")
(defun eshell-git-prompt--process-git-arguments (args)
"Prepare ARGS for a function that invokes Git."
(setq args (-flatten args))
(append eshell-git-prompt---git-global-arguments args))
(defun eshell-git-prompt--git-insert (&rest args)
"Execute Git with ARGS, inserting its output at point."
(setq args (eshell-git-prompt--process-git-arguments args))
(apply #'process-file "git" nil (list t nil) nil args))
(defun eshell-git-prompt--git-string (&rest args)
"Execute Git with ARGS, returning the first line of its output.
If there is no output return nil. If the output begins with a
newline return an empty string."
(with-temp-buffer
(apply #'eshell-git-prompt--git-insert args)
(unless (bobp)
(goto-char (point-min))
(buffer-substring-no-properties (point) (line-end-position)))))
(defun eshell-git-prompt--git-lines (&rest args)
"Execute Git with ARGS, returning its output as a list of lines.
Empty lines anywhere in the output are omitted."
(with-temp-buffer
(apply #'eshell-git-prompt--git-insert args)
(split-string (buffer-string) "\n" t)))
(defun eshell-git-prompt--collect-status ()
"Return working directory status as a plist.
If working directory is clean, return nil."
(let ((untracked 0) ; next git-add, then git-commit
(modified 0) ; next git-commit
(modified-updated 0) ; next git-commit
(new-added 0) ; next git-commit
(deleted 0) ; next git-rm, then git-commit
(deleted-updated 0) ; next git-commit
(renamed-updated 0) ; next git-commit
)
(-when-let (status-items (eshell-git-prompt--git-lines "status" "--porcelain"))
(--each status-items
(pcase (substring it 0 2)
("??" (cl-incf untracked))
("MM" (progn (cl-incf modified)
(cl-incf modified-updated)))
(" M" (cl-incf modified))
("M " (cl-incf modified-updated))
("A " (cl-incf new-added))
(" D" (cl-incf deleted))
("D " (cl-incf deleted-updated))
("R " (cl-incf renamed-updated))))
(list :untracked untracked
:modified modified
:modified-updated modified-updated
:new-added new-added
:deleted deleted
:deleted-updated deleted-updated
:renamed-updated renamed-updated))))
(defun eshell-git-prompt--branch-name ()
"Return current branch name."
(eshell-git-prompt--git-string "symbolic-ref" "HEAD" "--short"))
(defvar eshell-git-prompt-branch-name nil)
(defun eshell-git-prompt--commit-short-sha ()
(eshell-git-prompt--git-string "rev-parse" "--short" "HEAD"))
(defun eshell-git-prompt--readable-branch-name ()
(-if-let (branch-name eshell-git-prompt-branch-name)
branch-name
(concat "detached@" (eshell-git-prompt--commit-short-sha))))
(defun eshell-git-prompt--remote-branch-name ()
(eshell-git-prompt--git-string "for-each-ref" "--format=%(upstream:short)"
(format "refs/heads/%s"
eshell-git-prompt-branch-name)))
(defvar eshell-git-prompt-remote-branch-name nil)
(defun eshell-git-prompt--commits-ahead-of-remote ()
(-if-let (remote-branch-name eshell-git-prompt-remote-branch-name)
(string-to-number
(eshell-git-prompt--git-string "rev-list" "--right-only" "--count"
(format "%s...HEAD" remote-branch-name)))
0))
(defun eshell-git-prompt--commits-behind-of-remote ()
(-if-let (remote-branch-name eshell-git-prompt-remote-branch-name)
(string-to-number
(eshell-git-prompt--git-string "rev-list" "--left-only" "--count"
(format "%s...HEAD" remote-branch-name)))
0))
;;; * Themes
(defun eshell-git-prompt-simple ()
(eshell-git-prompt---str-read-only
(if (= (user-uid) 0) "# " "$ ")))
(defconst eshell-git-prompt-simple-regexp "^[#$] ")
;; the Eshell default prompt
(defun eshell-git-prompt-default-func ()
(eshell-git-prompt---str-read-only
(concat (abbreviate-file-name (eshell/pwd))
(if (= (user-uid) 0) " # " " $ "))))
(defconst eshell-git-prompt-default-regexp "^[^#$\n]* [#$] ")
;; oh-my-zsh's robbyrussell theme
;; see https://github.com/robbyrussell/oh-my-zsh/wiki/Themes#robbyrussell
(defun eshell-git-prompt-robbyrussell ()
"Eshell Git prompt with oh-my-zsh's robbyrussell theme.
It looks like:
➜ eshell-git-prompt git:(master) ✗ git status "
;; Prompt components
(let (beg dir git-branch git-dirty end)
;; Beg: start symbol
(setq beg
(with-face "➜" (if (eshell-git-prompt-exit-success-p)
'eshell-git-prompt-exit-success-face 'eshell-git-prompt-exit-fail-face)))
;; Dir: current working directory
(setq dir (with-face (eshell-git-prompt--shorten-directory-name)
'eshell-git-prompt-directory-face))
;; Git: branch/detached head, dirty status
(when (eshell-git-prompt--git-root-dir)
(setq eshell-git-prompt-branch-name (eshell-git-prompt--branch-name))
(setq git-branch
(concat
(with-face "git:(" 'eshell-git-prompt-robyrussell-git-face)
(with-face (eshell-git-prompt--readable-branch-name) 'eshell-git-prompt-robyrussell-branch-face)
(with-face ")" 'eshell-git-prompt-robyrussell-git-face)))
(setq git-dirty
(when (eshell-git-prompt--collect-status)
(with-face "✗" 'eshell-git-prompt-robyrussell-git-dirty-face))))
;; End: To make it possible to let `eshell-prompt-regexp' to match the full prompt
(setq end (propertize "$" 'invisible t))
;; Build prompt
(eshell-git-prompt---str-read-only
(concat (mapconcat #'identity (-non-nil (list beg dir git-branch git-dirty)) " ")
end
" "))))
(defconst eshell-git-prompt-robbyrussell-regexp "^[^$\n]*\\\$ ")
;; git-radar
;; see https://github.com/michaeldfallen/git-radar
(defun eshell-git-prompt-git-radar ()
"Eshell Git prompt inspired by git-radar."
(eshell-git-prompt---str-read-only
(concat
(with-face "➜"
(if (eshell-git-prompt-exit-success-p)
'eshell-git-prompt-exit-success-face 'eshell-git-prompt-exit-fail-face))
" "
(with-face (eshell-git-prompt--shorten-directory-name)
'eshell-git-prompt-directory-face)
;; Yo, we are in a Git repo, display some information about it
(when (eshell-git-prompt--git-root-dir)
(setq eshell-git-prompt-branch-name
(eshell-git-prompt--branch-name)
eshell-git-prompt-remote-branch-name
(eshell-git-prompt--remote-branch-name))
(concat
" "
(with-face "git:(" 'eshell-git-prompt-branch-face)
;; Branch name
(with-face (eshell-git-prompt--readable-branch-name)
'eshell-git-prompt-branch-face)
;; Local commits
(when eshell-git-prompt-remote-branch-name
(let ((local-behind (eshell-git-prompt--commits-behind-of-remote))
(local-ahead (eshell-git-prompt--commits-ahead-of-remote)))
(cond ((and (> local-ahead 0) (> local-behind 0))
(concat " "
(number-to-string local-behind)
(with-face "⇵" :foreground "yellow")
(number-to-string local-ahead)))
((> local-behind 0)
(concat " "
(number-to-string local-behind)
(with-face "↓" :foreground "red")))
((> local-ahead 0)
(concat " "
(number-to-string local-ahead)
(with-face "↑" :foreground "LimeGreen"))))))
(with-face ")" 'eshell-git-prompt-branch-face)
;; File status
(-when-let (git-status (eshell-git-prompt--collect-status))
(-let [(&plist :untracked untracked
:new-added new-added
:modified-updated modified-updated
:modified modified)
git-status]
(concat
;; Updated to index changes
(let (group1)
(setq group1
(concat
(when (> new-added 0)
(concat
(number-to-string new-added)
(with-face "A" 'eshell-git-prompt-add-face)))
(when (> modified-updated 0)
(concat
(number-to-string modified-updated)
(with-face "M" 'eshell-git-prompt-modified-face)))))
(when (> (length group1) 0)
(concat " " group1)))
;; Modified but not updated
(when (> modified 0)
(concat " " (number-to-string modified)
(with-face "M" 'eshell-git-prompt-modified-face)))
;; Untracked file
(when (> untracked 0)
(concat " " (number-to-string untracked)
(with-face "A" 'eshell-git-prompt-add-face))))))))
;; To make it possible to let `eshell-prompt-regexp' to match the full prompt
(propertize "$" 'invisible t) " ")))
(defconst eshell-git-prompt-git-radar-regexp "^[^$\n]*\\\$ ")
;; Powerline
;; For https://github.com/xuchunyang/eshell-git-prompt/issues/8
(defun eshell-git-prompt-powerline-dir ()
(abbreviate-file-name (eshell/pwd)))
(defun eshell-git-prompt-powerline ()
(let ((segment-separator "\xe0b0")
(branch "\xe0a0")
(detached "\x27a6")
(cross "\x2718")
dir git git-face)
(setq dir
(propertize
(concat
" "
(unless (eshell-git-prompt-exit-success-p)
(concat cross " "))
(eshell-git-prompt-powerline-dir)
" ")
'face 'eshell-git-prompt-powerline-dir-face))
(setq git
(when (eshell-git-prompt--git-root-dir)
(setq git-face
(if (eshell-git-prompt--collect-status)
'eshell-git-prompt-powerline-not-clean-face
'eshell-git-prompt-powerline-clean-face))
(setq eshell-git-prompt-branch-name (eshell-git-prompt--branch-name))
(propertize
(concat " "
(-if-let (branch-name eshell-git-prompt-branch-name)
(concat branch " " branch-name)
(concat detached " "(eshell-git-prompt--commit-short-sha)))
" ")
'face git-face)))
(eshell-git-prompt---str-read-only
(concat
(if git
(concat dir
(with-face segment-separator
:foreground (face-background 'eshell-git-prompt-powerline-dir-face)
:background (face-background git-face))
git
(with-face segment-separator
:foreground (face-background git-face)))
(concat dir
(with-face segment-separator
:foreground (face-background 'eshell-git-prompt-powerline-dir-face))))
(propertize "$" 'invisible t) " "))))
(defconst eshell-git-prompt-powerline-regexp "^[^$\n]*\\\$ ")
(defun eshell-git-prompt-multiline ()
"Eshell Git prompt inspired by spaceship-prompt."
(let (separator hr dir git git-dirty time sign command)
(setq separator (with-face " | " 'eshell-git-prompt-multiline-secondary-face))
(setq hr (with-face (concat "\n" (make-string (/ (window-total-width) 2) ?─) "\n") 'eshell-git-prompt-multiline-secondary-face))
(setq dir
(concat
(with-face "🗀" 'eshell-git-prompt-directory-face)
(concat (abbreviate-file-name (eshell/pwd)))))
(setq git
(concat (with-face "⎇" 'eshell-git-prompt-exit-success-face)
(concat (eshell-git-prompt--branch-name))))
(setq git-dirty
(when (eshell-git-prompt--branch-name)
(if (eshell-git-prompt--collect-status)
(with-face " ✎" 'eshell-git-prompt-modified-face)
(with-face " ✔" 'eshell-git-prompt-exit-success-face))))
(setq time (with-face (format-time-string "%I:%M:%S %p") 'eshell-git-prompt-multiline-secondary-face))
(setq sign
(if (= (user-uid) 0)
(with-face "\n#" 'eshell-git-prompt-multiline-sign-face)
(with-face "\nλ" 'eshell-git-prompt-multiline-sign-face)))
(setq command (with-face " " 'eshell-git-prompt-multiline-command-face))
;; Build prompt
(eshell-git-prompt---str-read-only
(concat hr dir separator git git-dirty separator time sign command))))
(defconst eshell-git-prompt-multiline-regexp "^[^$\n]*λ ")
(defun eshell-git-prompt-multiline2 ()
"Eshell Git prompt inspired by spaceship-prompt."
(let (beg separator usr at host dir git time sign command)
(setq beg (with-face "\n┌─(" 'eshell-git-prompt-multiline2-secondary-face))
(setq separator (with-face ")──(" 'eshell-git-prompt-multiline2-secondary-face))
(setq usr (with-face (user-login-name) ''eshell-git-prompt-multiline2-usr-face))
(setq at (with-face "@" 'eshell-git-prompt-multiline2-secondary-face))
(setq host (with-face (system-name) 'eshell-git-prompt-multiline2-host-face))
(setq dir
(with-face (eshell-git-prompt--shorten-directory-name)
'eshell-git-prompt-multiline2-dir-face))
(setq git
(concat
(if (eshell-git-prompt--branch-name)
(concat
separator
(with-face "⎇ " 'eshell-git-prompt-exit-success-face)
(with-face (eshell-git-prompt--branch-name)
'eshell-git-prompt-multiline2-git-face))
"")
(when (eshell-git-prompt--branch-name)
(if (eshell-git-prompt--collect-status)
(with-face " ✗" 'eshell-git-prompt-modified-face)
(with-face " ✔" 'eshell-git-prompt-exit-success-face)))))
(setq time (with-face (format-time-string "%I:%M:%S %p")
'eshell-git-prompt-multiline2-command-face))
(setq sign
(concat
(with-face ")\n└─" 'eshell-git-prompt-multiline2-secondary-face)
(if (not (eshell-git-prompt-exit-success-p))
(with-face ">>" 'eshell-git-prompt-multiline2-fail-face)
(with-face ">>" 'eshell-git-prompt-multiline2-secondary-face))))
(setq command (with-face " " 'eshell-git-prompt-multiline2-command-face))
;; Build prompt
(eshell-git-prompt---str-read-only
(concat beg usr at host separator dir git separator time sign command))))
(defconst eshell-git-prompt-multiline2-regexp "^[^$\n]*└─>> ")
(defvar eshell-git-prompt-current-theme nil)
;;;###autoload
(defun eshell-git-prompt-use-theme (theme)
"Pick up a Eshell prompt theme from `eshell-git-prompt-themes' to use."
(interactive
(let ((theme
(completing-read "Use theme: "
(--map (symbol-name (car it))
eshell-git-prompt-themes)
nil t)))
(list (intern theme))))
(when (stringp theme)
(setq theme (intern theme)))
(-if-let (func-regexp (assoc-default theme eshell-git-prompt-themes))
(progn
(setq eshell-prompt-function (symbol-function (car func-regexp))
eshell-prompt-regexp (symbol-value (cadr func-regexp)))
(setq eshell-git-prompt-current-theme theme)
(when (called-interactively-p 'interactive)
(message
"Now kill all Eshell buffers and re-enter Eshell to use %s theme"
(symbol-name theme))))
(error "Theme \"%s\" is not available" theme)))
;; TODO: Support the --help option
;; TODO: Support completing (via pcomplete)
;;;###autoload
(defun eshell/use-theme (&optional theme)
"List all available themes and pick one from Eshell."
(if (null theme)
(progn
(eshell-printn "")
(eshell-printn (make-string 60 ?-))
(eshell-printn (format "%-20s%s" "Name" "Preview"))
(eshell-printn (make-string 60 ?-))
(eshell-printn
(mapconcat (lambda (theme)
(format "%-20s%s"
(symbol-name (car theme))
(funcall (cadr theme))))
eshell-git-prompt-themes "\n"))
(eshell-printn (make-string 60 ?-))
(eshell-printn "")
(eshell-printn "Type 'use-theme theme-name' to use a theme."))
(when (numberp theme)
(setq theme (number-to-string theme)))
(setq theme (intern theme))
(-if-let (func-regexp (assoc-default theme eshell-git-prompt-themes))
(progn
(setq eshell-prompt-function (symbol-function (car func-regexp))
eshell-prompt-regexp (symbol-value (cadr func-regexp)))
(setq eshell-git-prompt-current-theme theme)
(eshell-printn ""))
(error
"Theme \"%s\" is not available.
Run this command again without argument to view all available themes.
usage: use-theme: (&optional theme)"
(symbol-name theme))))
nil)
(provide 'eshell-git-prompt)
;; Local Variables:
;; coding: utf-8
;; End:
;;; eshell-git-prompt.el ends here