Creating a custom prompt

Oct 10, 2018
9
0
I haven't used Take Command/TCC in years. Now, I'm struggling with a simple? task and the documentation. Here is what I would like to do:

I want to create a custom prompt that appends the git branch, if the current directory is in fact a git repository. I'm close, but I am flailing at the last step. Here is what I have so far:


Code:
function gitbranch=`%@word[1,%@execstr[git branch]]`


prompt %@if[direxist .git,$P$s(%@gitbranch[])$g,$P$g]

It sort of works, once, but I want it to be invoked every time I change directories. Also, I must be doing something wrong because if I run the prompt command outside of a git branch, the gitbranch function is still being executed. (It seems I should be using IFF instead of @IF, but I can't figure out how to get that to work either).

Also, is there an easy way to debug such things?
 
Last edited:

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,689
106
Albuquerque, NM
prospero.unm.edu
I know nothing about git, but DIREXIST .GIT will be true only if there is a subdirectory named .GIT (with the leading period) within the current directory. Which seems unlikely to me ...?
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,689
106
Albuquerque, NM
prospero.unm.edu
prompt %@if[direxist .git,$P$s(%@gitbranch[])$g,$P$g][/CODE]

It sort of works, once, but I want it to be invoked every time I change directories.

I think you'll need to double the percent signs before @IF and @GITBRANCH. Or else put the whole thing in strong quotes. You want to store the functions in your prompt, so they'll be expanded when the prompt is displayed; not to expand them when you first define the prompt.

Also, I must be doing something wrong because if I run the prompt command outside of a git branch, the gitbranch function is still being executed.

That's right. Both values of the @IF are evaluated, whether the test works out to be true or false. (I'm pretty sure that that's nothing special to @IF; all functions have their parameters expanded before they get to see 'em.)
 
Oct 10, 2018
9
0
I think you'll need to double the percent signs before @IF and @GITBRANCH

That gets me one step further (Thanks!). But, if I am not in a git repo, "git branch" returns an error. Is there any way to suppress that?

Screenshot_2.png
 
Oct 10, 2018
9
0
I got it to work!

Code:
function git_branch=`%@word[1,%@execstr[git branch 2> nul]]`
prompt %%@if[direxist .git,$P$s(%%@git_branch[])$g,$P$g]
 
Oct 10, 2018
9
0
Ok! Now that I got that to work, I want to color it. But, I'm doing something wrong again, because I am getting a syntax error:

Code:
prompt %%@if[direxist .git, $e[94;40;1m $P$s $e[92;1m(%%@git_branch[]) $e[94;1m$g $e[37;40;1m,$P$g]

TCC: Syntax error "@git_branch["
) > ,C:\Users\robin_dev\Projects\github-demo>]

This is the prompt I want to have (with the git branch, if any, appearing between the parenthesis:
Code:
prompt $e[94;1m$p$s$e[92;1m( )$e[94;1m$g $e[37;40;1m
 
Last edited:
Oct 10, 2018
9
0
With help from Charles and Scott, I got what I wanted!

Code:
function gitbranch=`%@execstr[set _gb=%@execstr[git rev-parse --abbrev-ref HEAD 2>nul] & if %[_gb]. != . echo. (%_gb) & unset _gb]`
prompt=`$e[94;1m$p$e[92;1m%@gitbranch[]$e[94;1m$g $e[37;40;1m`
 

Similar threads