danielcopper / wezterm-session-manager

MIT License
76 stars 16 forks source link

Trouble setting up script #4

Open levinotik opened 6 months ago

levinotik commented 6 months ago

Thanks for this much needed script. I'm having some trouble getting it set up. I cloned this project into ~/.config/wezterm and modified my wezterm.lua to add:

local session_manager = require('wezterm-session-manager')

.....

wezterm.on('save_session', function(window) session_manager.save_state(window) end)
wezterm.on('load_session', function(window) session_manager.load_state(window) end)
wezterm.on('restore_session', function(window) session_manager.restore_state(window) end)

in my config.keys = {...}, I've added:

{ key = 'S', mods = 'LEADER', action = wezterm.action { EmitEvent = 'save_session' } },
{ key = 'L', mods = 'LEADER', action = wezterm.action { EmitEvent = 'load_session' } },
{ key = 'R', mods = 'LEADER', action = wezterm.action { EmitEvent = 'restore_session' } },

After saving and trying with this key bindings, nothing seems to happen. Is there supposed to be some confirmation or visual clue that the session was successfully saved? I thought there might not be, so just to be sure in case the session did save, I tried restoring, but that didn't seem to do anything either.

Appreciate any guidance or assistance you can provide. Thanks again!


My full config is below for reference:

local wezterm = require 'wezterm'
local session_manager = require('wezterm-session-manager')

local config = wezterm.config_builder()
local act = wezterm.action

wezterm.on('save_session', function(window) session_manager.save_state(window) end)
wezterm.on('load_session', function(window) session_manager.load_state(window) end)
wezterm.on('restore_session', function(window) session_manager.restore_state(window) end)

wezterm.on('update-right-status', function(window, pane)
    window:set_right_status(window:active_workspace())
end)
config.default_cursor_style = 'SteadyBlock'
config.leader = { key = 'b', mods = 'CTRL', timeout_milliseconds = 1000 }
config.keys = {
    {
        key = 'LeftArrow',
        mods = 'CTRL|SHIFT',
        action = act.ActivatePaneDirection 'Left',
    },
    {
        key = 'RightArrow',
        mods = 'CTRL|SHIFT',
        action = act.ActivatePaneDirection 'Right',
    },
    {
        key = 'UpArrow',
        mods = 'CTRL|SHIFT',
        action = act.ActivatePaneDirection 'Up',
    },
    {
        key = 'DownArrow',
        mods = 'CTRL|SHIFT',
        action = act.ActivatePaneDirection 'Down',
    },
    {
        key = '|',
        mods = 'LEADER|SHIFT',
        action = wezterm.action.SplitHorizontal { domain = 'CurrentPaneDomain' },
    },
    {
        key = '-',
        mods = 'LEADER',
        action = wezterm.action.SplitVertical { domain = 'CurrentPaneDomain' },
    },
    {
        key = 'a',
        mods = 'LEADER|CTRL',
        action = wezterm.action.SendKey { key = 'a', mods = 'CTRL' },
    },
    { key = 'l', mods = 'ALT',    action = wezterm.action.ShowLauncher },
    { key = 's', mods = 'LEADER', action = wezterm.action.ShowLauncherArgs { flags = 'FUZZY|WORKSPACES' } },
    { key = 't', mods = 'LEADER', action = wezterm.action.ShowLauncherArgs { flags = 'FUZZY|TABS' } },
    {
        key = 'w',
        mods = 'LEADER',
        action = act.PromptInputLine {
            description = wezterm.format {
                { Attribute = { Intensity = 'Bold' } },
                { Foreground = { AnsiColor = 'Fuchsia' } },
                { Text = 'Enter name for new workspace' },
            },
            action = wezterm.action_callback(function(window, pane, line)
                if line then
                    window:perform_action(
                        act.SwitchToWorkspace {
                            name = line,
                        },
                        pane
                    )
                end
            end),
        },
    },

    {
        key = 'w',
        mods = 'CMD',
        action = wezterm.action.CloseCurrentPane { confirm = true },
    },
    {
        key = 'z',
        mods = 'LEADER',
        action = wezterm.action.TogglePaneZoomState,
    },
    {
        key = 'e',
        mods = 'LEADER',
        action = act.PromptInputLine {
            description = 'Enter new name for tab',
            action = wezterm.action_callback(function(window, pane, line)
                if line then
                    window:active_tab():set_title(line)
                end
            end),
        },
    },
    { key = 'S', mods = 'LEADER', action = wezterm.action { EmitEvent = 'save_session' } },
    { key = 'L', mods = 'LEADER', action = wezterm.action { EmitEvent = 'load_session' } },
    { key = 'R', mods = 'LEADER', action = wezterm.action { EmitEvent = 'restore_session' } },
}

config.color_scheme = 'Ayu Dark (Gogh)'

config.colors = {
    selection_fg = 'none',
    selection_bg = 'rgba(50% 50% 50% 50%)',
}
config.window_background_opacity = 0.72
config.font_size = 22
config.font = wezterm.font('JetBrainsMono Nerd Font Mono') --, {weight = 'Medium'})
config.window_decorations = 'RESIZE'
return config
levinotik commented 6 months ago

I see the issue now. wezterm is looking for the file in one of several locations:

runtime error: [string "/Users/me/.config/wezterm/wezterm.lua"]:2:
module 'wezterm-session-manager' not found:
        no field package.preload['wezterm-session-manager']
        no file '/Users/me/.config/wezterm/wezterm-session-manager.lua'
        no file '/Users/me/.config/wezterm/wezterm-session-manager/init.lua'
        no file '/Users/me/Library/Application Support/wezterm/plugins/wezterm-
session-manager/plugin/init.lua'
        no file '/Users/me/.wezterm/wezterm-session-manager.lua'
        no file '/Users/me/.wezterm/wezterm-session-manager/init.lua'
        no file '/usr/local/share/lua/5.4/wezterm-session-manager.lua'
        no file '/usr/local/share/lua/5.4/wezterm-session-manager/init.lua'
        no file '/usr/local/lib/lua/5.4/wezterm-session-manager.lua'
        no file '/usr/local/lib/lua/5.4/wezterm-session-manager/init.lua'
        no file './wezterm-session-manager.lua'
        no file './wezterm-session-manager/init.lua'

        can't load C modules in safe mode
stack traceback:
        [C]: in function 'require'
        [string "/Users/me/.config/wezterm/wezterm.lua"]:2: in main chunk

I renamed wezterm-session-manager.lua to init.lua and it worked. I believe your instructions have the file ending up in ~/.config/wezterm/wezterm-session-manager/session-manager.lua. The file would need to be one level up and renamed to wezterm-session-manager.lua for that to work or it can remane in the wezterm-session-manager directory and renamed to init.lua as I've done.

levinotik commented 6 months ago

Another update: the session seems to be saved. I can see it in wezterm_state_config.json but I can't restore it. The restore session key binding just doesn't seem to do anything.

danielcopper commented 6 months ago

Hi. I won't have time for the next couple of days to really check the issue. But I will get to it. In the meantime feel free to check out my dotfiles. https://github.com/danielcopper/dotfiles/blob/arch/.config/wezterm/wezterm.lua

This is the wezterm config where it does work for me on Linux.

Broderick-Westrope commented 6 months ago

@levinotik I just had the same issue (file not found) as you. Renaming to init.lua fixed this. Unsure about your "restore" issue though. My restore is working fine. Did you figure it out in the end?

danielcopper commented 6 months ago

Hi again,

i finally had time to look into it. I noticed that i actually have set up the session manager differently than what i described in the readme. The require statement should actually look like this:

local session_manager = require("wezterm-session-manager/session-manager)

I will update the readme and close the PR.

Concerning your other question about some visual feedback. i just used your config, adjusted the path for the session manager and i get a visual feedback for save and restore.

image

Let me know if everything works now.

Cheers

levinotik commented 6 months ago

Ah thanks @danielcopper

@Broderick-Westrope I haven't tried to fix this again. I'm going to look at it again soon and hopefully resolve.

levinotik commented 6 months ago

I updated my wezterm config to reference:

local session_manager = require('wezterm-session-manager/session_manager')

Still seem to have issues. When I load up wezterm, I see the following error:

runtime error: [string "/Users/me/.config/wezterm/wezterm.lua"]:2:
module 'wezterm-session-manager/session_manager' not found:
        no field package.preload['wezterm-session-manager/session_manager']
        no file '/Users/me/.config/wezterm/wezterm-session-manager/
session_manager.lua'
        no file '/Users/me/.config/wezterm/wezterm-session-manager/
session_manager/init.lua'
        no file '/Users/me/Library/Application Support/wezterm/plugins/wezterm-
session-manager/session_manager/plugin/init.lua'
        no file '/Users/me/.wezterm/wezterm-session-manager/
session_manager.lua'
        no file '/Users/me/.wezterm/wezterm-session-manager/session_manager/
init.lua'
        no file '/usr/local/share/lua/5.4/wezterm-session-manager/session_manager.lua'
        no file '/usr/local/share/lua/5.4/wezterm-session-manager/session_manager/
init.lua'
        no file '/usr/local/lib/lua/5.4/wezterm-session-manager/session_manager.lua'
        no file '/usr/local/lib/lua/5.4/wezterm-session-manager/session_manager/
init.lua'
        no file './wezterm-session-manager/session_manager.lua'
        no file './wezterm-session-manager/session_manager/init.lua'

        can't load C modules in safe mode
stack traceback:
        [C]: in function 'require'
        [string "/Users/me/.config/wezterm/wezterm.lua"]:2: in main chunk

to be 100% clear, here's what my config directory looks like now:

/Users/me/.config/wezterm
❯ tree
.
├── wezterm-session-manager
│   ├── LICENSE
│   ├── README.md
│   ├── session-manager.lua
│   └── wezterm_state_config.json
└── wezterm.lua

2 directories, 5 files
~/.config/wezterm
❯
danielcopper commented 6 months ago

Change your require statement to

local session_manager = require("wezterm-session-manager/session-manager")

instead of local session_manager = require("wezterm-session-manager/session_manager")

levinotik commented 6 months ago

Doh! Thanks. That does get rid of errors, but still no indicator that saving the session succeeded (though I can see the json file) and restoring doesn't seem to work at all.

levinotik commented 6 months ago

Oh it looks like it's creating a state file for each workspace. I tested by creating a new workspace "foo" and that got saved as wezterm-session-manager/wezterm_state_foo.json. Does that get referenced from another state file or is the design that only a single workspace can be restored?

levinotik commented 6 months ago

From the README:

restore Session Reopens a previously saved session that matches the current workspace name, restoring its layout and directories.

It sounds like I'd have to manually recreate workspaces with matching names and then restore within those? That could work, but it does mean this is a lot less useful than it could be. Is there some limitation within wezterm that makes restoring all workspaces with all layouts not possible?

danielcopper commented 6 months ago

You are right, this is currently how you would have to restore sessions that are not the default one. I am in the process of adding the load workspace option. That should allow to pick which saved session to restore.

levinotik commented 6 months ago

Sounds great @danielcopper please let us know if there's anything we could do to help. Happy to take on anything even if administrivia or testing.

danielcopper commented 6 months ago

I just opened up the discussions page. Feel free to use it for requests and ideas. I might also open a connected project page, not sure yet.

danielcopper commented 6 months ago

@levinotik Please test the notifications again, so we can close this issue. maybe give it a try with a minimal setup or use mine as a template.