running `print` now appends to output editor

akkartik
Nov 19, 2023, 10:37 PM
I52S4E5FT6E6FHH3QEDJPWO7C6W7AIMAFDP2KQ5FEWXFQX7RY6CAC

Dependencies

  • [2] VAVXKWZV aggregate global state inside a 'pane' object
  • [3] 6RYGW5H3 bugfix: output border color
  • [4] Z5M23NTK implement second, 'output' editor
  • [5] 5RUFNRJO start of the visual skeleton
  • [6] WR2WMEPE implement 'Run' button
  • [7] PRE6XPRN responsively increase/decrease font height
  • [*] R5QXEHUI somebody stop me

Change contents

  • file addition: 0051-run_button (----------)
    [9.2]
    run_button = function()
    button(Global_state, 'run', {x=Menu_left+5, y=Menu_top+5, w=App.width('Run')+10, h=Line_height, bg={r=0.6, g=0.8, b=0.6},
    icon = function(p)
    App.color(Normal_color)
    love.graphics.rectangle('line', p.x,p.y, p.w,p.h, 2,2)
    love.graphics.print('Run', p.x+5,p.y+2)
    end,
    onpress1 = function()
    -- ## run: initialize
    local buf = table.concat(map(Current_pane.editor_state.lines, function(line) return line.data end), '\n')
    Current_pane.canvas = love.graphics.newCanvas()
    love.graphics.setCanvas(Current_pane.canvas)
    love.graphics.push('all')
    love.graphics.setBackgroundColor(1,1,1)
    love.graphics.setColor(0,0,0)
    Current_pane.output_editor_state.lines = {}
    Text.redraw_all(Current_pane.output_editor_state)
    local real_print = print
    print = print_to_output
    -- ## run
    local status, result = live.eval(buf)
    -- ## run: save some stuff, clean up the rest
    print = real_print
    print(status, result)
    if result then
    -- could be either output or error
    table.insert(Current_pane.output_editor_state.lines, {data=tostring(result)})
    end
    if #Current_pane.output_editor_state.lines == 0 then
    table.insert(Current_pane.output_editor_state.lines, {data=''})
    end
    Text.redraw_all(Current_pane.output_editor_state)
    love.graphics.pop()
    love.graphics.setCanvas()
    end,
    })
    end
  • file addition: 0050-print_to_output (----------)
    [9.2]
    print_to_output = function(...)
    local line = table.concat(map({...}, tostring), ' ')
    table.insert(Current_pane.output_editor_state.lines,
    {data=line})
    end
  • replacement in 0021-draw_menu at line 5
    [3.763][2.1427:1550](),[3.886][3.948:969](),[2.1550][3.948:969](),[3.948][3.948:969](),[3.969][3.138:165](),[3.165][3.887:945](),[3.995][3.887:945](),[3.945][3.995:1085](),[3.995][3.995:1085](),[3.1085][2.1551:1759](),[2.1759][3.644:790](),[3.644][3.644:790](),[3.790][3.1576:1658](),[3.1658][2.1760:1807](),[2.1807][3.1692:1728](),[3.1692][3.1692:1728](),[3.1728][2.1808:1862](),[3.1769][3.830:889](),[2.1862][3.830:889](),[3.830][3.830:889](),[3.889][3.946:957](),[3.1085][3.946:957]()
    button(Global_state, 'run', {x=Menu_left+5, y=Menu_top+5, w=App.width('Run')+10, h=Line_height, bg={r=0.6, g=0.8, b=0.6},
    icon = function(p)
    App.color(Normal_color)
    love.graphics.rectangle('line', p.x,p.y, p.w,p.h, 2,2)
    love.graphics.print('Run', p.x+5,p.y+2)
    end,
    onpress1 = function()
    print('run')
    local buf = table.concat(map(Current_pane.editor_state.lines, function(line) return line.data end), '\n')
    Current_pane.canvas = love.graphics.newCanvas()
    love.graphics.setCanvas(Current_pane.canvas)
    love.graphics.push('all')
    love.graphics.setBackgroundColor(1,1,1)
    love.graphics.setColor(0,0,0)
    local status, result = live.eval(buf)
    print(status, result)
    if result then
    -- could be either output or error
    Current_pane.output_editor_state.lines = {
    {data=tostring(result)},
    }
    Text.redraw_all(Current_pane.output_editor_state)
    end
    love.graphics.pop()
    love.graphics.setCanvas()
    end,
    })
    [3.763]
    [3.957]
    run_button()