/zoeplat

To get this branch, use:
bzr branch http://9ix.org/bzr/zoeplat

« back to all changes in this revision

Viewing changes to zoetrope/utils/debug.lua

  • Committer: Josh C
  • Date: 2013-03-02 20:40:57 UTC
  • Revision ID: josh@9ix.org-20130302204057-yrra0a51zgtpq2v2
zoetrope 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
--              - Control-Alt-S saves a screenshot to the app's directory --
13
13
--                see https://love2d.org/wiki/love.filesystem for where this is.
14
14
 
15
 
DebugConsole = Group:extend
16
 
{
 
15
DebugConsole = Group:extend{
17
16
        -- Property: toggleKey
18
17
        -- What key toggles visibility. By default, this is the tab key.
19
18
        toggleKey = 'tab',
84
83
                obj:add(obj.prompt)
85
84
 
86
85
                local inputIndent = obj.log._fontObj:getWidth('>') + 4
87
 
                obj.input = TextInput:new
88
 
                {
 
86
                obj.input = TextInput:new{
89
87
                        x = inputIndent, y = 0, width = the.app.width,
90
88
                        active = false,
91
89
                        onType = function (self, char)
119
117
 
120
118
                obj._oldPrint = print
121
119
                print = function (...)
122
 
                        local caller = debug.getinfo(2)
123
 
 
124
 
                        if caller.linedefined ~= 0 then
125
 
                                obj.log.text = obj.log.text .. '(' .. caller.short_src .. ':' .. caller.linedefined .. ') '
126
 
                        end
127
 
 
128
 
                        for _, value in pairs{...} do
129
 
                                obj.log.text = obj.log.text .. tostring(value) .. ' '
130
 
                        end
131
 
 
132
 
                        obj.log.text = obj.log.text .. '\n'
133
 
                        obj._updateLog = true
134
 
                        obj._oldPrint(...)
135
 
                end
136
 
 
137
 
                debugger._unsourcedPrint = function (...)
138
120
                        for _, value in pairs{...} do
139
121
                                obj.log.text = obj.log.text .. tostring(value) .. ' '
140
122
                        end
189
171
 
190
172
        execute = function (self, code)
191
173
                if string.sub(code, 1, 1) == '=' then
192
 
                        code = 'debugger._unsourcedPrint (' .. string.sub(code, 2) .. ')'
 
174
                        code = 'print (' .. string.sub(code, 2) .. ')'
193
175
                end
194
176
 
195
177
                local func, err = loadstring(code)
198
180
                        local ok, result = pcall(func)
199
181
 
200
182
                        if not ok then
201
 
                                debugger._unsourcedPrint('Error, ' .. tostring(result) .. '\n')
 
183
                                print('Error, ' .. tostring(result) .. '\n')
202
184
                        else
203
 
                                debugger._unsourcedPrint('')
 
185
                                print('')
204
186
                        end
205
187
 
206
188
                        return tostring(result)
207
189
                else
208
 
                        debugger._unsourcedPrint('Syntax error, ' .. string.gsub(tostring(err), '^.*:', '') .. '\n')
 
190
                        print('Syntax error, ' .. string.gsub(tostring(err), '^.*:', '') .. '\n')
209
191
                end
210
192
        end,
211
193
 
283
265
                        -- update log
284
266
 
285
267
                        if self._updateLog then
286
 
                                local lineHeight = self.log._fontObj:getHeight()
 
268
                                local maxHeight = the.app.height - 20
287
269
                                local _, height = self.log:getSize()
288
 
                                local linesToDelete = math.ceil((height - the.app.height - 20) / lineHeight)
289
 
                                
290
 
                                if linesToDelete > 0 then
291
 
                                        self.log.text = string.gsub(self.log.text, '.-\n', '', linesToDelete) 
292
 
                                        height = height - linesToDelete * lineHeight
 
270
 
 
271
                                while height > maxHeight do
 
272
                                        self.log.text = string.gsub(self.log.text, '^.-\n', '') 
 
273
                                        _, height = self.log:getSize()
293
274
                                end
294
275
 
295
276
                                self.prompt.y = height + 4
329
310
                        end
330
311
 
331
312
                        if the.keys:justPressed('return') then
332
 
                                debugger._unsourcedPrint('>' .. self.input.text)
 
313
                                print('>' .. self.input.text)
333
314
                                self:execute(self.input.text)
334
315
                                table.insert(self.inputHistory, self.inputHistoryIndex, self.input.text)
335
316
 
360
341
if debugger then
361
342
        debugger.reload = function()
362
343
                if DEBUG then
363
 
                        love.audio.stop()
364
 
 
365
344
                        -- create local references to needed variables
366
345
                        -- because we're about to blow the global scope away
367
346