Mercurial Hosting > luan
comparison core/src/luan/modules/Io.luan @ 589:97c8ae330efe
add varargs to Luan.try;
add Io.output_to and Io.output_of;
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 26 Aug 2015 04:38:37 -0600 |
parents | 18504c41b0be |
children | 790d5de23042 |
comparison
equal
deleted
inserted
replaced
588:5fdbefa80146 | 589:97c8ae330efe |
---|---|
44 function M.print(...) | 44 function M.print(...) |
45 M.print_to(M.stdout,...) | 45 M.print_to(M.stdout,...) |
46 end | 46 end |
47 | 47 |
48 | 48 |
49 function M.output_to(out,fn,...) | |
50 local old_out = M.stdout | |
51 return try( { | |
52 function(...) | |
53 M.stdout = out | |
54 fn(...) | |
55 end; | |
56 finally = function() | |
57 M.stdout = old_out | |
58 end; | |
59 }, ... ) | |
60 end | |
61 | |
62 local uri = M.uri -- make local | |
63 | |
64 function M.output_of(fn,...) | |
65 local string_uri = uri "string:" | |
66 local out = string_uri.text_writer() | |
67 M.output_to(out,fn,...) | |
68 out.close() | |
69 return string_uri.read_text() | |
70 end | |
71 | |
72 | |
49 -- repr | 73 -- repr |
50 | 74 |
51 local function do_repr(obj,done) | 75 local function do_repr(obj,done) |
52 local tp = type(obj) | 76 local tp = type(obj) |
53 if tp == "table" then | 77 if tp == "table" then |
85 else | 109 else |
86 %><<%=obj%>><% | 110 %><<%=obj%>><% |
87 end | 111 end |
88 end | 112 end |
89 | 113 |
90 local uri = M.uri -- make local | |
91 | |
92 function M.repr(obj) | 114 function M.repr(obj) |
93 local old_out = M.stdout | 115 return M.output_of(do_repr,obj,{}) |
94 return try { | |
95 function() | |
96 local string_uri = uri "string:" | |
97 M.stdout = string_uri.text_writer() | |
98 do_repr(obj,{}) | |
99 M.stdout.close() | |
100 return string_uri.read_text() | |
101 end; | |
102 finally = function() | |
103 M.stdout = old_out | |
104 end; | |
105 } | |
106 end | 116 end |
107 | 117 |
108 | 118 |
109 -- useful for SimplyHTML responsiveness | 119 -- useful for SimplyHTML responsiveness |
110 | 120 |