Sublime Forum

Change how the show_quick_panel truncates strings or truncate strings by getting quick panel width

#1

I am writing something to select text to be inserted that has already been stored in a register but the quick panel truncates the text randomly if it is too long. This also depends on the width of the quick panel which changes depending on the size of the sublime text window.

Ideally I want the text to display like:
a: This is the text if it comes to the end of the window it has three dots...

But it displays like this:
a: T....balah blah blah....and the end of the window

I was wondering if you can tell the quick panel to just truncate text at the end? Or if not then can you detect the quick panel width depending on the window’s width (or even the window width might work) so the text can be manually truncated before being passed to the quick panel.

1 Like

The show_quick_panel overlay often truncates the longest string
#2

I’m experiencing a similar issue while implementing some modifications to @BugFix’s File Browser.

For some reason, certain entries are truncated, even though they are stored correctly in their variables & various entries with a greater length are still displayed correctly:

 

 

1 Like

#3

Yeah that seems to be weird that it truncates it by two characters. What happens if two spaces are at the end of the first one? I wonder how the quick panel decides the size to show as you would think it should try to be as large as necessary to fit the text.

So do you know anyway to get the width of the current view that the quick panel comes up in?

0 Likes

#4

 
Idk, I might revisit it later to see if I can figure it out - got a ton of other modules I’m trying to wrap up for now.

 


 
This works pretty well on views ( assuming that a monospace font is active ), but seems a bit inaccurate if used on a quick panel:

characterWidth      = self.view.em_width()
viewWidth           = self.view.viewport_extent()[ 0 ]
lineCharacter_Count = viewWidth / characterWidth

print( characterWidth )
print( viewWidth )
print( lineCharacter_Count )

 
Not sure if it’s possible to adjust for a dynamic-width font.  Maybe figure out a way to get the font + size & loop through each character in the string ( getting a sum of each character’s width ) ?

0 Likes

#5

No, you can’t. Your only solution is not to make the quick panel truncate at all, as @fico seemingly attempts to do.
It does seem that the maximum quick panel width is based on the viewport extends however.

0 Likes

#6

Yes I ended up using the viewport extends to set the amount of text to be shown in the quick panel to be roughly the correct length which worked really well. I still have the problem @fico states that it randomly truncates sometimes when it doesn’t need to as longer text could easily fit.

I thought about trying to pit a last line below all others with just --------- that could probably be changed to all spaces maybe so that the user might not notice that was maybe 3 to 10 extra characters longer than the longest text. To see if that would make it not truncate the other text but still have not tried that. This wouldn’t be ideal but just curious if @fico tried anything like this?

1 Like

#7

@stdarkstar

 
Seems to work, good call :+1:

Just tried it with like 1204912 spaces & maxed out the QuickPanel width.  Tested some directories that were truncating before & no longer experienced the issue.

 

 

I’ll probably add something to keep track of maxEntryLength while the list is populating & then thrown in something like:
lastEntry += ( " " * ( ( maxEntryLength - len( lastEntry ) ) + 3 ) )

1 Like

#8

Okay awesome I will give that a try then as I thought it might work due to the randomness of the truncating. Just didn’t try because will need to change some other code to error check if it was the last entry and do nothing. Or possibly since it will be selectable anyway. I will make it say something like Press Here to Exit Quick Panel but this would be used for the fuzzy search so maybe not ideal.

@fico
I assume there is no way to tell the quick panel to not be able to select or press enter on certain entries?

0 Likes

#9

 
Yes, you can actually.  Check out Select File/Folder Dialog with QuickPanel, specifically the on_selection function.

Basically; check the selected entry during an internal onDone function, and if it matches some particular text - you can exit the quickpanel, re-open it to give the impression that the user’s click had no effect, run a specific function, etc.  Otherwise, run the intended callback & pass the selection as an argument.

1 Like