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
Added Tmux Selector #1897
base: develop
Are you sure you want to change the base?
Added Tmux Selector #1897
Conversation
powerline/selectors/tmux.py
Outdated
def tmux_via_ssh(pl, segment_info, mode, caching=True, default=False): | ||
'''Returns True if tmux client is connected via SSH | ||
''' | ||
pid = get_tmux_output(pl, "display", "-p", "#{client_pid}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are not passing environment anywhere, this is not going to work right. The whole bindings file itself was never intended to work in daemon though, so currently you can’t pass environment there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way of doing it?
For me this worked okay-ish (it got the display -p #{client_pid}
output for the current active tmux window so that it would not show excluded segments when I was connected via SSH to the machine and working actively on it), also when using the powerline-daemon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need another tmux instance not run from ssh for that to not be OK. This does not mean that variant is acceptable, you would need to refactor relevant code.
powerline/selectors/tmux.py
Outdated
if pid in _cached_clients: | ||
return _cached_clients[pid] | ||
try: | ||
pstree_out = subprocess.check_output(("pstree", "-s", pid)).decode() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use subprocess
directly, and do not use check_output
at all as it is not present in all supported Python versions. There is run_cmd
in powerline.lib.shell
for this purpose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually don’t use pstree
, as well as any regular expressions. psutil.Process
class provides everything needed, it works in-process, it should be more portable and it would not query all the parents, just up to the one that happens to match sshd
. Just need to mention requirements in the documentation. pstree
is the requirement as well though.
powerline/selectors/tmux.py
Outdated
_cached_clients = {} | ||
# This Regex parses pstree -s <Process_ID> information | ||
# It will match, if any process name is sshd | ||
SSHD_REGEX = re.compile(r"(?:^|\W)sshd(?:^|\W)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the strings in powerline use single quotes.
powerline/selectors/tmux.py
Outdated
SSHD_REGEX = re.compile(r"(?:^|\W)sshd(?:^|\W)") | ||
|
||
|
||
def tmux_via_ssh(pl, segment_info, mode, caching=True, default=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Selectors do not take additional arguments, so they must not be present. This will only be confusing in documentation.
powerline/selectors/tmux.py
Outdated
if not pid.isnumeric(): | ||
return default | ||
if caching: | ||
if pid in _cached_clients: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logical to write using EAFP principle: just return _cached_clients[pid]
and if that throws KeyError
proceed with using psutil
.
I have done changes so that it works now consistently. |
…uced in tmux 2.1)
Added Error Handling for psutil not being installed
I added documentation now for the tmux SSH selector. |
powerline/commands/main.py
Outdated
@@ -107,6 +107,10 @@ def get_argparser(ArgumentParser=argparse.ArgumentParser): | |||
'-w', '--width', type=int, | |||
help='Maximum prompt with. Triggers truncation of some segments.' | |||
) | |||
parser.add_argument( | |||
'--process-id', metavar='PID', type=int, | |||
help='process_ID for the desired process' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation should start with a capital, end with dot, not use identifiers where they are not meant to (what’s wrong with “Process ID”?) and be more descriptive then “desired process”. I have no idea what “desired process” is supposed to mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would 'The process ID of process that renders powerline.' be okay for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine.
powerline/selectors/tmux.py
Outdated
|
||
Requires the ``psutil`` module. | ||
|
||
Works only for tmux versions >= 2.1''' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is tied to --process-id
being supplied on command-line, not to tmux version in any fashion. Also closing '''
needs to be on its own line and it is not qualified to be a sentence end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#{client_pid} got introduced with tmux 2.1, so it is not supported before
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is tmux bindings implementation detail. All what selector cares about is whether --process-id
is supplied or not, if it is it would be completely happy without even having tmux installed.
powerline/selectors/tmux.py
Outdated
@@ -0,0 +1,34 @@ | |||
# vim:fileencoding=utf-8:noet |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I would actually suggest moving that to powerline/selectors/proc.py
and get rid of any references to tmux here. Neither idea of the process tree traversal nor current implementation of the selector is really tmux-specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is good, however this requires the exclude function to be named powerline.selectors.proc.is_ssh
.
I would propose to change this into a file named common.py and add this module to the general search path for the selectors
powerline/selectors/tmux.py
Outdated
@@ -0,0 +1,34 @@ | |||
# vim:fileencoding=utf-8:noet | |||
from __future__ import (unicode_literals, division, absolute_import, print_function) | |||
from powerline.bindings.tmux import get_tmux_output |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import.
powerline/selectors/tmux.py
Outdated
from powerline.bindings.tmux import get_tmux_output | ||
|
||
|
||
_cached_clients = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keys are process identifiers.
powerline/selectors/tmux.py
Outdated
|
||
_cached_clients = {} | ||
|
||
def tmux_via_ssh(pl, segment_info, mode): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in_ssh
.
powerline/selectors/tmux.py
Outdated
_cached_clients = {} | ||
|
||
def tmux_via_ssh(pl, segment_info, mode): | ||
'''Returns True if tmux client is connected via SSH |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
… if ``--process-id`` has sshd as its ancestor
powerline/selectors/tmux.py
Outdated
try: | ||
import psutil | ||
except ImportError: | ||
pl.warn('Module "psutil" is not installed, thus we cannot detect whether tmux is run via ssh') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check should be performed at module start. See cpu_load_percent
segment in powerline/segments/common/sys: depending on whether import is successfull it either creates an actual class or a dummy which does nothing, but a warning with None
return.
powerline/selectors/tmux.py
Outdated
return False | ||
process_id = segment_info['args'].process_id | ||
if process_id is None: | ||
pl.warn("Process ID was not passed, maybe you are using tmux version < 2.1") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove “, maybe …”.
Thanks for your Feedback @ZyX-I. |
Do not add second default selectors module. There must not be two default modules out there. Also if you were to do that you also need to
|
The problem with the current approach is that the default module is depending on your current powerline renderer. |
This is not the first place where path to function is (or needs to be) full with module, and it will not be the last. But AFAIR there are exactly no places where there are two default modules. I would (now) happily remove all default modules completely everywhere, this makes functions easier to discover when needed and configuration file is not a thing that is edited too frequently to bother, but this is backwards incompatible and also not all people share same opinion. |
Implementation will show segments depending on whether the current active tmux client is connected via SSH
This still has the flaw that it does not draw independent status bars to the screens, but draws the same depending on the status of the last active pane (whether that has is connected via SSH)
References #1895 and #858