wezterm icon indicating copy to clipboard operation
wezterm copied to clipboard

get_current_working_dir returns nil when sourcing wezterm.sh at the end of .zshrc

Open marchyman opened this issue 1 month ago • 5 comments

What Operating System(s) are you seeing this problem on?

macOS

Which Wayland compositor or X11 Window manager(s) are you using?

No response

WezTerm version

wezterm 20240520-135708-b8f94c47

Did you try the latest nightly build to see if the issue is better (or worse!) than your current version?

Yes, and I updated the version box above to show the version of the nightly that I tried

Describe the bug

When using zsh AND sourcing wezterm.sh the function get_current_working_dir(), called from wezterm_on("update-status") runs 5 times after the last keystroke. The last time it runs it returns nil.

That behavior does not occur before sourcing wezterm.sh.

To Reproduce

Open a new window with the above test config using wezterm --config-file ~/.config/wezterm/test.lua start & .zshrc should NOT source wezterm.sh (which I renamed .wezterm.sh) The status bar is correct.

Screenshot 2024-05-28 at 11 54 43 AM

At the prompt enter source .wezterm.sh and hit return. The status bar is no longer correct.

Screenshot 2024-05-28 at 11 55 17 AM

The log output shows that "update-status" is triggered every keystroke. I didn't expect that. On the last keystroke (the key I see this:

12:46:12.594  INFO   logging > lua: marc
12:46:12.600  INFO   logging > lua: marc
12:46:12.604  INFO   logging > lua: marc
12:46:12.610  INFO   logging > lua: marc
12:46:12.618  INFO   logging > lua:

The request for cwd occurs 5 times, the last time returning nothing. Before .wezterm.sh was sourced multiple invocations of update-status did not occur when the return key is hit.

Configuration

local wezterm = require("wezterm")

local config = {}
-- Use config builder object if possible
if wezterm.config_builder then config = wezterm.config_builder() end

config.initial_cols = 60
config.initial_rows = 20

config.use_fancy_tab_bar = false
config.status_update_interval = 10000 -- long update period
config.tab_bar_at_bottom = false
wezterm.on("update-status", function(window, pane)

  local basename = function(s)
    -- Nothing a little regex can't fix
    return string.gsub(s, "(.*[/\\])(.*)", "%2")
  end

  local cwd = pane:get_current_working_dir()
  cwd = cwd and basename(cwd.file_path) or ""
  wezterm.log_info(cwd)
  local cmd = pane:get_foreground_process_name()
  cmd = cmd and basename(cmd) or ""

  window:set_right_status(wezterm.format({
    { Text = wezterm.nerdfonts.md_folder .. "  " .. cwd },
    { Text = " | " },
    { Foreground = { Color = "#e0af68" } },
    { Text = wezterm.nerdfonts.fa_code .. "  " .. cmd .. " " },
    "ResetAttributes",
  }))

end)

return config

Expected Behavior

wezterm should return the cwd when possible.

Logs

No response

Anything else?

No response

marchyman avatar May 28 '24 20:05 marchyman