-
Notifications
You must be signed in to change notification settings - Fork 3
/
OutputViewController.lua
122 lines (89 loc) · 3.27 KB
/
OutputViewController.lua
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
--
-- OutputViewController.lua : View Controller to eval the Script and display the output inside a TextView.
--
-- Authors:
-- Vinicius Jarina (vinicius.jarina@xamarin.com)
-- Copyright 2013-2014 Xamarin Inc.
--
-- Licensed under MIT License
--
local OutputViewController = {}
import ('System','System.Text')
OutputViewController.mt = { __call = function(self, code,...)
return self.new (code, ...)
end
}
setmetatable (OutputViewController,OutputViewController.mt)
function OutputViewController.new (code,...)
OutputViewController.m = {}; -- create a table members to store Lua fields
OutputViewController.m.backView = nil;
OutputViewController.m.scriptCode = code;
OutputViewController.m.timer = nil;
luanet.make_object (OutputViewController, 'MonoTouch.UIKit.UIViewController');
return OutputViewController;
end
function OutputViewController:ViewDidLoad ()
self.base:ViewDidLoad();
self.View.BackgroundColor = UIColor.Clear;
self.View.Frame = UIScreen.MainScreen.Bounds;
self.View.AutoresizingMask = luanet.enum(UIViewAutoresizing,'FlexibleWidth,FlexibleHeight');
self.m.backView = UITextView (self.View.Frame);
self.m.backView.AutocapitalizationType = UITextAutocapitalizationType.None;
self.m.backView.AutocorrectionType = UITextAutocorrectionType.No;
self.m.backView.Font = UIFont.FromName ("Menlo", 14.0);
self.m.backView.Editable = false;
self.m.backView.AutoresizingMask = luanet.enum(UIViewAutoresizing,'FlexibleWidth,FlexibleHeight');
self.View:AddSubview (self.m.backView);
local playButton = UIBarButtonItem (UIBarButtonSystemItem.Stop, function() self:OnStop() end);
self.NavigationItem.RightBarButtonItem = playButton;
self.View:LayoutIfNeeded ();
self.m.timer = NSTimer.CreateScheduledTimer (0.1, function ()
self:EvalScript ();
end);
-- Perform any additional setup after loading the view, typically from a nib.
end
function OutputViewController:EvalScript ()
local context = NLuaBoxAppDelegate.AppDelegate.Context;
local printOutputFunc = function (output, ...) self:OutputString(output, ...) end;
print = printOutputFunc;
io.write = printOutputFunc;
local function run()
local context = NLuaBoxAppDelegate.AppDelegate.Context;
context:DoString (self.m.scriptCode);
end
err,msg = pcall (run)
if (not err) then
local error = "Error running... ";
if (msg ~= nil) then
error = error .. msg:ToString ();
if (msg.InnerException ~= nil) then
error = error .. " " .. msg.InnerException:ToString();
end
end
self:ErrorString (error);
end
end
function OutputViewController:ErrorString (str)
self.m.backView.TextColor = UIColor.Red;
self:OutputStringRaw (str);
end
function OutputViewController:OutputString (output, ...)
self.m.backView.TextColor = UIColor.DarkTextColor;
self:OutputStringRaw (output, ...);
end
function OutputViewController:OutputStringRaw (output, ...)
local builder = StringBuilder ();
builder:Append (self.m.backView.Text);
builder:Append (output);
local args = table.pack(...)
for i=1,args.n do
local s = tostring (args[i]);
builder:Append (' ');
builder:Append (s);
end
self.m.backView.Text = builder:ToString ();
end
function OutputViewController:OnStop ()
self:DismissViewController (true, null);
end
return OutputViewController;