/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-05 22:06:58 UTC
  • Revision ID: josh@9ix.org-20130305220658-92yptjso9z57vzly
use zoetrope 288:a82a08660477 2013-02-23

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{
 
15
DebugConsole = Group:extend
 
16
{
16
17
        -- Property: toggleKey
17
18
        -- What key toggles visibility. By default, this is the tab key.
18
19
        toggleKey = 'tab',
83
84
                obj:add(obj.prompt)
84
85
 
85
86
                local inputIndent = obj.log._fontObj:getWidth('>') + 4
86
 
                obj.input = TextInput:new{
 
87
                obj.input = TextInput:new
 
88
                {
87
89
                        x = inputIndent, y = 0, width = the.app.width,
88
90
                        active = false,
89
91
                        onType = function (self, char)
117
119
 
118
120
                obj._oldPrint = print
119
121
                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 (...)
120
138
                        for _, value in pairs{...} do
121
139
                                obj.log.text = obj.log.text .. tostring(value) .. ' '
122
140
                        end
171
189
 
172
190
        execute = function (self, code)
173
191
                if string.sub(code, 1, 1) == '=' then
174
 
                        code = 'print (' .. string.sub(code, 2) .. ')'
 
192
                        code = 'debugger._unsourcedPrint (' .. string.sub(code, 2) .. ')'
175
193
                end
176
194
 
177
195
                local func, err = loadstring(code)
180
198
                        local ok, result = pcall(func)
181
199
 
182
200
                        if not ok then
183
 
                                print('Error, ' .. tostring(result) .. '\n')
 
201
                                debugger._unsourcedPrint('Error, ' .. tostring(result) .. '\n')
184
202
                        else
185
 
                                print('')
 
203
                                debugger._unsourcedPrint('')
186
204
                        end
187
205
 
188
206
                        return tostring(result)
189
207
                else
190
 
                        print('Syntax error, ' .. string.gsub(tostring(err), '^.*:', '') .. '\n')
 
208
                        debugger._unsourcedPrint('Syntax error, ' .. string.gsub(tostring(err), '^.*:', '') .. '\n')
191
209
                end
192
210
        end,
193
211
 
265
283
                        -- update log
266
284
 
267
285
                        if self._updateLog then
268
 
                                local maxHeight = the.app.height - 20
 
286
                                local lineHeight = self.log._fontObj:getHeight()
269
287
                                local _, height = self.log:getSize()
270
 
 
271
 
                                while height > maxHeight do
272
 
                                        self.log.text = string.gsub(self.log.text, '^.-\n', '') 
273
 
                                        _, 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
274
293
                                end
275
294
 
276
295
                                self.prompt.y = height + 4
310
329
                        end
311
330
 
312
331
                        if the.keys:justPressed('return') then
313
 
                                print('>' .. self.input.text)
 
332
                                debugger._unsourcedPrint('>' .. self.input.text)
314
333
                                self:execute(self.input.text)
315
334
                                table.insert(self.inputHistory, self.inputHistoryIndex, self.input.text)
316
335
 
341
360
if debugger then
342
361
        debugger.reload = function()
343
362
                if DEBUG then
 
363
                        love.audio.stop()
 
364
 
344
365
                        -- create local references to needed variables
345
366
                        -- because we're about to blow the global scope away
346
367