Skip to content

Commit

Permalink
Merge pull request #66 from MLFlexer/50-add-default-workflow-to-savel…
Browse files Browse the repository at this point in the history
…oad-state-based-on-tab-title

50 add default workflow to saveload state based on tab title
  • Loading branch information
MLFlexer authored Oct 5, 2024
2 parents 1462398 + 5f29743 commit d7f4eed
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local wezterm = require("wezterm")
local resurrect = wezterm.plugin.require("https://github.com/MLFlexer/resurrect.wezterm")
```

2. Saving workspace and/or window state based on name and title:
2. Saving workspace, window and/or tab state based on name and title:

```lua
local resurrect = wezterm.plugin.require("https://github.com/MLFlexer/resurrect.wezterm")
Expand All @@ -41,6 +41,11 @@ config.keys = {
mods = "ALT",
action = resurrect.window_state.save_window_action(),
},
{
key = "T",
mods = "ALT",
action = resurrect.tab_state.save_tab_action(),
},
{
key = "s",
mods = "ALT",
Expand All @@ -60,30 +65,27 @@ local resurrect = wezterm.plugin.require("https://github.com/MLFlexer/resurrect.
config.keys = {
-- ...
{
key = "l",
key = "r",
mods = "ALT",
action = wezterm.action_callback(function(win, pane)
resurrect.fuzzy_load(win, pane, function(id, label)
local type = string.match(id, "^([^/]+)") -- match before '/'
id = string.match(id, "([^/]+)$") -- match after '/'
id = string.match(id, "(.+)%..+$") -- remove file extension
local state
id = string.match(id, "(.+)%..+$") -- remove file extention
local opts = {
relative = true,
restore_text = true,
on_pane_restore = resurrect.tab_state.default_on_pane_restore,
}
if type == "workspace" then
state = resurrect.load_state(id, "workspace")
resurrect.workspace_state.restore_workspace(state, {
relative = true,
restore_text = true,
on_pane_restore = resurrect.tab_state.default_on_pane_restore,
})
local state = resurrect.load_state(id, "workspace")
resurrect.workspace_state.restore_workspace(state, opts)
elseif type == "window" then
state = resurrect.load_state(id, "window")
resurrect.window_state.restore_window(pane:window(), state, {
relative = true,
restore_text = true,
on_pane_restore = resurrect.tab_state.default_on_pane_restore,
-- uncomment this line to use active tab when restoring
-- tab = win:active_tab(),
})
local state = resurrect.load_state(id, "window")
resurrect.window_state.restore_window(pane:window(), state, opts)
elseif type == "tab" then
local state = resurrect.load_state(id, "tab")
resurrect.tab_state.restore_tab(pane:tab(), state, opts)
end
end)
end),
Expand Down Expand Up @@ -202,12 +204,13 @@ You can checkout my configuration [here](https://github.com/MLFlexer/.dotfiles/t
You can add the `opts` table to change the behaviour. It exposes the following options:

```lua
---@param opts? { interval_seconds: integer?, save_workspaces: boolean?, save_windows: boolean? }
---@param opts? { interval_seconds: integer?, save_workspaces: boolean?, save_windows: boolean?, save_tabs: boolean? }
```

`interval_seconds` will save the state every time the supplied number of seconds has surpassed.
`save_workspaces` will save workspaces if true otherwise not.
`save_windows` will save windows if true otherwise not.
`save_tabs` will save tabs if true otherwise not.

### Limiting the amount of output lines saved for a pane

Expand Down
12 changes: 12 additions & 0 deletions plugin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,18 @@ function pub.periodic_save(opts)
end
end

if opts.save_tabs then
for _, gui_win in ipairs(wezterm.gui.gui_windows()) do
local mux_win = gui_win:mux_window()
for _, mux_tab in ipairs(mux_win:tabs()) do
local title = mux_tab:get_title()
if title ~= "" and title ~= nil then
pub.save_state(pub.tab_state.get_tab_state(mux_tab))
end
end
end
end

pub.periodic_save(opts)
end)
end
Expand Down
25 changes: 25 additions & 0 deletions plugin/resurrect/tab_state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ function pub.restore_tab(tab, tab_state, opts)
wezterm.emit("resurrect.tab_state.restore_tab.finished")
end

function pub.save_tab_action()
return wezterm.action_callback(function(win, pane)
local resurrect = wezterm.plugin.require("https://github.com/MLFlexer/resurrect.wezterm")
local tab = pane:tab()
if tab:get_title() == "" then
win:perform_action(
wezterm.action.PromptInputLine({
description = "Enter new tab title",
action = wezterm.action_callback(function(_, callback_pane, title)
if title then
callback_pane:tab():set_title(title)
local state = pub.get_tab_state(tab)
resurrect.save_state(state)
end
end),
}),
pane
)
elseif tab:get_title() then
local state = pub.get_tab_state(tab)
resurrect.save_state(state)
end
end)
end

--- Function to restore text or processes when restoring panes
---@param pane_tree pane_tree
function pub.default_on_pane_restore(pane_tree)
Expand Down

0 comments on commit d7f4eed

Please sign in to comment.