MLFlexer / resurrect.wezterm

A plugin to restore windows, tabs and panes for Wezterm inspired by tmux-resurrect
MIT License
74 stars 2 forks source link

feat: toast notification on_save, on_resurrect, on_periodic_save, on_error #25

Closed joncrangle closed 3 months ago

joncrangle commented 3 months ago

Looking for your thoughts on a feature to initiate a toast notification following successful/unsuccessful plugin events. We could let the user override defaults according to their preference.

Wezterm provides a window:toast_notification() that we can use to provide some optional feedback:

window:toast_notification('Workspace state saved', nil, 4000)

Defaults could look something like:

---@class NotificationSettings
---@field on_save boolean
---@field on_resurrect boolean
---@field on_periodic_save boolean
---@field on_error boolean

---@type NotificationSettings
local default_notifications = {
    on_save = true,
    on_resurrect = false,
    on_periodic_save = false,
    on_error = true,
}

--- Changes the default notification settings
---@param settings NotificationSettings A table containing optional fields `on_save`, `on_resurrect`, `on_periodic_save` and `on_error`
function pub.notification(settings)
    for key, value in pairs(settings) do
        if default_notifications[key] ~= nil then
            default_notifications[key] = value
        end
    end
end

Then, depending on the value of default_notifications we could run a window:toast_notification function.

MLFlexer commented 3 months ago

I personally think the toast notification is very obstructive, so I would personally not use it for anything other than errors. I dont mind the feature if it is opt-in/out, so what you are proposing might be good.

Is it something you are interested in using, if so then I am all for it 😄 But if you are not, then it could be implemented but be turned off for all notifications except errors

joncrangle commented 3 months ago

I'll play around with this feature a bit and make sure to set the defaults to only show a toast on_error. Personally, I would only use toasts on_save and on_error. I don't think an on_resurrect option is particularly useful since it is obvious when this succeeds. An on_periodic_save would be intrusive, but I could provide this option and disable it by default.

To send a toast, the user would need to call functions passing in a window object, so if no window object is passed in there wouldn't be any toasts. I'll make sure everything still operates without this opt to make sure the current API still works as-is.

MLFlexer commented 3 months ago

What if there was an event emitted when saving, resurrecting and for the periodic save via. wezterm.emit, then there could be a default wezterm.on callback which could show the toast (for on save only if that is what should be default). This could of course be disabled/enabled at will for the user. This would also make it possible for users to have other callback functions when these events are emitted

And then keep the toasts for on_error without a wezterm.emit?

joncrangle commented 3 months ago

Now that you bring up wezterm.emit, I think this is a better option than including toasts within the plugin.

We could leave it entirely up to the user to setup wezterm.on events in their config if they would like toasts.

We could emit events and errors and provide sample code in the docs about how to create toasts from these events, but the emitted events could be used for other purposes (for instance someone might want to initiate a syncthing process on save, or their own encryption mechanism outside of the plugin).

MLFlexer commented 3 months ago

I will make a new issue for implementing wezterm.on in the various functions.

I do like the idea of having some toast notification when an error occurs, like if there is an error when writing the file or if the user is trying to use encryption but havn't done the setup with their keys ect. Will also open a new issue for this