Sublime Forum

Help trimming full path in status bar [SOLVED]

#1

Does anybody know if it’s possible to trim the full path?

import sublime
import sublime_plugin

    # do something here
class CurrentPathStatusEvent(sublime_plugin.EventListener):
    def on_activated(self, view):
        test = view.file_name()
        view.set_status('currentPath', test[25:])

I’ve come up with this so far but the directory isn’t always going to be the same amount of characters, how would I go about getting the directory name to calculate it?

Where as I only want this to show

Edit: Solved

import sublime
import sublime_plugin

    # do something here
class CurrentPathStatusEvent(sublime_plugin.EventListener):
    def on_activated(self, view):
        test = view.file_name()
        dir = view.file_name().split('/')[4]
        num = len(dir) + 20
        view.set_status('currentPath', test[num:])

For anybody wanting to use this you’d have to change the number depending on your name.

This does the trick, hacked this together, don’t judge I don’t know python :blush:

0 Likes

#2

Your original post was doing it the right way, using os.path.split, only you were capturing the result of the split in a variable and then setting the filename into the status area instead of using the filename you split away.

Note that your third edited version will blow up if you’re using Goto Anything because when you’re previewing files the filename is not always present. To be safe, you should test if it’s None before you proceed.

An example (based on your original unedited post) would be this:

import sublime
import sublime_plugin
import os

class CurrentPathStatusEventHandler(sublime_plugin.EventListener):
    def on_activated(self, view):
        if view.file_name() is None:
            view.erase_status("_filename")
        else:
            filename = os.path.split(view.file_name())[1]
            view.set_status("_filename", filename)
0 Likes

#3

Thanks for replying :slight_smile:

I would like to trigger the status bar every time a file is opened, but hide itself as soon as I start scrolling either using a mouse or using j like in vim.

Is that possible?

Thanks.

Edit: just implemented the if else (y)

0 Likes

#4

I think it’s possible, but it seems pretty tricky to do correctly, since there’s no ready event handler for knowing where (or how) the text view is scrolling.

There are some potential workarounds, but they would rely on being able to tell when on_load is triggering as a result of you actually loading a file versus file previews (e.g. using Goto Anything to pick a file).

0 Likes

#5

I came across this Key Down Event

import sublime_plugin
import sublime
from random import randrange
DEFAULT_WAIT = 300
class CenterCurrentLineCommand(sublime_plugin.TextCommand):
    def __init__(self, view):
        self.view = view
        settings = sublime.load_settings('Default.sublime-settings')
        view.settings().set('center_current_line', settings.get('center_current_line',  True))
    def run(self, args):
    sel = self.view.sel()
    if len(sel) == 1 and self.view.settings().get('center_current_line'):
        self.view.show_at_center(sel[0])
class CenterCurrentLineOnCommand(sublime_plugin.TextCommand):
    def __init__(self, view):
        self.view = view
    def run(self, args):
    self.view.settings().set('center_current_line', True)
class CenterCurrentLineOffCommand(sublime_plugin.TextCommand):
    def __init__(self, view):
        self.view = view
    def run(self, args):
    self.view.settings().set('center_current_line', False)
class CenterLineEventHandler(sublime_plugin.EventListener):
    def __init__(self):
        self.wait_id = None
    def check_wait(self, wait_id):
    if self.wait_id != wait_id:
        new_wait_id = randrange(1, 999999)
        self.wait_id = new_wait_id
        sublime.set_timeout(
            lambda: self.check_wait(wait_id=new_wait_id),
            self.wait_ms
        )
    else:
        self.wait_id = None
        window = sublime.active_window()
        view = window.active_view() if window != None else None
        if view != None:
            view.run_command('center_current_line')

def wait(self):
    new_wait_id = randrange(1, 999999)
    if self.wait_id == None:
        self.wait_id = new_wait_id
        sublime.set_timeout(
            lambda: self.check_wait(wait_id=new_wait_id),
            self.wait_ms
        )
    else:
        self.wait_id = new_wait_id

def on_selection_modified(self, view):
    self.wait_ms = view.settings().get('center_current_line_wait', DEFAULT_WAIT)
    self.wait()

Is this what i’m looking for?

The work flow I want is command + p > browse to a file (have the status bar appear when I hit enter) > and disappear when I either the k, j or i key is pressed or any of the arrows, and if possible maybe even scrolling with the mouse ( but not essential )

Is there any chance you could make me a quick POC i’d be eternally grateful.

0 Likes

#6

Just came across this, can’t figure out if it’ll be helpful for me though, I can’t seem to get it to toggle only when i’m saving a file.

0 Likes