-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreener
executable file
·143 lines (115 loc) · 3.63 KB
/
screener
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
##############################################################################
#
# NAME: screener
#
# DESCRIPTION: Show a menu with all the existing screens (GNU screen) and
# offer the user the choice to join any of them.
#
# Present also an option to create a new named screen session.
#
# USAGE: screener
#
# DEPENDENCIES:
# * GNU screen
#
##############################################################################
#
import subprocess
import sys
import os
FLAG_ATTACH = "-r"
FLAG_JOIN = "-x"
FLAG_CREATE = "-S"
def select_elems(a_list, elems):
"""
Non consecutive slicing.
"""
return [a_list[li] for li in elems]
def get_screen_list():
"""
Run the screen command and get a list of available screens.
Every list element is a tuple of the form:
("screen name", "Screen status")
Screen status can be either (Attached) or (Detached)
"""
screen_list_cmd = "screen -ls"
# Filter the output, only Attached or Detached screens
filter_screens = lambda x: ("(Attached)" in x or "(Detached)" in x)
screens = list(filter(filter_screens,
subprocess.getoutput(screen_list_cmd).splitlines()))
# Get only screen name and status
screen_list = [select_elems(line.strip().split(), (0, -1))
for line in screens]
return screen_list
def show_screen_menu(screen_list):
"""
Print all screen names in the list with a different number each.
"""
if len(screen_list) == 0:
print("No screens found")
print()
screen_list.append(["New screen", ""])
for (num, screen) in enumerate(screen_list):
print("%2d) %20s %12s" % (num + 1, screen[0], screen[1]))
def get_user_numeric_option(prompt, minopt, maxopt):
"""
Get a number from the user in a given range.
Return -1 if the input is not a number or if it out of range.
"""
try:
numopt = int(input(prompt))
if numopt < minopt or numopt > maxopt:
numopt = -1
except:
numopt = -1
return numopt
def validate_option(opt, invalid, errmsg):
"""
Check that the option is in the list of valid options.
"""
if opt in invalid:
print(errmsg)
return False
return True
def main():
screen_list = get_screen_list()
screen_count = len(screen_list)
if screen_count > 0:
# there is something to choose from
show_screen_menu(screen_list[:])
try:
opt = get_user_numeric_option("Screen # --> ", 1, screen_count + 1)
except KeyboardInterrupt:
print()
return 1
if not validate_option(opt, [-1], "No valid screen selected"):
return 1
else:
print("No screens found, will create one.")
print()
opt = 1 # This forces the creation of a new screen
# New screen
if opt == screen_count + 1:
try:
screen_name = input("New screen name --> ")
except KeyboardInterrupt:
print()
return 1
modif = FLAG_CREATE
validate_option(screen_name, [""], "Invalid screen name")
# Existing screen
else:
screen = screen_list[opt - 1]
screen_name = screen[0]
if "Attached" in screen[1]:
modif = FLAG_JOIN
elif "Detached" in screen[1]:
modif = FLAG_ATTACH
else:
print("Don't know what to do with a screen in %s state" % (
screen[1]))
return 1
os.execlp("screen", "screen", modif, screen_name)
if __name__ == "__main__":
sys.exit(main())