Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Line editor
import std
import color
import vec
let fontsize = 40
let cursor = xy{ 4, fontsize }
let cursor_hz = 2
fatal(gl_window("edlin", 800, 800))
check(gl_set_font_name("data/fonts/Droid_Sans_Mono/DroidSansMono.ttf"), "can\'t load font!")
var text = ""
var at = 0
let lowers = [
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
".", ",", "/", ";", "\'", "\\", "[", "]", "-", "="
]
let uppers = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
")", "!", "@", "#", "$", "%", "^", "&", "*", "(",
"<", ">", "?", ":", "\"", "|", "{", "}", "_", "+"
]
while gl_frame():
gl_clear(color_black)
gl_set_font_size(fontsize)
gl_translate(xy_1 * 20):
if length(text) > 0:
gl_text(text)
if int(gl_time() * cursor_hz) % 2 == 0:
let size = gl_text_size(substring(text, 0, at))
gl_translate(xy_x * float(size))
gl_rect(float(cursor))
if gl_button("left ctrl") >= 0 || gl_button("right ctrl") >= 0:
if gl_button("u") == 0:
text = substring(text, at, length(text)-at)
at = 0
if gl_button("k") == 0:
text = substring(text, 0, at)
if gl_button("a") == 0:
at = 0
if gl_button("e") == 0:
at = length(text)
if gl_button("w") == 0:
nil // TODO
else: if gl_button("backspace") == 0:
text = substring(text, 0, max(0, at - 1)) + substring(text, at, length(text)-at)
at = max(0, at - 1)
else: if gl_button("delete") == 0:
text = substring(text, 0, at) + substring(text, at+1, min(length(text), length(text)-at+1))
else: if gl_button("left") == 0:
at = max(0, at - 1)
else: if gl_button("right") == 0:
at = min(length(text), at + 1)
else:
def advance(letter):
text = substring(text, 0, at) + letter + substring(text, at, length(text)-at)
at = at + 1
if gl_button("space") == 0:
advance(" ")
for(lowers) lower, i:
if gl_button(lower) == 0:
if gl_button("left shift") >= 0 || gl_button("right shift") >= 0:
advance(uppers[i])
else:
advance(lower)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.