Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Add TMUX(1) support for Window::copy #53

Merged
merged 2 commits into from
Sep 30, 2015
Merged

Conversation

Hywan
Copy link
Member

@Hywan Hywan commented Sep 30, 2015

Fix #52.

Two steps (one more is required to get something clean, in another issue):

  1. Add the Console::isTmuxRunning method,
  2. Add TMUX(1) support on Window::copy.

To check whether TMUX(1) is running, we use the following Shell command (see Parameter expansion):

echo ${TMUX:-"no"}

I first used ${TMUX?} but it prints an error on stderr if the variable is not defined. This “trick” is still clean though.

To add TMUX(1) support on Window::copy, we use information on this URL: http://permalink.gmane.org/gmane.comp.terminal-emulators.tmux.user/1324. We learn there is a special form to by-pass TMUX(1) and send control sequences to the upper terminal. We have to use the:

\033Ptmux;…\033\\

control sequence where is the original control sequence where \033 are doubled.

This PR is ready to be reviewed.

The next step is to have a unified way to write on the output (so far we only do echo and it sucks sometimes, e.g. with Window::copy; it is only more difficult to embed, /cc @jubianchi). I am opening a new issue right now.

I am assigning @jubianchi for the review.

return $_out;
}

return $_out = 'no' !== Processus::execute('echo ${TMUX:-"no"}', false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doing a shell-out here instead of something like:

$tmux = getenv('TMUX');

return $tmux !== '' && $tmux !== false;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree… Maybe isset($_SERVER['TMUX']) is even better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sometimes this is useful to know whether we are running behind TMUX(1)
or not. For instance, some control sequences are not supported by
TMUX(1) yet and we have to apply some specific strategies. Because
TMUX(1) is very popular, we allow ourselves such an approach.
Refering to
http://permalink.gmane.org/gmane.comp.terminal-emulators.tmux.user/1324,
we learn there is a special form to by-pass TMUX(1) and send control
sequences to the upper terminal. We have to use the:

    \033Ptmux;…\033\\

control sequence where `…` is the original control sequence where `\033`
are doubled.
@Hywan
Copy link
Member Author

Hywan commented Sep 30, 2015

@jubianchi Fixed.

@Bhoat Bhoat merged commit a13a9a2 into hoaproject:master Sep 30, 2015
@Hywan Hywan removed the in progress label Sep 30, 2015
@jubianchi
Copy link
Member

@Hywan perhaps this is out of scope but let ask it anyway :)

Wouldn't it make sense to also do the same for screen ?

It seems we can check if we are in a screen session by checking for $STY. We would then avec Console:: isTmuxRunning and Console::isScreenRunning.

@Hywan
Copy link
Member Author

Hywan commented Sep 30, 2015

@jubianchi Maybe yes. I have to test everything on SCREEN(1) too. This is hard to do testing here :-/. With #54, this is going to be much better: For a specific tmux file, we will be able to test all the cursor, window etc. APIs, but I don't know how to test with TMUX(1). Yes we can still emulate TMUX(1) and re-run all the test suites, but we are not sure this is how TMUX(1) will work in practise.

@jubianchi
Copy link
Member

@Hywan this was just an idea :)

Perhaps we should just keep this comment as a reminder ;)

@jubianchi
Copy link
Member

@Hywan I can't get this work even on iTerm2 (2.1.1) within tmux or simply running bash (GNU bash, version 3.2.57(1)-release and GNU bash, version 4.3.42(1)-release) outside of tmux

@Hywan
Copy link
Member Author

Hywan commented Sep 30, 2015

@jubianchi Please, open an issue as a reminder for screen :-).

@Hywan
Copy link
Member Author

Hywan commented Oct 1, 2015

@jubianchi So first, the shell is not important. This is the responsibility of the terminal, so, what is the result of:

$ hoa console:termcap -t

Personally, I have xterm-256color.
Then, I also have iTerm2 (2.1.1) and tmux. Here is what we are going to try:

Outside tmux Inside tmux
Console\Console::isTmuxRunning()
Console\Window::copy('foo' . uniqid())

Here are the results for me:

Outside tmux Inside tmux
Console\Console::isTmuxRunning() bool(false) bool(true)
Console\Window::copy('foo' . uniqid()) $ pbpaste # foo60cd3c6159c3 $ pbpaste # foo560cd3c584044

Same results for both Bash and Zsh (normally it does not interfer).

What are yours?

@jubianchi
Copy link
Member

@Hywan my results outside of tmux:

$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)
Copyright (C) 2007 Free Software Foundation, Inc.

$ vendor/bin/hoa console:termcap -t

$ export TERM=xterm-256color
$ vendor/bin/hoa console:termcap -t
xterm-256color

$ cat index.php
<?php

require_once __DIR__ . '/vendor/autoload.php';

var_dump(\Hoa\Console\Console::isTmuxRunning());

$ php index.php
bool(false)

$ cat index2.php
<?php

require_once __DIR__ . '/vendor/autoload.php';

Hoa\Console\Window::copy('foo' . uniqid());

$ php index2.php
$ pbpaste
previously copied string

@Hywan
Copy link
Member Author

Hywan commented Oct 1, 2015

@jubianchi In iTerm2 preferences, in the General panel, do you have “Allow clipboard access to terminal apps” checked in the Selection section?
screen shot 2015-10-01 at 11 55 10

@jubianchi
Copy link
Member

@Hywan my bad! The checkbox was not checked: everything is ok now!

Great feature 👍

@Hywan
Copy link
Member Author

Hywan commented Oct 1, 2015

Fiou, I was afraid!

@Jir4
Copy link

Jir4 commented Oct 1, 2015

Maybe we have to notified this somewhere

@jubianchi
Copy link
Member

@Jir4 +1 : this should be documented somewhere

@Hywan
Copy link
Member Author

Hywan commented Oct 2, 2015

Please, open an issue for that :-).

@dmnc-0
Copy link

dmnc-0 commented Oct 5, 2015

Won't the shell command used to test $TMUX be shell dependent though? The ${:-} stuff isn't universally supported afaik.

@dmnc-0
Copy link

dmnc-0 commented Oct 5, 2015

[ -z "$TMUX" ] would check if $TMUX is zero-length (empty or undefined) without requiring from the shell that ${:-} is understood.

@Hywan
Copy link
Member Author

Hywan commented Oct 5, 2015

Does no longer matter since we use the _SERVER PHP variable. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

5 participants