Sublime Forum

Trigger status bar on file load and hide it on trigger?

#1

Is it possible when I command + p > select file (hit enter) < have this show the status bar

Then either using keys, arrows or when the view reaches either halfway or the bottom make the status bar hide?

I’d be eternally grateful if anybody can help me, i’ve been stuck on this for hours.

0 Likes

#2

Of course it is possible :wink:

Here’s a proof of concept plugin, which displays the status bar when opening a file and hides it 5 secs later.

import sublime
import sublime_plugin


class EventListener(sublime_plugin.EventListener):

    # reference counter of pending debounce timers
    refs = 0

    # show status bar if file is loaded
    def on_load(self, view):
        window = sublime.active_window()
        if window:
            self.refs += 1
            window.set_status_bar_visible(True)

            def on_time():
                self.refs -= 1
                if not self.refs:
                    window.set_status_bar_visible(False)

            sublime.set_timeout(on_time, 5000)

    # show statusbar if view is activated
    on_activated = on_load

Instead of the timer, each on_...() event handler may be used to hide the statusbar, too.

1 Like

#3

Thank you!

I’m having a small problem sometimes when switching between files the status bar doesn’t show up, it seems to happen if i go back and forth between 2 files or if the file has been visited, any idea what’s causing it

0 Likes

#4

If you want the status bar show up upon view change, too, you’ll just need to handle the on_activated event.

For simplicity just add on_activated = on_load to the class.

I updated the snipped above.

0 Likes

#5

works like a charm thank you so muh :pray:

ideally I wanted to use the j key to scroll like vim but that doesn’t seem to be a option.

which on_ handler would you recommend would be most suitable to use?

would it be possible to calculate the length of the file and hide it after you reach a certain line?

0 Likes

#6

on_query_context(key, operator, operand, match_all)

Called when determining to trigger a key binding with the given context key. If the plugin knows how to respond to the context, it should return either True of False. If the context is unknown, it should return None.

Could I use this with key binding

{"keys": ["j"], "command": "press_key", "args": {"key": "j"}, "context": [{"key": "vi_command_mode_aware"}]},

or am I reading it wrong? if I can how would I go about implementing it.

0 Likes

#7

ST is shipped with a Vintage package which simulates several vi functions. It is disabled by default. If you like vi functionality, you might want to give it a try? Maybe it already includes what you need.

You could look at the ctrl+up and ctrl+down keybindings, which are used to scroll by default.

0 Likes

#8

Sorry I didn’t explain it correctly.

I am using Vintageous and already am using the j, k, l, h for navigation.

I was wondering is there a way to hook into that and use those as the event handler to hide the status bar?

Either that or I was wondering if it’s possible to calculate how many lines are in the file and hide the status bar when it goes past half way?

Thank you for all your help.

0 Likes

#9

I was wondering is there a way to hook into that and use those as the event handler to hide the status bar?

You could add a hook like the following to the EventListener and catch the commands assigned to those keys j,k,l… to do something afterwards.

    def on_post_text_command(self, view, command_name, args):
        if command_name in ('command1', 'command2', 'move'):

Here is a list of all available hooks: https://www.sublimetext.com/docs/3/api_reference.html#sublime_plugin.EventListener

calculate how many lines are in the file and hide the status bar when it goes past half way?

Maybe like this.

cur_row, _ = view.rowcol(view.visible_region().end())
last_row, _ = view.rowcol(view.size())
relative_position = cur_row / last_row

There is also a view.viewport_position() to return the scroll position, but I did not find any function to return the last line position.

0 Likes

#10
def on_post_text_command(self, view, command_name, args):
        if command_name in ('command1', 'command2', 'move'):

This seems internetesting but i’m confused to how it works, i’m not sure what i’m supposed to replace, could you implement it in the above code you provide to maybe toggle the sidebar?

Thanks.

0 Likes

#11

The listener should look like this.

The status bar is always displayed 5secs after activating a view. After that the viewport position decides.

import sublime
import sublime_plugin


class EventListener(sublime_plugin.EventListener):

    # reference counter of pending debounce timers
    refs = 0

    def on_load(self, view):
        window = sublime.active_window()
        if window:
            self.refs += 1
            window.set_status_bar_visible(True)

            def on_time():
                self.refs -= 1
                if not self.refs:
                    self.hide_statusbar_by_scroll_position(view)

            sublime.set_timeout(on_time, 5000)

    on_activated = on_load

    def on_post_text_command(self, view, command_name, args):
        """Check whether to hide or show status bar after cursor movement."""
        if command_name in ('scroll_lines', 'move', 'move_to'):
            self.hide_statusbar_by_scroll_position(view)

    def hide_statusbar_by_scroll_position(self, view):
        cur_row, _ = view.rowcol(view.visible_region().end())
        last_row, _ = view.rowcol(view.size())
        relative_position = cur_row / (last_row + 1) # avoid division by 0
        sublime.active_window().set_status_bar_visible(relative_position < 0.5)
0 Likes

#12
import sublime
import sublime_plugin


class EventListener(sublime_plugin.EventListener):

    # reference counter of pending debounce timers
    refs = 0

    def on_load(self, view):
        window = sublime.active_window()
        if window:
            self.refs += 1
            window.set_status_bar_visible(True)

            def on_time():
                self.refs -= 1
                if not self.refs:
                    self.hide_statusbar_by_scroll_position(view)

            # sublime.set_timeout(on_time, 5000)

    on_activated = on_load

    def on_post_text_command(self, view, command_name, args):
        """Check whether to hide or show status bar after cursor movement."""
        if command_name in ('scroll_lines', 'move', 'move_to'):
            self.hide_statusbar_by_scroll_position(view)


    """ does this trigger the status bar to hide when it goes past half way? """
    def hide_statusbar_by_scroll_position(self, view):
        cur_row, _ = view.rowcol(view.visible_region().end())
        last_row, _ = view.rowcol(view.size())
        relative_position = cur_row / (last_row + 1) # avoid division by 0
        sublime.active_window().set_status_bar_visible(relative_position < 0.5)

After commenting out

sublime.set_timeout(on_time, 5000)

It doesn’t want to hide anymore, even when I scroll, is there a way to keep it from hiding until I change lines instead of closing after 5 seconds?

Ideally say there are 40 lines in a file once I scroll to either line 20 or higher in then I want it to hide, I hope that’s possible.

Thank you.

0 Likes

#13

The normal scrolling is not hookable. Therefore it won’t effect anything.

You could add ‘drag_select’ to the tuple of catched commands -> ('scroll_lines', 'move', 'move_to', 'drag_select')

This would update the statusbar as soon as you click into the view.

You can also try to call sublime.log_commands(True) from ST’s console and check which commands are called by pressing j, and k, Maybe they are different from what I added to the tuple above.

0 Likes

#14

Looks like it’s just keys being pressed as this is all i’m getting.

Hopefully they’ll update their API so we can hook into key presses.

0 Likes

#15

My last guess would be is there any way of chaining toggle_status_bar to run after

{"keys": ["j"], "command": "press_key", "args": {"key": "j"}, "context": [{"key": "vi_command_mode_aware"}]}

Think it’s possible? i’ve been trying to toy around with it for the last half hour with no joy.

0 Likes

#16

You might just need to add the ‘press_key’ to the event handler.

def on_post_text_command(self, view, command_name, args):
        """Check whether to hide or show status bar after cursor movement."""
        if command_name in ('scroll_lines', 'move', 'move_to', 'press_key'):
            self.hide_statusbar_by_scroll_position(view)
0 Likes

#17

Already tried that, unfortunately it didn’t work, had high hopes for that too.

0 Likes