swiftbar / SwiftBar

Powerful macOS menu bar customization tool
https://swiftbar.app
MIT License
2.93k stars 92 forks source link

rebuilding dropdown menu items from clicking an entry #265

Closed mlcampbe closed 2 years ago

mlcampbe commented 2 years ago

I have written speedtest plugin that run speedtest-cli to check my internet speed. This outputs a correct value and I see it in the dropdown menu. I've also got a list of additional servers that I would like to manually test against in the dropdown menu as well. For example, the following line:

echo " 40882) Cumberland Connect (Clarksville, TN, United States) [193.86 km] | shell='$0' param1='32539' terminal=true trim=false refresh=true"

When I click the entry it opens a new terminal window and I can see that it runs "/Users/username/tools/swiftBar-plugins/speedtest.1h.sh 40882" as expected. The terminal window shows the output from the execution against the correct speedtest server but the dropdown menu does not get updated. It appears that when I click the entry it runs in its own space and swiftbar is not aware of the output.

Is it possible to capture the output from clicking the dropdown menu item and have that replace the existing dropdown menu text?

melonamin commented 2 years ago

SwiftBar and current plugin API doesn't support partial menu updates, the current model is stateless and will probably stay that way since it is way easier to maintain.

So, to achieve what you want you have to refresh the whole menu. You can preserve test results by passing it as a parameter to your script.

Does it make sense?

mlcampbe commented 2 years ago

I believe that I am refreshing the entire plugin as the execution of the script outputs the icon image and all dropdown entries. Yet what I am seeing is that once the script is re-executed by selecting an alternate server the script runs successfully with the newly selected server but its output is discarded.

Below is the text of the script if you can review it for me.

speedtest.zip

melonamin commented 2 years ago

I've looked at the script, your menu items looks like this:

City of Bellevue (Bellevue, WA, United States) [8.94 km] | shell='/SwiftBar/#265/speedtest. 1h.sh' param1='36480' terminal=false trim=false refresh=true
Whitesky Communications LLC (Seattle, WA, United States) [17.12 km] | shell='/SwiftBar/#265/speedtest.1h. sh' param1='22168' terminal=false trim=false refresh=true
DediPath (Seattle, WA, United States) [17:12 km] | shell='/SwiftBar/#265/speedtest. 1h.sh' param1='45799' terminal=false trim=false refresh=true
...

Let's go through parameters:

So what happens is you launch a background script with a parameter, but result of this script is ignored and in addition to that you refresh the plugin(launch speedtest. 1h.sh without parameters). So double the work and no visible result in the menu bar!

I hope this explains what is going on.

mlcampbe commented 2 years ago

So basically what you are saying is when I click and entry in the dropdown menu that it executes the shell= command in a separate process that has no interaction with the swiftbar process at all. Therefore I can't capture the output from the shell= command execution to rebuild the swiftbar menu. Correct?

melonamin commented 2 years ago

Yep, exactly.

mlcampbe commented 2 years ago

Ok thanks.