wez / wezterm

A GPU-accelerated cross-platform terminal emulator and multiplexer written by @wez and implemented in Rust
https://wezfurlong.org/wezterm/
Other
16.78k stars 751 forks source link

pane:get_foreground_process_name() don't return actual foreground process #1898

Closed kuntau closed 2 years ago

kuntau commented 2 years ago

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

macOS

WezTerm version

wezterm 20220419-232436-924b63eb

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

I had this issue for couple of month actually and I'm not sure how to debug it. Wezterm just detect my shell as foreground process as shown in log /usr/local/bin/zsh or /usr/local/bin/nu even I have vim, htop, ping or any other process running.

I tried setting default_prog to different shell to no avail

To Reproduce

I'm not sure to how reproduce it but here's my full config

Nothing out of ordinary config

Configuration

local is_vim = function(pane)
  local proc = basename(pane:get_foreground_process_name())
  wezterm.log_info('@is_vim, proc='..proc)
  return proc ==  'nvim' or proc == 'vim'
end

    { key='h', mods="CTRL", action=wezterm.action_callback(function(win, pane)
      if is_vim(pane) then
        win:perform_action({ SendKey = { key = 'h', mods = 'CTRL' } }, pane)
      else
        win:perform_action({ ActivatePaneDirection = 'Left' }, pane)
      end
    end) },

Expected Behavior

Wezterm should detect vim/nvim is the foreground process

Logs

This log is the output when I actually have other process running on top of shell

1:49:46.703  INFO   config::lua                            > lua: @is_vim, proc=nu
01:50:13.196  WARN   wezterm_term::terminalstate            > unhandled XtermKeyMode OtherKeys Some(0)
01:50:16.849  WARN   mux::localpane                         > unknown DeviceControlMode::Enter EnterDeviceControlMode(params: [], intermediates: [], byte: 'z' 0x7a, ignored_extra_intermediates=false)
01:50:16.849  WARN   mux::localpane                         > unhandled DeviceControlMode::Data 7a z
01:50:16.849  WARN   wezterm_term::terminalstate::performer > unknown unspecified CSI: "0%m"
01:50:17.597  INFO   config::lua                            > lua: @is_vim, pane=/usr/local/Cellar/nushell/0.61.0/bin/nu
01:50:17.598  INFO   config::lua                            > lua: @is_vim, proc=nu
01:50:30.672  INFO   config::lua                            > lua: @is_vim, pane=/usr/local/Cellar/nushell/0.61.0/bin/nu
01:50:30.672  INFO   config::lua                            > lua: @is_vim, proc=nu
01:52:57.219  INFO   config::lua                            > lua: @is_vim, pane=/usr/local/Cellar/nushell/0.61.0/bin/nu
...
01:27:03.827  INFO   config::lua                            > lua: @is_vim, proc=zsh
01:27:09.208  WARN   wezterm_term::terminalstate            > unhandled XtermKeyMode OtherKeys Some(0)
01:27:14.367  INFO   config::lua                            > lua: @is_vim, pane=/bin/zsh
01:27:14.367  INFO   config::lua                            > lua: @is_vim, proc=zsh
01:27:15.728  INFO   config::lua                            > lua: @is_vim, pane=/bin/zsh
01:27:15.728  INFO   config::lua                            > lua: @is_vim, proc=zsh
01:27:27.178  WARN   wezterm_term::terminalstate            > unhandled XtermKeyMode OtherKeys Some(2)
01:27:28.230  INFO   config::lua                            > lua: @is_vim, pane=/bin/zsh
01:27:28.230  INFO   config::lua                            > lua: @is_vim, proc=zsh
01:27:29.082  INFO   config::lua                            > lua: @is_vim, pane=/bin/zsh
01:27:29.082  INFO   config::lua                            > lua: @is_vim, proc=zsh
01:38:13.302  ERROR  luahelper::serde_lua                   > Ignoring unknown field `initial_column` in struct of type `Config`. Did you mean one of `initial_cols`, `initial_rows`? Other possible fields are `adjust_window_size_when_changing_font_size`, `allow_download_protocols`, `allow_square_glyphs_to_overflow_width`, `allow_win32_input_mode`, `alternate_buffer_wheel_scroll_speed`, `animation_fps`, `anti_alias_custom_block_glyphs`, `audible_bell`, `automatically_reload_config`, `bidi_direction`, `bidi_enabled`, `bold_brightens_ansi_colors`, `bypass_mouse_reporting_modifiers`, `canonicalize_pasted_newlines`, `check_for_updates`, `check_for_updates_interval_seconds`, `clean_exit_codes`, `color_scheme`, `color_scheme_dirs`, `color_schemes`, `colors`, `cursor_blink_ease_in`, `cursor_blink_ease_out`, `cursor_blink_rate`, `custom_block_glyphs`, `daemon_options`, `debug_key_events`, `default_cursor_style`, `default_cwd`, `default_domain`, `default_gui_startup_args`, `default_prog`, `default_workspace`, `disable_default_key_bindings`, `disable_default_mouse_bindings`, `disable_default_quick_select_patterns`, `dpi`, `enable_csi_u_key_encoding`, `enable_kitty_graphics`, `enable_scroll_bar`, `enable_tab_bar`, `enable_wayland`, `enq_answerback`, `exit_behavior`, `experimental_pixel_positioning`, `font`, `font_dirs`, `font_locator`, `font_rasterizer`, `font_rules`, `font_shaper`, `font_size`, `force_reverse_video_cursor`, `foreground_text_hsb`, `freetype_interpreter_version`, `freetype_load_flags`, `freetype_load_target`, `freetype_render_target`, `front_end`, `harfbuzz_features`, `hide_tab_bar_if_only_one_tab`, `hyperlink_rules`, `inactive_pane_hsb`, `key_map_preference`, `key_tables`, `keys`, `launch_menu`, `leader`, `line_height`, `max_fps`, `mouse_bindings`, `mux_env_remove`, `mux_output_parser_buffer_size`, `native_macos_fullscreen_mode`, `pane_focus_follows_mouse`, `periodic_stat_logging`, `prefer_egl`, `quick_select_alphabet`, `quick_select_patterns`, `ratelimit_mux_line_prefetches_per_second`, `scroll_to_bottom_on_input`, `scrollback_lines`, `search_font_dirs_for_fallback`, `selection_word_boundary`, `send_composed_key_when_left_alt_is_pressed`, `send_composed_key_when_right_alt_is_pressed`, `set_environment_variables`, `show_tab_index_in_tab_bar`, `show_update_window`, `skip_close_confirmation_for_processes_named`, `sort_fallback_fonts_by_coverage`, `ssh_backend`, `ssh_domains`, `status_update_interval`, `swallow_mouse_click_on_pane_focus`, `swallow_mouse_click_on_window_focus`, `swap_backspace_and_delete`, `tab_and_split_indices_are_zero_based`, `tab_bar_at_bottom`, `tab_bar_style`, `tab_max_width`, `term`, `text_background_opacity`, `text_blink_ease_in`, `text_blink_ease_out`, `text_blink_rapid_ease_in`, `text_blink_rapid_ease_out`, `text_blink_rate`, `text_blink_rate_rapid`, `tls_clients`, `tls_servers`, `treat_east_asian_ambiguous_width_as_wide`, `treat_left_ctrlalt_as_altgr`, `unicode_version`, `unix_domains`, `unzoom_on_switch_pane`, `use_cap_height_to_scale_fallback_fonts`, `use_dead_keys`, `use_fancy_tab_bar`, `use_ime`, `use_resize_increments`, `visual_bell`, `warn_about_missing_glyphs`, `window_background_gradient`, `window_background_image`, `window_background_image_hsb`, `window_background_opacity`, `window_close_confirmation`, `window_decorations`, `window_frame`, `window_padding`, `wsl_domains`, `xcursor_size`, `xcursor_theme`, `xim_im_name`.
01:38:13.303  ERROR  luahelper::serde_lua                   > Ignoring unknown field `initial_row` in struct of type `Config`. Did you mean one of `initial_rows`, `initial_cols`? Other possible fields are `adjust_window_size_when_changing_font_size`, `allow_download_protocols`, `allow_square_glyphs_to_overflow_width`, `allow_win32_input_mode`, `alternate_buffer_wheel_scroll_speed`, `animation_fps`, `anti_alias_custom_block_glyphs`, `audible_bell`, `automatically_reload_config`, `bidi_direction`, `bidi_enabled`, `bold_brightens_ansi_colors`, `bypass_mouse_reporting_modifiers`, `canonicalize_pasted_newlines`, `check_for_updates`, `check_for_updates_interval_seconds`, `clean_exit_codes`, `color_scheme`, `color_scheme_dirs`, `color_schemes`, `colors`, `cursor_blink_ease_in`, `cursor_blink_ease_out`, `cursor_blink_rate`, `custom_block_glyphs`, `daemon_options`, `debug_key_events`, `default_cursor_style`, `default_cwd`, `default_domain`, `default_gui_startup_args`, `default_prog`, `default_workspace`, `disable_default_key_bindings`, `disable_default_mouse_bindings`, `disable_default_quick_select_patterns`, `dpi`, `enable_csi_u_key_encoding`, `enable_kitty_graphics`, `enable_scroll_bar`, `enable_tab_bar`, `enable_wayland`, `enq_answerback`, `exit_behavior`, `experimental_pixel_positioning`, `font`, `font_dirs`, `font_locator`, `font_rasterizer`, `font_rules`, `font_shaper`, `font_size`, `force_reverse_video_cursor`, `foreground_text_hsb`, `freetype_interpreter_version`, `freetype_load_flags`, `freetype_load_target`, `freetype_render_target`, `front_end`, `harfbuzz_features`, `hide_tab_bar_if_only_one_tab`, `hyperlink_rules`, `inactive_pane_hsb`, `key_map_preference`, `key_tables`, `keys`, `launch_menu`, `leader`, `line_height`, `max_fps`, `mouse_bindings`, `mux_env_remove`, `mux_output_parser_buffer_size`, `native_macos_fullscreen_mode`, `pane_focus_follows_mouse`, `periodic_stat_logging`, `prefer_egl`, `quick_select_alphabet`, `quick_select_patterns`, `ratelimit_mux_line_prefetches_per_second`, `scroll_to_bottom_on_input`, `scrollback_lines`, `search_font_dirs_for_fallback`, `selection_word_boundary`, `send_composed_key_when_left_alt_is_pressed`, `send_composed_key_when_right_alt_is_pressed`, `set_environment_variables`, `show_tab_index_in_tab_bar`, `show_update_window`, `skip_close_confirmation_for_processes_named`, `sort_fallback_fonts_by_coverage`, `ssh_backend`, `ssh_domains`, `status_update_interval`, `swallow_mouse_click_on_pane_focus`, `swallow_mouse_click_on_window_focus`, `swap_backspace_and_delete`, `tab_and_split_indices_are_zero_based`, `tab_bar_at_bottom`, `tab_bar_style`, `tab_max_width`, `term`, `text_background_opacity`, `text_blink_ease_in`, `text_blink_ease_out`, `text_blink_rapid_ease_in`, `text_blink_rapid_ease_out`, `text_blink_rate`, `text_blink_rate_rapid`, `tls_clients`, `tls_servers`, `treat_east_asian_ambiguous_width_as_wide`, `treat_left_ctrlalt_as_altgr`, `unicode_version`, `unix_domains`, `unzoom_on_switch_pane`, `use_cap_height_to_scale_fallback_fonts`, `use_dead_keys`, `use_fancy_tab_bar`, `use_ime`, `use_resize_increments`, `visual_bell`, `warn_about_missing_glyphs`, `window_background_gradient`, `window_background_image`, `window_background_image_hsb`, `window_background_opacity`, `window_close_confirmation`, `window_decorations`, `window_frame`, `window_padding`, `wsl_domains`, `xcursor_size`, `xcursor_theme`, `xim_im_name`.

Anything else?

No response

wez commented 2 years ago

I can't reproduce this. I wonder if this might be something weird with nushell?

wez commented 2 years ago

Can you try to zero in on a repro scenario?

kuntau commented 2 years ago

I can't reproduce this. I wonder if this might be something weird with nushell?

This issue happened both with zsh & nutshell.

Nutshell have default configuration.

What do you mean by zero in reproduction scenario?

Thank you

wez commented 2 years ago

What are the minimum number of steps to deterministically make the problem happen?

This is the config I tried; this shows the process in the right status (which only updates ~1 every second), and redefines the key assignment to log the process name. Even quickly starting and exiting vim had it producing the correct results.

I made it run bash without running any rc files in order to isolate it from any config that might be in my shell rcfiles.

local wezterm = require 'wezterm'

function basename(s)
  return string.gsub(s, "(.*[/\\])(.*)", "%2")
end

wezterm.on("update-right-status", function(window, pane)
  window:set_right_status(basename(pane:get_foreground_process_name()))
end)

return {
  default_prog = {"bash", "--norc"},
  keys = {
    { key='h', mods="CTRL", action=wezterm.action_callback(function(win, pane)
      wezterm.log_info("is_vim", pane:get_foreground_process_name())
    end) },
  }
}
kuntau commented 2 years ago

default_prog = {"bash", "--norc"},

Weird, for some reason whenever use bash as default shell, wezterm can correctly show foreground process.

Have you tried from zsh or nu?

Thank you

edit:

Launching zsh with -f flag fixed the issue, so must be my configs somewhere, but nu with no config still can't detect foreground process.

Feel free to close this issue.

Thank you

rosshadden commented 2 years ago

@kuntau I've been trying to figure this out for months. Have you had any luck? I opened this issue with nushell: https://github.com/nushell/nushell/issues/5050 Nushell people tell me it's a terminal issue, but I've debugged this with help from @wez himself over IRC and we think it's a nushell issue. I have tried making changes in both, and compiling from source. But nothing I have tried has had any success and it's easily the most frustrating part of my workflow right now.

One thing I wanted to do is exactly what you are trying to do above, make mappings that navigate vim panes if in vim, otherwise wez panes. But another much more frustrating side effect of this problem or maybe some other problem is that opening new panes does not open in the current pane directory if you start wezterm with wezterm start, which is how I open all of my wezterm instances.

github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.