hoaproject / Console

The Hoa\Console library.
https://hoa-project.net/
366 stars 32 forks source link

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

Closed Hywan closed 9 years ago

Hywan commented 9 years ago

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.

Hywan commented 9 years ago

@jubianchi Fixed.

jubianchi commented 9 years ago

@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 commented 9 years ago

@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 commented 9 years ago

@Hywan this was just an idea :)

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

jubianchi commented 9 years ago

@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 commented 9 years ago

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

Hywan commented 9 years ago

@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 commented 9 years ago

@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 commented 9 years ago

@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 commented 9 years ago

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

Great feature :+1:

Hywan commented 9 years ago

Fiou, I was afraid!

Jir4 commented 9 years ago

Maybe we have to notified this somewhere

jubianchi commented 9 years ago

@Jir4 +1 : this should be documented somewhere

Hywan commented 9 years ago

Please, open an issue for that :-).

dmnc-0 commented 8 years ago

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

dmnc-0 commented 8 years ago

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

Hywan commented 8 years ago

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