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

feat: add tmux support #115

Open
5 of 17 tasks
tummetott opened this issue Dec 14, 2023 · 9 comments
Open
5 of 17 tasks

feat: add tmux support #115

tummetott opened this issue Dec 14, 2023 · 9 comments
Assignees
Labels
enhancement New feature or request tmux

Comments

@tummetott
Copy link

tummetott commented Dec 14, 2023

tmux support

Important

🧪 tmux support is currently experimental
This issue is used to track all tmux related issues or questions. Please comment on this issue instead of created a new issue while tmux support is considered experimental.

The following checklist is planned to be completed before moving these feature to stable.

  • add tests checking env var and kitty_data.tmux
  • add multipane test
  • check positioning of paste window
    • status line impacts this, look into tmux set-option status on tmux set-option status 3 etc . check with tmux show-options status
    • adjust cursor pos for split panes (tmux display-message -p '#{cursor_x} #{cursor_y} #{pane_width} #{pane_height} #{window_visible_layout}')
    • idea, check if zoomed, if not toggle zoom before hand. tmux resize-pane -Z need to figure out command to check if already zoomed ( try #{window_zoomed_flag} )
  • check positioning for scrolled window
  • add command to generate config
  • copy to clipboard, add/modify option to copy to tmux buffer with set-buffer command
  • look into history-limit configuration and document preferred settings
  • doc differences between tmux copy mode, e.g., one pane full screen instead of split panes
  • if get-extent all, -S - -E -, if screen remove -S and -E options. see tmux capture-pane help
  • review all options in capture-pane https://man7.org/linux/man-pages/man1/tmux.1.html
       capture-pane [-aAepPqCJN] [-b buffer-name] [-E end-line] [-S
               start-line] [-t target-pane]
                     (alias: capturep)
               Capture the contents of a pane.  If -p is given, the
               output goes to stdout, otherwise to the buffer specified
               with -b or a new buffer if omitted.  If -a is given, the
               alternate screen is used, and the history is not
               accessible.  If no alternate screen exists, an error will
               be returned unless -q is given.  If -e is given, the
               output includes escape sequences for text and background
               attributes.  -C also escapes non-printable characters as
               octal \xxx.  -T ignores trailing positions that do not
               contain a character.  -N preserves trailing spaces at
               each line's end and -J preserves trailing spaces and
               joins any wrapped lines; -J implies -T.  -P captures only
               any output that the pane has received that is the
               beginning of an as-yet incomplete escape sequence.

               -S and -E specify the starting and ending line numbers,
               zero is the first line of the visible pane and negative
               numbers are lines in the history.  ‘-’ to -S is the start
               of the history and to -E the end of the visible pane.
               The default is to capture only the visible contents of
               the pane.
  • doc required tmux version
  • nice to have? maybe? add tmux nerd font icon to status when in tmux mode
  • doc doesn't work over ssh
  • investigate if tmux_capture_last_command_output or alternatives are a viable option for getting the last command output. or create a follow up issue for this

This issue has been modified. See details on the original issue below.

Original issue below

Hello,
First of all, I wanted to express my admiration for your plugin, which I had the pleasure of discovering after attending your talk at the nvim conference.

While experimenting with your plugin, I encountered a specific issue related to its compatibility with tmux. It appears that the kitty_get_text.extent setting do not seem to function as expected when used in conjunction with tmux. It has come to my attention that this setting, regardless of the value chosen—be it (all, first_cmd_output_on_screen, last_cmd_output, last_non_empty_output, last_visited_cmd_output, selection), —seems to consistently yield the same result. In essence, it consistently displays only the content visible on the screen, akin to the behavior one would expect when setting the setting to screen.

The root cause of this behavior lies in the fact that the kitty @ get-text command consistently retrieves only the content visible on the current screen. Unfortunately, tmux retains all text within its internal buffer, which remains inaccessible to the kitty scrollback history.

I understand that some of these modes, such as last_visited_cmd_output, are inherently dependent on kitty-specific features that tmux lacks. However, I wonder if there might be a way to at least make the full mode compatible with tmux.

I'd like to offer a couple of suggestions that might help in achieving this compatibility:

  • Detecting tmux: You could consider checking if tmux is running, which can be accomplished by checking if the $TMUX environment variable is set. This step would allow the plugin to identify when it is operating within a tmux session.
  • Accessing the buffer content: To access the buffer content in tmux, the following command could be utilized:

tmux capture-pane -p -S - -E -

The -p flag directs the output to stdout.

The -S and -E options specify the starting and ending line numbers for the capture. For instance, '0' represents the first line of the visible pane, and negative numbers refer to lines in the history. The use of '-' with -S denotes the start of the history, and '-' with -E indicates the end of the visible pane.

While I lack the confidence to submit a pull request myself, I sincerely hope that these suggestions prove useful in enhancing the compatibility of your plugin with tmux. I believe that such compatibility would greatly benefit users like me who rely on both tools in their development workflow.

@tummetott
Copy link
Author

However, I'm not sure about how altering the input source from kitty @ get-text to tmux capture-pane -p -S - -E - might impact the handling of ANSI escape sequences. Upon examining your code, I've noticed that you employ sed for filtering out a number of these escape sequences. However, I lack clarity regarding the specific ANSI sequences that tmux generates, as they might differ from those produced by kitty?

@mikesmithgh
Copy link
Owner

@tummetott thanks! It looks like there is an -e flag that will keep the ANSI escape sequences.

tmux capture-pane -e -p -S - -E -

     capture-pane [-aepPqCJN] [-b buffer-name] [-E end-line] [-S start-line] [-t target-pane]
                   (alias: capturep)
             Capture the contents of a pane.  If -p is given, the output goes to stdout, otherwise to the buffer specified with -b or a new buffer if
             omitted.  If -a is given, the alternate screen is used, and the history is not accessible.  If no alternate screen exists, an error will be
             returned unless -q is given.  If -e is given, the output includes escape sequences for text and background attributes.  -C also escapes non-
             printable characters as octal \xxx.  -N preserves trailing spaces at each line's end and -J preserves trailing spaces and joins any wrapped
             lines.  -P captures only any output that the pane has received that is the beginning of an as-yet incomplete escape sequence.

             -S and -E specify the starting and ending line numbers, zero is the first line of the visible pane and negative numbers are lines in the
             history.  ‘-’ to -S is the start of the history and to -E the end of the visible pane.  The default is to capture only the visible contents of
             the pane.

I'd have to look into this a bit, but this seems like a good idea to me 😸.

Since kitty-scrollback.nvim runs in a kitten (separate process) it does not have access to the TMUX environment variable. I played around a bit and may be able to determine if tmux is running in kitty_scrollback_nvim.py with the code:

        print(w.child.foreground_processes)
        fg_process_entrypoints = list(
            map(
                lambda pd: next(
                    iter(pd.get(
                        'cmdline',
                        [],
                    )),
                    None,
                ),
                w.child.foreground_processes,
            ))
        print(fg_process_entrypoints)
        if 'tmux' in fg_process_entrypoints:
            print('perform alternative logic')
        else:
            print('perform normal logic')

output

[{'pid': 83576, 'cmdline': ['tmux'], 'cwd': '/Users/mike'}]
['tmux']
perform alternative logic

if this logic holds, I should be able to use tmux capture-pane instead of kitty @ get-text.

@mikesmithgh mikesmithgh added the enhancement New feature or request label Dec 14, 2023
@mikesmithgh mikesmithgh changed the title Extend full scrollback buffer when using tmux feat: add tmux suport Dec 14, 2023
@mikesmithgh mikesmithgh changed the title feat: add tmux suport feat: add tmux support Dec 16, 2023
@mikesmithgh
Copy link
Owner

Reference: https://github.com/roosta/tmux-fuzzback

@milch milch mentioned this issue Jan 6, 2024
6 tasks
@milch
Copy link

milch commented Jan 6, 2024

I don't remember if GH sends out notifications when an issue is mentioned, but I added a PoC over at #151

@mikesmithgh
Copy link
Owner

        print(w.child.foreground_processes)
        fg_process_entrypoints = list(
            map(
                lambda pd: next(
                    iter(pd.get(
                        'cmdline',
                        [],
                    )),
                    None,
                ),
                w.child.foreground_processes,
            ))
        print(fg_process_entrypoints)
        if 'tmux' in fg_process_entrypoints:
            print('perform alternative logic')
        else:
            print('perform normal logic')

this approach does not work since the TMUX_PANE env variable is needed

@mikesmithgh
Copy link
Owner

mikesmithgh commented Feb 11, 2024

Current plan:

Setup .tmux.conf with a command similar to

bind h run-shell 'kitty @ kitten /Users/mike/gitrepos/kitty-scrollback.nvim/python/kitty_scrollback_nvim.py --env "TMUX=$TMUX" --env "TMUX_PANE=%#{pane_id}"'

kitty-scrollback.nvim logic:

tmux capture-pane -t$TMUX_PANE -e -p -S - -E -
  • else continue with normal flow kitty @ get-text

@mikesmithgh mikesmithgh self-assigned this Feb 11, 2024
@mikesmithgh mikesmithgh linked a pull request Feb 11, 2024 that will close this issue
16 tasks
@mikesmithgh mikesmithgh removed a link to a pull request Feb 19, 2024
16 tasks
@mikesmithgh mikesmithgh mentioned this issue Feb 19, 2024
16 tasks
@mikesmithgh
Copy link
Owner

🎉 I have added experimental tmux support to the latest release 4.1.0 🎉

See the README section tmux (🧪 experimental ) for setup instructions.

I have also added a checklist to this issue to include what I consider needs to be completed to make this feature stable.

If you have any questions or issues please comment on this PR. Thanks!

@tummetott
Copy link
Author

AMAZING. I'll look into this soon. thanks you in advance

@mikesmithgh
Copy link
Owner

🎉 I have added cursor position fixes to take into account the tmux status line and added some basic test coverage in the latest release 4.2.1 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request tmux
Projects
None yet
Development

No branches or pull requests

3 participants