/
tty-macros.dylan
57 lines (55 loc) · 1.36 KB
/
tty-macros.dylan
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
module: tty
synposis: TTY macros.
author: Ingo Albrecht <prom@berlin.ccc.de>
copyright: see accompanying file LICENSE
/*
* Run body in context of tty
*/
define macro with-tty
{ with-tty (?tty:expression)
?:body
end }
=> { begin
local method %code () => ()
?body
end;
%with-tty(?tty, %code);
end }
end macro;
/*
* Helper for with-tty macro
*/
define method %with-tty(t :: <tty>, function :: <function>, #rest arguments)
=> ();
// remember original streams and tty
let oo = *standard-output*;
let oe = *standard-error*;
let ot = *current-tty*;
block ()
// finish previous tty and switch to new one
if (ot)
tty-finish(ot);
end;
*current-tty* := t;
tty-start(t);
// wrap stdio streams
*standard-output* := make(<tty-stream>, inner-stream: tty-output(t));
*standard-error* := make(<tty-stream>, inner-stream: tty-error(t) | tty-output(t));
// call body function
apply(function, arguments);
cleanup
// finish all remaining activities
while (tty-activity(t))
tty-finish-activity(t);
end;
// restore original streams and tty
*standard-output* := oo;
*standard-error* := oe;
// finish tty and switch to previous one
*current-tty* := ot;
tty-finish(t);
if (ot)
tty-start(ot);
end;
end;
end;