Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Creating a custom prompt

Oct
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:
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 ...?
 
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.)
 
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
 
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]
 
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:
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

Back
Top