Showing posts with label utility. Show all posts
Showing posts with label utility. Show all posts

Mar 11, 2016

Showing mercurial current branch in ConEmu prompt

As a long time git user, I had a relatively hard time when I started using mercurial CVS; one of the things that I really missed (this one goes to the list of annoyances) was the lack of support for showing the current branch in the console. In the following screenshot you can see that GIT console does show me the current branch (in the example, ofx)


Considering that I have a terrible memory and usually have at least 5 ~ 6 consoles open at any given time, it is not hard to conclude that, from time to time, I simply forget which is the current branch in each console. 

Since mercurial do not provide any help (other than keeping issuing hg branch command all the time ;), it happened, not once, but many times, that I found myself working in one console under the assumption that I was operating on branch Foo when in fact I was working in a completely different one.

Some days ago I had the idea of using mercurial hooks to detect when I am changing branches and setting the console prompt accordingly, but that did not worked out very well. To be fair, mercurial hooks worked as expected; the problem was that my hook was run by hg.exe process which itself is spawned from the console when one type, for instance, hg up -r Foo to change to branch Foo and this process (hg.exe) would need to change the PROMPT of its parent console, which is not allowed (at least AFAIK).

My next step was check whether ConEmu (it is not a secret that I'm in love with ConEmu ;))  had some support to achieve my goal and bingo! ConEmu support some extended ANSI codes, more specifically $E ] 9 ; 7 ; ”cmd“$E\  which allows one to execute the command specified in the string (cmd) and use its output as part of the PROMP string. 

With that information, I just created the following batch file:

@echo off
IF NOT EXIST .hg GOTO NOT_A_MERCURIAL_REPO
hg branch
GOTO END

:NOT_A_MERCURIAL_REPO
@echo !repo
:END

and set the PROMP to:

SET PROMPT=$E[32m$E]9;8;"USERNAME"$E\@$E]9;8;"COMPUTERNAME"$E\$S$E[1D$S$E[92m$P$E[90m$E[38;5;14m $Q$G $E]9;7;"path_to_above.bat"$E\$E[90m$G$E[m$S

and voalá!



In the picture above you can see that when the current directory is not a mercurial repository the string "!repo" is shown (I'll probably change that later and in this case will show nothing); also, as soon as I initialized a local repository the PROMPT changed to indicate that I was in the default branch. 

Now, the only missing bit was to configure ConEmu to automatically set that prompt when it creates a new console, which can be done by specifying a custom batch file (in my case I created a file named ConEmuInit.cmd):




Basically I copied the original file (that can be found in "%ConEmuBaseDir%\CmdInit.cmd") and change it accordingly.

Now I am happier ;) The changes of making mistakes are small now :)

Edit:
If you are using CONEMU  Build 160609 or newer you'll need to include you bath in a white list in order to CONEMU to run it:



EDIT (06/Feb/2018)
The path set above is case sensitive! (I've spent 3 hours hunting ghosts) i.e, if you use D:\utils\branch.bat in the PROMPT variable, adding d:\utils\branch.bat will not work.
 
Happy coding

Leia este post em Português!

May 24, 2014

Useful utility: doskey

Leia este post em Português

If you use the Windows console in your everyday tasks you probably is a very sad person :)

Let me explain, you should move to a better console ;) And we have a couple of options (as I stated here).

But this is not the focus of this post. My point is, if you rely on a console on Windows (whenever cmd.exe, ConEmu, whatever) and you want to be able to create aliases to applications you can use a built in command (at least on Windows 7 / 8) called doskey (you can find it in %windir%\System32\doskey.exe) (it probably exists on all Windows versions all way down to Windows 3.0 and even to DOS 4.2 - I remember using it on such OSs :).

For instance, if you want to make it more convenient to open a text file in Sublime (or any other text editor) type the following command at your console (replacing the path to sublime executable with the path to your text editor:
doskey subl = "%ProgramFiles%\Sublime Text 2\sublime_text.exe" $* 
and from now on, on that console, you can simply type:
subl my-text-file.ext
and your file will open in Sublime!

Hope you find this info useful.

Happy coding.

Feb 26, 2014

Useful command line utility: Clip

Are you a heavy Windows console user? Have you ever copied the output from a program to the clipboard by using the context menu? Well, are you aware of clip.exe that comes with Windows (at least it is present on Windows 7 and newer) ? 

It simply copy whatever it gets from the standard input to the clipboard! So you can do things like:


c:\temp>dir | clip

And get the contents of dir output to the clipboard (and of course, now you can past it wherever you want)!

Of course you can also redirect the standard input:
c:\temp>clip < file.txt

Happy coding!

(this post in portuguese)