Sublime Forum

Displaying project name on the rite side of the status bar

#1

Hi Guys,

I am working on multiple projects every day (sometimes I have opened multiple projects at the same time) and I need a quick way to check if I have open the right project on sublime. I know that the project name is in title bar but it is not very convenient because I need to search for that word in sometimes quite long text (eg “/home/lampp/htdocs/main/something/somethingelse/file.php (Project) - Sublime Text”).

Best would be for me to have the project name on the right side of the status bar (see attached image)

And additional question - is there any way to add there any color. Something like big color dot beside project name (each project different color)? That would be a huge help for me because may of my projects have same file names (I am Wordpress developer) and sometimes I am editing the right file but in wrong project :slightly_smiling:

0 Likes

#2

This is mostly possible; Text can be added to the status bar although you can’t specify an exact position, and (as far as I am aware) it is not possible to change the color of the text or add an image (e.g. a colored dot).

A simplistic example of this is the following plugin code, which you can save in your Packages\User folder as something like project_in_statusbar.py (use Preferences > Browse Packages if you don’t know where that location is):

[edit]
The code below is modified from the original version I posted so that it includes plugin_loaded method that applies the project name to all views in all windows when Sublime initially starts, since event handlers do not trigger at this time.

It’s also modified to put the project name at the left of the status bar instead of the right, and the project name is slightly different (I find it easier to distinguish with it wrapped in square brackets).
[/edit]

import sublime
import sublime_plugin
import os

def plugin_loaded ():
    # Show project in all views of all windows
    for window in sublime.windows ():
        for view in window.views ():
            show_project (view)

def show_project(view):
    # Is there a project file defined?
    project_file = view.window ().project_file_name ()
    if project_file is not None:
        # Get the project filename without path or extension
        project_name = os.path.splitext (os.path.basename (project_file))[0]
        view.set_status ("00ProjectName", "[" + project_name + "]")

# Display the current project name in the status bar
class ProjectInStatusbar(sublime_plugin.EventListener):
    # When you create a new empty file
    def on_new(self, view):
        show_project (view)

    # When you load an existing file
    def on_load(self, view):
        show_project (view)

    # When you use File > New view into file on an existing file
    def on_clone(self, view):
        show_project (view)

The key to this is the call to view.set_status(). This adds text to the status bar, where we add the text Project: and the name of the current project file without any path or extension. This could be modified to make the text stand out more, such as forcing the name to upper case or wrapping it in [ and ].

The text can’t be positioned at the far right of the bar, though. Sublime sets aside a single part of the status bar for plugins to add text to, and it orders the text added alphabetically by the key that is provided, which here is zzProjectName to try and push it alphabetically to the end of the list.

This puts the text as far right as you can get it, although it still appears to the left of the text that shows you what line and column you are currently at in the file.

2 Likes

#3

Thanks a lot. It works nearly fine. I have adjusted the code a bit so the project name will be on very left instead (renamed the key to 00ProjectName but I have other small problem. It doesn’t work when I open the SublimeText. I need to open file or switch project to make it work. I have tried to add

def plugin_loaded(self,view):
  self.show_project (view)

but it did not help. Any suggestions?

0 Likes

#4

Ahh interesting; I didn’t realize that in that situation, event handlers don’t trigger.

The plugin_loaded method doesn’t get invoked with any arguments, and is a module level function (i.e. outside of the event listener class) so it doesn’t have access directly to the instance method show_project.

I’m by no means an expert in python (so there is undoubtedly a better way to go about this that’s more “python-y”), but I would tweak this by shifting show_project to be module level instead so that plugin_loaded can invoke it as well, since I’m not aware of any way to get at instances of existing event handlers.

I edited the code in my original post to reflect how that might look. It also is slightly changed in that it uses 00ProjectName instead of zzProjectName, and I’ve wrapped the project name in square brackets because I find it easier to pick out on the fly like that; you probably want to modify that according to your own tastes.

0 Likes

#5

I don’t do python at all so you are hundred levels above me anyway.

This works great. I have just edited the code a bit because it seams to me like there is no need to trigger the script again when creating/loading/cloning file, because the project name is already displayed there.

Here is my code:

import sublime
import sublime_plugin
import os

def plugin_loaded ():
    # Show project in all views of all windows
    for window in sublime.windows ():
        for view in window.views ():
            show_project (view)

def show_project(view):
    # Is there a project file defined?
    project_file = view.window ().project_file_name ()
    if project_file is not None:
        # Get the project filename without path or extension
        project_name = os.path.splitext (os.path.basename (project_file))[0]
        view.set_status ("00ProjectName", "[   " + project_name + "   ] ")

So far it works great. Thanks again for your help.

0 Likes

#6

Just for clarification, without the event handler portion of the code, the only views that will include your project name are the ones that are already open at the time that sublime starts, so if you open a file that you didn’t already have open (or create a new one), the status bar will not display the project name while those files have the input focus.

Depending on your workflow, that may or may not be a deal breaker. I’m mostly just mentioning that for future reference purposes.

0 Likes

#7

OK. That’s a different story then. I have posted my reply without properly testing the script. In that case, I need to use your version. Thanks for the clarification. :+1:

0 Likes

#8

Sorry for necrobumping, but I miss this feature too.

@OdatNurd’s code works as expected (tested on ST 3.2.2, build 3211).

As he has noted, it won’t always output the project name in the status, namely, it won’t display it when you create a new project.

I’d like to note that I use Project Manager plugin and for me the above-mentioned issue is solved the following way:

  • I always create a new project uses the ProjectSave Project As…’ (actually, I created a custom shortcut/keybinding for it; the command is save_project_and_workspace_as). I had some issues with Add New Project from Project Manager (it did not save the workspace with project). Note that I always save the projects in [sublime_config_folder]/Packages/User/Projects folder, which is the default for Project Manager.

  • Right after I create a new project, there is no project name in the status bar, but when I open it from the Project Manager (ProjectProject ManagerOpen Project), the name of the project is displayed in the status bar.


If anyone knows how to automatically open a project in the Project Manager, I’ll be very grateful. :smiley:


Update:

I needed to have subfolders in the displayed project name, because all my projects are saved in [sublime_config]/Packages/User/Projects and there I have som subfolders which I need to have displayed in the status bar (I sort the projects into folders).

# I have replace this line
project_name = os.path.splitext (os.path.basename (project_file))[0]

# with these two
regex = re.compile(r'^.*Packages/User/Projects/(.*)\.sublime-project$')
project_name = regex.match(project_file).groups()[0]

Note that I have never ever coded in Python (I prefer other languages).

0 Likes