Update Title Bar to show current Branch

Jun 9, 2010 at 4:00 PM
Hi, I wrote up a little addin to Visual Studio that would change the Visual Studio Title Bar to show the current branch. I imagine other people might like it too. If you like you could roll the code at the bottom of this post into your addin. Thanks for the great tool, Jake #region Member Variables Timer titleTimer; Timer branchTimer; string branch = ""; const string BranchPrefix = " (Branch: "; #endregion #region Properties string SolutionPath { get { return new FileInfo(_applicationObject.Solution.FullName).Directory.FullName; } } #endregion public Connect() { } void titleTimer_Elapsed(object sender, ElapsedEventArgs e) { try { var currentCaption = _applicationObject.MainWindow.Caption; var prefix = currentCaption; if (currentCaption.Contains(BranchPrefix)) { prefix = currentCaption.Substring(0, currentCaption.IndexOf(BranchPrefix)); } IntPtr hWnd = (IntPtr)_applicationObject.MainWindow.HWnd; SetWindowText(hWnd, prefix + branch); } catch (Exception) { } } void branchTimer_Elapsed(object sender, ElapsedEventArgs e) { var process = new System.Diagnostics.Process(); var processInfo = new ProcessStartInfo(); processInfo.RedirectStandardOutput = true; processInfo.UseShellExecute = false; processInfo.FileName = "hg"; processInfo.Arguments = "branch"; processInfo.WorkingDirectory = SolutionPath; processInfo.WindowStyle = ProcessWindowStyle.Hidden; processInfo.CreateNoWindow = true; process.StartInfo = processInfo; process.Start(); var line = process.StandardOutput.ReadLine(); if (!line.StartsWith("abort:")) { branch = BranchPrefix + line + ")"; } process.WaitForExit(); } [DllImport("user32.dll")] private static extern bool SetWindowText(IntPtr hWnd, string lpString); public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; titleTimer = new Timer(1000); titleTimer.Elapsed += new ElapsedEventHandler(titleTimer_Elapsed); titleTimer.Start(); branchTimer = new Timer(30000); branchTimer.Elapsed += new ElapsedEventHandler(branchTimer_Elapsed); branchTimer.Start(); } public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) { branchTimer.Dispose(); titleTimer.Dispose(); }
Jun 9, 2010 at 4:03 PM
Sorry for the loss of formatting, hopefully VS.NET will clean it up when you paste. Otherwise DM me and I will email you a better version.
Coordinator
Jun 14, 2010 at 7:53 PM

Nice idea. But I have in mind that one solution can be consists of more than one Repo/Branch, so which Branch should be shown - my be the current project one.

I will see if it works for VisualHG. Thanks for your work,  Bernd