Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restoring workspace into current window #70

Closed
SamLarenN opened this issue Nov 23, 2024 · 17 comments · Fixed by #88
Closed

Restoring workspace into current window #70

SamLarenN opened this issue Nov 23, 2024 · 17 comments · Fixed by #88
Assignees

Comments

@SamLarenN
Copy link

Is it possible to restore the workspace into the current window instead of opening a separate window?
It would be nice to open a workspace as a workspace accessible in the workspace list in Wezterm. This would keep the rest of the workspaces available as well.

@joncrangle
Copy link
Contributor

You should be able to restore within your current window by passing in the following as opts:

            resurrect.window_state.restore_window(pane:window(), state, {
              relative = true,
              restore_text = true,
              on_pane_restore = resurrect.tab_state.default_on_pane_restore,
              -- the following line should use active tab when restoring
              tab = win:active_tab(),
            })

@travisjbeck
Copy link

Im having the same issue. setting

tab = win:active_tab()

had no effect, the workspace still opened in a new window.

I've also tried:

window = win:mux_window(),

This works, but the first tab always reset it's CWD. Like if I had NVIM open to .dotfiles in that tab, it will still have nvim open, but the CWD will my default cwd.

Any solution?

@joncrangle
Copy link
Contributor

@travisjbeck have you configure OSC 7 integration with Wezterm? This might be why the new tab is not opening in the expected directory. See Wezterm docs - Shell Integration.

@Snejj
Copy link

Snejj commented Jan 6, 2025

@travisjbeck have you configure OSC 7 integration with Wezterm? This might be why the new tab is not opening in the expected directory. See Wezterm docs - Shell Integration.

I just set this up by copying the shell integration script and sourcing it at the end of my .zshrc, and with the same config mentioned above my cwd in the first tab of my saved workspace is still getting set to the default. Not sure how to troubleshoot this on my end

@MLFlexer
Copy link
Owner

MLFlexer commented Jan 9, 2025

@travisjbeck have you configure OSC 7 integration with Wezterm? This might be why the new tab is not opening in the expected directory. See Wezterm docs - Shell Integration.

I just set this up by copying the shell integration script and sourcing it at the end of my .zshrc, and with the same config mentioned above my cwd in the first tab of my saved workspace is still getting set to the default. Not sure how to troubleshoot this on my end

Strange, what platform are you on? Mac, Windows using a local domain? Windows using WSL-domain? Linux? Unix socket?
Any information about the platform would be a great help! 😄

Also have you tried updating the plugin? You can do so with the documentation which is described here

@Snejj
Copy link

Snejj commented Jan 9, 2025

Thanks for the quick response! This is on Mac using a local domain. The plugin is up to date but no luck 😕

@tcchau
Copy link

tcchau commented Jan 10, 2025

Thanks for a very useful plugin @MLFlexer . I'd like to add to this thread by mentioning that the new workspace takes on whatever directory the window/pane/tab was in, when you resurrect the workspace. I am currently also trying to configure this behaviour as my own preference as well, i.e. resurrect the CWD that was saved with the workspace originally. I'm on MacOS.

@travisjbeck
Copy link

@travisjbeck have you configure OSC 7 integration with Wezterm? This might be why the new tab is not opening in the expected directory. See Wezterm docs - Shell Integration.

I did not have that initially, but after adding it, the first tab still loses the CWD. I'm on Mac 15.1.1

I also updated the plugin.

@MLFlexer
Copy link
Owner

Thanks for a very useful plugin @MLFlexer .

Thanks for the kind words!

I'd like to add to this thread by mentioning that the new workspace takes on whatever directory the window/pane/tab was in, when you resurrect the workspace.

And thank you for looking into this, I don't really use this feature myself, so it is great to have information like this.

I will try and work on this in the following days, my life has been really busy lately, so I havn't had much time to work on it 😄

@MLFlexer
Copy link
Owner

@travisjbeck have you configure OSC 7 integration with Wezterm? This might be why the new tab is not opening in the expected directory. See Wezterm docs - Shell Integration.

I did not have that initially, but after adding it, the first tab still loses the CWD. I'm on Mac 15.1.1

I also updated the plugin.

Thanks for keeping me updated, I think I have an idea of a fix, and will work on this in the following days

@MLFlexer MLFlexer self-assigned this Jan 23, 2025
@MLFlexer MLFlexer linked a pull request Jan 27, 2025 that will close this issue
@MLFlexer
Copy link
Owner

Update the plugin and checkout the new readme section if you want to be able to restore into the current window 😄

Also please report all bugs you find related to this, as I am not myself a user of this feature, so I don't know if there are bugs

@travisjbeck
Copy link

Thanks for the update. Unfortunately, I still have the same issue with the first tab. I tried what you had in the readme, but my config is workspace based, i believe. My dotfiles are here if you want to take a look at my configuration.

@MLFlexer
Copy link
Owner

Thanks for the update. Unfortunately, I still have the same issue with the first tab. I tried what you had in the readme, but my config is workspace based, i believe. My dotfiles are here if you want to take a look at my configuration.

I can only view the commit from 1 month ago, can you paste your resurrect/config.lua here, so I can see the version with the updated opts?

Also just to make sure, you have updated the plugin and updated the config with the following opts?

local opts = {
  close_open_tabs = true, -- **THIS IS THE NEW OPTION**
  window = pane:window(), -- **THIS IS ALSO NEEDED**
  on_pane_restore = resurrect.tab_state.default_on_pane_restore,
  relative = true,
  restore_text = true,
}

And made sure that the opts table does not contain a tab field/key?

@travisjbeck
Copy link

Thanks for the update. Unfortunately, I still have the same issue with the first tab. I tried what you had in the readme, but my config is workspace based, i believe. My dotfiles are here if you want to take a look at my configuration.

I can only view the commit from 1 month ago, can you paste your resurrect/config.lua here, so I can see the version with the updated opts?

Also just to make sure, you have updated the plugin and updated the config with the following opts?

local opts = {
close_open_tabs = true, -- THIS IS THE NEW OPTION
window = pane:window(), -- THIS IS ALSO NEEDED
on_pane_restore = resurrect.tab_state.default_on_pane_restore,
relative = true,
restore_text = true,
}
And made sure that the opts table does not contain a tab field/key?

Here is my config file. My repo is up to date as well now.

-- File: resurrect/config.lua
-- resurrect.wezterm configuration and settings
--
-- This module:
-- * Configures the resurrect.wezterm plugin
-- * Configures event listener configuration (via an additional required file)
-- * Returns wezterm keybinding configuration for resurrect-related actions.
--
-- The main wezterm configuration is then responsible for merging the
-- keybindings with other keybindings, or setting up its own.
-- plugins are stored at ~/Users/Travis/Library/Application Support/wezterm/plugins/

local config = {}
local wezterm = require("wezterm")
local resurrect = wezterm.plugin.require("https://github.com/MLFlexer/resurrect.wezterm")
local act = wezterm.action
local mux = wezterm.mux

-- resurrect.wezterm periodic save every 5 minutes
resurrect.periodic_save({
	interval_seconds = 300,
	save_tabs = true,
	save_windows = true,
	save_workspaces = true,
})

resurrect.set_encryption({
	enable = true,
	method = "/opt/homebrew/bin/age",
	private_key = wezterm.home_dir .. "/.age/resurrect.txt",
	public_key = "age1749r4a5hq6wepxrpqqpfh385swyf9u7rmakawlp5mmulzw979ams23uqlw",
})

wezterm.on("resurrect.save_state.finished", function()
	wezterm.gui.gui_windows()[1]:toast_notification("Session Saved", "", nil, 4000)
end)

-- Save only 5000 lines per pane
resurrect.set_max_nlines(5000)

-- Default keybindings
-- These will need to be merged with the main wezterm keys.
config.keys = {
	{
		-- Save current and window state
		-- See https://github.com/MLFlexer/resurrect.wezterm for options around
		-- saving workspace and window state separately
		key = "S",
		mods = "LEADER|SHIFT",
		action = wezterm.action_callback(function(win, pane) -- luacheck: ignore 212
			local state = resurrect.workspace_state.get_workspace_state()
			resurrect.save_state(state)
			resurrect.window_state.save_window_action()
		end),
	},
	{
		-- Load workspace or window state, using a fuzzy finder
		key = "L",
		mods = "LEADER|SHIFT",
		action = wezterm.action_callback(function(win, pane)
			resurrect.fuzzy_load(win, pane, function(id, label) -- luacheck: ignore 212
				local type = string.match(id, "^([^/]+)") -- match before '/'
				id = string.match(id, "([^/]+)$") -- match after '/'
				id = string.match(id, "(.+)%..+$") -- remove file extension

				-- local opts = {
				-- 	window = win:mux_window(),
				-- 	relative = true,
				-- 	restore_text = true,
				-- 	on_pane_restore = resurrect.tab_state.default_on_pane_restore,
				-- }

				-- From https://github.com/MLFlexer/resurrect.wezterm/issues/70#issuecomment-2618667719
				local opts = {
					close_open_tabs = true, -- **THIS IS THE NEW OPTION**
					window = pane:window(), -- **THIS IS ALSO NEEDED**
					on_pane_restore = resurrect.tab_state.default_on_pane_restore,
					relative = true,
					restore_text = true,
				}

				if type == "workspace" then
					local state = resurrect.load_state(id, "workspace")
					resurrect.workspace_state.restore_workspace(state, opts)
				elseif type == "window" then
					local state = resurrect.load_state(id, "window")
					-- opts.tab = win:active_tab()
					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),
	},
	{
		-- Delete a saved session using a fuzzy finder
		key = "D",
		mods = "LEADER|SHIFT",
		action = wezterm.action_callback(function(win, pane)
			resurrect.fuzzy_load(win, pane, function(id)
				resurrect.delete_state(id)
			end, {
				title = "Delete State",
				description = "Select session to delete and press Enter = accept, Esc = cancel, / = filter",
				fuzzy_description = "Search session to delete: ",
				is_fuzzy = true,
			})
		end),
	},
	-- Rename current session; analagous to command in tmux
	{
		key = "$",
		mods = "LEADER|SHIFT",
		action = act.PromptInputLine({
			description = "Enter new name for session",
			action = wezterm.action_callback(function(window, pane, line)
				if line then
					mux.rename_workspace(window:mux_window():get_workspace(), line)
				end
			end),
		}),
	},
}

return config

@MLFlexer
Copy link
Owner

MLFlexer commented Feb 1, 2025

It should work fine when restoring the windows, but you also mention that your config is workspace based so that might be where I'm missing something. 😄

Can you walk me through how you use it and what you would like/expect as an outcome?

Would you like to resurrect a workspace when you switch to it? If so, then it might be related to this
If this is the case, then I think it would be a good idea to expose a function which does this, as it seems like a use case which multiple users have, so please let me know 😄

@travisjbeck
Copy link

travisjbeck commented Feb 1, 2025

It should work fine when restoring the windows, but you also mention that your config is workspace based so that might be where I'm missing something. 😄

Can you walk me through how you use it and what you would like/expect as an outcome?

Would you like to resurrect a workspace when you switch to it? If so, then it might be related to this If this is the case, then I think it would be a good idea to expose a function which does this, as it seems like a use case which multiple users have, so please let me know

Really appreciate the time your putting in to helping us figure this out.

I say "workspace based" but I could be describing it incorrectly. Here is the scenario:

My workflow is to have 2-3 tabs open with different nvim projects and a tab or so of command lines for those projects. I have a default workspace name defined in wezterm's config. When I go to load the session, I choose the top most option named after my default workspace. Im not really switching workspaces, just want to restore the previous state.

This currently works perfectly except for the first tab's cwd, nvim will be running and even still has it's buffers, but the underlying working directory is always " ~/". All other tabs are exactly how I want them.

I'm only using tabs and not splits or multiple windows.

@PrayagS
Copy link

PrayagS commented Apr 5, 2025

This comment might be helpful. #73 (comment)

The config that I'm using does the following,

  • creates a temporary workspace with the same name as the workspace being restored and switch to it
  • restore the state of the intended workspace in this temporary workspace.

The options close_open_tabs and window fix the issue with dangling window(s) from the starting workspace.

Credits to @strobe for sharing the snippet for creating the temporary workspace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants