/var/lib/sorcery/modules/libscreen
1 #!/bin/bash
2
3 #-----
4 ## Output a short message about using libscreen
5 #-----
6 function screen_quick_intro() {
7 message "${MESSAGE_COLOR}Using screen mode. Watch the bottom for notices."
8 message "${MESSAGE_COLOR}Press ^A-n and ^A-p to cycle through windows."
9 message "${MESSAGE_COLOR}Screen help">help">help is available with ^A-? (control-a ?)${DEFAULT}"
10 }
11
12 #-----
13 ## @param Screen session name
14 ## @param Command
15 ## @param (optional) Command parameter
16 ## @param ...
17 ## THIS FUNCTION NEVER RETURNS.
18 ## It execs a screen session with the specified name, with the specified
19 ## arguments.
20 #-----
21 function screen_start() {
22 $STD_DEBUG
23 local name="$1"
24 local screen
25 shift
26 if ! smgl_which screen screen > /dev/null ; then
27 message "Cannot find screen."
28 message "Consider fixing the PATH, or disabling screen mode and casting screen."
29 exit 1
30 fi
31 exec $screen -S "$name" $SCREEN_KEY -c $SCREENRC "$@"
32 }
33
34 #-----
35 ## @param Screen session name
36 ## This function is not used, and probably won't be. It is a way to run
37 ## a command in a screen.
38 ## The .!!| args to exec were determined by trial and error. ::: didn't work.
39 #-----
40 function screen_command() {
41 $STD_DEBUG
42 local name="$1"
43 shift
44 screen -x "$name" -X exec '.!!|' echo "$@"
45 sleep $SCREEN_ANTI_RACE_SLEEP
46 }
47
48 #-----
49 ## @param Screen session name
50 ## @param Window type
51 ## @param Name of new window
52 ## @param Command
53 ## @param (optional) Command args
54 ## @param ...
55 ## Window type may be one of:
56 ## <li>SCREEN_CAST_WIN
57 ## <li>SCREEN_SUMMON_WIN
58 ## <li>SCREEN_MAIN_WIN
59 ## <li>SCREEN_LAST_FAILED_CAST_WIN
60 ## <li>SCREEN_DEBUG_WIN
61 ## Only one of each type is allowed to exist at once
62 ## They are actualy window numbers in disguise
63 #-----
64 function screen_new_window() {
65 $STD_DEBUG
66 local screen_name="$1"
67 local win_num="$2"
68 local win_name="$3"
69 shift 3
70 screen -x "$screen_name" -X screen "$win_num" "$@"
71 sleep $SCREEN_ANTI_RACE_SLEEP
72 screen_name_window "$screen_name" "$win_num" "$win_name"
73 screen_notify "$screen_name" "$win_name started"
74 }
75
76 #-----
77 ## @param Screen session name
78 ## Attaches the named screen session to the tty.
79 ## Doesn't work properly since screen doesn't like being run in the bg.
80 ## Default escape code is ^A.
81 #-----
82 function screen_attach() {
83 $STD_DEBUG
84 screen -r "$1" $SCREEN_KEY &
85 }
86
87 #-----
88 ## @param Screen session name
89 ## Detaches the named screen session from the tty.
90 #-----
91 function screen_detach() {
92 $STD_DEBUG
93 screen -x "$1" -X detach
94 }
95
96 #-----
97 ## @param Screen session name
98 ## @param Window number
99 ## Kills the window in the session. Also notifies the user that the window was
100 ## closed
101 #-----
102 function screen_kill_window() {
103 $STD_DEBUG
104 screen -x "$1" -p "$2" -X kill
105 sleep $SCREEN_ANTI_RACE_SLEEP
106 screen_notify "$1" "$2 ended"
107 # screen_switch_window "$1" 0
108 }
109
110 #-----
111 ## @param Screen session name
112 ## @param Window number
113 ## @param New name
114 ## Give an existing window a new name
115 #-----
116 function screen_name_window() {
117 $STD_DEBUG
118 screen -x "$1" -p "$2" -X title "$3"
119 sleep $SCREEN_ANTI_RACE_SLEEP
120 }
121
122 #-----
123 ## @param Screen session name
124 ## Give aname to the current window.
125 ## There are race conditions aplenty if you're not careful
126 #-----
127 function screen_name_curr_window() {
128 $STD_DEBUG
129 screen -x "$1" -X title "$2"
130 sleep $SCREEN_ANTI_RACE_SLEEP
131 }
132
133 #-----
134 ## @param Screen session name
135 ## @param Message
136 ## Notify the user of something happening using the status bar
137 #-----
138 function screen_notify() {
139 $STD_DEBUG
140 screen -x "$1" -X echo "$@"
141 sleep $SCREEN_ANTI_RACE_SLEEP
142 }
143
144 #-----
145 ## @param Screen session name
146 ## @param Window to monitor
147 ## Sets a window to be monitored. The user will be informed of any activity
148 ## in the window.
149 #-----
150 function screen_monitor_window() {
151 $STD_DEBUG
152 screen -x "$1" -p "$2" -X monitor on
153 sleep $SCREEN_ANTI_RACE_SLEEP
154 }
155
156 #-----
157 ## @param Screen session name
158 ## @param Window to monitor
159 ## Stops a window from being monitored
160 #-----
161 function screen_unmonitor_window() {
162 $STD_DEBUG
163 screen -x "$1" -p "$2" -X monitor off
164 sleep $SCREEN_ANTI_RACE_SLEEP
165 }
166
167 #-----
168 ## @param Screen session name
169 ## @param Window to switch to
170 ## Changes the window the user sees
171 #-----
172 function screen_switch_window() {
173 $STD_DEBUG
174 screen -x "$1" -X select "$2"
175 sleep $SCREEN_ANTI_RACE_SLEEP
176 }
177
178 #-----
179 ## @param Screen session name
180 ## Ends the screen session. Should end all processes running in the screen.
181 #-----
182 function screen_quit() {
183 $STD_DEBUG
184 screen -x "$1" -X quit
185 }
186
187 #-----
188 ## @param Screen session name
189 ## @param Window to move to
190 ## @param Window number to move to
191 ## Moves a window to a new number
192 ## If something is already at that number, it swaps them
193 #-----
194 function screen_move_window() {
195 $STD_DEBUG
196 screen -x "$1" -p "$2" -X number "$3"
197 sleep $SCREEN_ANTI_RACE_SLEEP
198 }
199
200 #-----
201 ## @param Screen session name
202 ## @param Delay in seconds
203 ## Sets the time screen shows messages for
204 #-----
205 function screen_set_msgwait() {
206 $STD_DEBUG
207 screen -x "$1" -X msgwait $2
208 sleep $SCREEN_ANTI_RACE_SLEEP
209 }