Sublime Forum

How to get the time for executing an API call and set the status for that duration?

#1

How do I set the status in the status bar for the duration when an external API call is made ?
For example, let’s say I’m fetching a list of posts from an external API end point and displaying them in a window.show_quick_panel (). There will be some time before the posts are fetched the and panel being shown. I would like to set the status to saying something like “Posts retrieving …” or something along those lines in the status bar during that time to give the user an indication of what’s happening. But the problem is I don’t know how long the API call will take. How can I achieve this ?

0 Likes

#2

I think you might be overthinking this one; when you start the request that will ultimately get data for you, use view.set_status() to add a message saying what you’re doing, and when the answer is retrieved and you’re about to show the quick panel, use view.erase_status() to remove the status that you added. Essentially you don’t need to know ahead of time how long it will take.

Note that view.set_status() only applies to the current file, so you may need an event listener to notice when a new file is focused or opened so you can set the same status key there. Make sure you erase_status() in all views when you’re done.

0 Likes

#3

You mean something like this ?

view.set_status("Key", "Retrieving")
# Do data fetching 
view.erase_status("Key")
window.show_quick_panel(...)
0 Likes

#4

if you don’t mind having a dependency on PackageLib, then it has an activity indicator modelled on Package Control’s:

1 Like

#5

Thanks for the suggestion but to have a dependency just for an activity indicator seems to be overkill so I was actually looking for options within the API itself. I’ll definitely have a look at the snippet though.

0 Likes

#6

That particular dependency has a lot of wrappers and helpers for common tasks in plugins, so depending on what you’re trying to do you may find it useful for a lot of other things as well and not just this.

That said, I find it’s often a good idea to be familiar with what a library like this is doing for you behind the scenes because if you know how something is working behind the scenes you can trouble shoot problems easier.

So basically, I’d recommend both approaches. :smiley:

1 Like

#7

I’d definitely try sublime_lib one of these days but the docs are as usual very terse and very little examples of how to use the API are given which is quite demotivating for a newbie.

1 Like