1K Terminal challenge

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

1K Terminal challenge

Post by ivan »

Goal:
Lua command line terminal

Rules:
-typed input via the keyboard
-execute commands by pressing enter
-delete characters via backspace
-do not use external files, system fonts or love's default embedded font

Extra credit:
-print results and errors on the screen
-upper and lowercase, caps
-allow printing in the terminal, ex: print("123") or print(1+2)
-shift modifier
-numeric keypad
-locals
Last edited by ivan on Mon Oct 17, 2016 7:26 pm, edited 9 times in total.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: 1K Terminal challange

Post by ivan »

Test with a large 8x8 font = 1,531 bytes:

Code: Select all

F=" 66 __ ` 66?c6f_ 0 63<_   6?n ÿ? ?   0; <_ _ 6?f3 f   6cn _ _______ 8??_ 3+<+3_ 3;0060+?0?30> 0700330_?3+0+3  ??0 _____ __>?<?<3xgcccff6FFf30fwg6{3ff306oc{3>f?0k{c{?ffs336Fcsc3ff6Ff33ffcc63?<?|3gcc________???+cc3 f3f3-+cc+ f3f+c636 >3>+kc ;8+6F0_633wcc`_8g?cc@________ ÿ  8  _   0 6 __ >0n6f7 0f3>33n6+ >f3?3fk+ 3f+>f6c+ n=n0ggc3___ _____ ___8n_____;;n>>+cc3? 6f36+c63 8 cf36+k3 c>>0,36>& c0n6c0?8 x___ ___"F=F:gsub("_","  "):gsub("+","33")O=""for a=1,#F do b=F:byte(a)for j=1,8 do h=b%2;O=O..(h>0 and"ÿÿÿÿ"or"   y")b=(b-h)/2 end end;L=love;G=L.graphics;d=L.image.newImageData(128,48,O)I=G.newImage(d)Q={}for c=0,5 do for f=0,15 do Q[#Q+1]=G.newQuad(f*8,c*8,8,8,128,48)end end;function W(g,f,c)for a=1,#g do b=g:byte(a)-31;if Q[b]then G.draw(I,Q[b],f+(a-1)*8,c)else W(g:sub(a+1),f,c+8)break end end end;O=""function L.draw()W(O.."_",0,0)end;function print(i)O=O.."\n"..tostring(i)end;Z='~!@#$%^&*()_+|{}:"<>?'z="`1234567890-=\\[];',./"function L.keypressed(k)if k=="space"then O=O.." "elseif k=="backspace"then O=O:sub(1,-2)elseif k=="return"then e=O:match("[^\n]-$")r,t=pcall(loadstring(e))O=O.."\n"..(t or"").."\n"elseif#k<2 then if L.keyboard.isScancodeDown('lshift')then j=z:find(k,1,true)k=j and Z:sub(j,j)or k;k=k:upper()end;O=O..k end end
Note: artifacts probably due to newImageData, no locals
Attachments
1kterminal.love
version 1
(1.1 KiB) Downloaded 346 times
Font image used - 1-bit BMP with the header removed, converted to 8-bit chars
Font image used - 1-bit BMP with the header removed, converted to 8-bit chars
terminal8x8_gs_ro2.PNG (623 Bytes) Viewed 9498 times
Last edited by ivan on Sun May 22, 2016 5:30 am, edited 5 times in total.
User avatar
Ortimh
Citizen
Posts: 90
Joined: Tue Sep 09, 2014 5:07 am
Location: Indonesia

Re: 1K Terminal challange

Post by Ortimh »

It's so hard. I couldn't do it. I use your code, optimize it a bit, and now can use right shift for caps, with 3 bytes in extra (1,528 bytes).

Code: Select all

F=(" 66 __ ` 66?c6f_ 0 63<_   6?n ÿ? ?   0; <_ _ 6?f3 f   6cn _ _______ 8??_ 3+<+3_ 3;0060+?0?30> 0700330_?3+0+3  ??0 _____ __>?<?<3xgcccff6FFf30fwg6{3ff306oc{3>f?0k{c{?ffs336Fcsc3ff6Ff33ffcc63?<?|3gcc________???+cc3 f3f3-+cc+ f3f+c636 >3>+kc ;8+6F0_633wcc`_8g?cc@________ ÿ  8  _   0 6 __ >0n6f7 0f3>33n6+ >f3?3fk+ 3f+>f6c+ n=n0ggc3___ _____ ___8n_____;;n>>+cc3? 6f36+c63 8 cf36+k3 c>>0,36>& c0n6c0?8 x___ ___"):gsub("_","  "):gsub("+","33")O=""for a=1,#F do b=F:byte(a)for j=1,8 do h=b%2 O=O..(h>0 and"ÿÿÿÿ"or"   y")b=(b-h)/2 end end L=love G=L.graphics K=L.keyboard.isScancodeDown I=G.newImage(L.image.newImageData(128,48,O)) Q={}for c=0,5 do for f=0,15 do Q[#Q+1]=G.newQuad(f*8,c*8,8,8,128,48) end end function W(g,f,c) for a=1,#g do b=g:byte(a)-31 if Q[b]then G.draw(I,Q[b],f+(a-1)*8,c)else W(g:sub(a+1),f,c+8)break end end end O=""function L.draw()W(O.."_",0,0)end function print(i)O=O.."\n"..tostring(i)end Z='~!@#$%^&*()_+|{}:"<>?'z="`1234567890-=\\[];',./"function L.keypressed(k)k=k:gsub("space"," ")if k=="back "then O=O:sub(1,-2)elseif k=="return"then e=O:match("[^\n]-$")r,t=pcall(loadstring(e))O=O.."\n"..(t or"").."\n"elseif#k<2 then if K('lshift')or K('rshift') then j=z:find(k,1,true)k=(j and Z:sub(j,j)or k):upper()end O=O..k end end
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: 1K Terminal challange

Post by ivan »

Great, I'll use your version in future revisions.
It's possible to get it under 1k, if we use a smaller font - as you see from the image in the previous post 8x8 px per character is a lot.
Also, there is a "bug" with: r,t=pcall(loadstring(e)) - it doesn't print the exact Lua error, I'll see if I can fix it in the future.
Thanks for taking a look!
User avatar
DeltaF1
Citizen
Posts: 64
Joined: Mon Apr 27, 2015 4:12 pm
Location: The Bottom of the Stack
Contact:

Re: 1K Terminal challange

Post by DeltaF1 »

I can't run this, it says "The size of the raw byte string must match the imageData's actual size in bytes."
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: 1K Terminal challange

Post by airstruck »

Instructions ambiguous. Here's my entry.

Code: Select all

local buffer = ''

function love.draw ()
    love.graphics.printf(buffer, 0, 0, 800)
end

function love.textinput (text)
    buffer = buffer .. text
end

function love.keypressed (key)
    if key == 'backspace' then
        buffer = buffer:gsub('[^\n]$', '')
    elseif key == 'return' then
        buffer = buffer .. '\n' ..
            io.popen(buffer:match('[^\n]*$') .. ' 2>&1'):read('*a') .. '\n'
    end
end
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: 1K Terminal challange

Post by ivan »

DeltaF1 wrote:I can't run this, it says "The size of the raw byte string must match the imageData's actual size in bytes."
Might have something to do with the encoding of the .lua file so I've included a .love.
Instructions ambiguous
The point of the challenge is to make a terminal without using external/system fonts. In 1K or less. :)
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: 1K Terminal challange

Post by airstruck »

ivan wrote:The point of the challenge is to make a terminal without using external/system fonts. In 1K or less. :)
I guess I did that, then.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: 1K Terminal challange

Post by ivan »

Unfortunately, you used love.graphics.printf which doesn't comply with the rules.
The embedded Love2D font is around 400 k in its non-compiled form.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: 1K Terminal challange

Post by airstruck »

ivan wrote:Unfortunately, you used love.graphics.printf which doesn't comply with the rules.
The embedded Love2D font is around 400 k in its non-compiled form.
The rules don't restrict the use of love.graphics.printf or the use of the font that's packaged with love as far as I can tell. They also don't mention what the terminal is supposed to do (mine runs shell commands). The rules also don't mention anything about counting the size of assets packaged with love against the 1k character count. That's not something I'd expect given that those assets were not part of my submission. As far as I can tell, my entry complies with the rules as written.
Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests