Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- do -- console input (but you have to use the main window.... for now)
- local buffer = ""
- local history = {}
- local i = 0
- function asdfml.OnTextEntered(params)
- local str = string.char(params.text.unicode)
- if str and str:byte() >= 32 then
- buffer = buffer .. str
- io.write(str)
- end
- end
- local caret_position = 0
- local function set_caret_pos(pos)
- -- let's say pos should be somewhere at #buffer * 0.75
- -- our carret position is here
- -- 1 2 3 4 5 6 7 8 9 |
- -- since it may not be we need to add any offsets
- pos = pos + caret_position
- -- when we add 9 (the length of the buffer) stars (\b) it will go back to the start
- -- | 1 2 3 4 5 6 7 8 9 * * * * * * * * *
- -- so the length of the line + the start position
- -- would be the position from the beginning
- -- but if we add one more, it will go even further out
- -- because it's in /reverse/
- pos = -pos
- -- so if we reverse it, we should get it somewhere here
- -- 1 2 3 4 5 6 | 7 8 9
- io.write(("\b"):rep(#buffer + pos))
- caret_position = pos
- end
- local function remove(start, stop)
- if not start and not stop then
- start = 0
- stop = #buffer
- end
- -- io.write(("\b"):rep(start#buffer), (" "):rep((#buffer%start) + #buffer%stop), ("\b"):rep(#buffer%start), buffer)
- end
- function asdfml.OnKeyPressed(params)
- local key = params.key.code
- if key == e.KEY_END then
- set_caret_pos(#buffer*0.75)
- end
- if key == e.KEY_LEFT then
- io.write("\b")
- elseif key == e.KEY_RIGHT then
- io.write(buffer:sub(0,1):rep(2), "\b")
- end
- if key == e.KEY_UP then
- i = i - 1
- local prev = buffer
- buffer = history[i%#history+1]
- io.write(("\b"):rep(#prev), (" "):rep(#prev), ("\b"):rep(#prev), buffer)
- elseif key == e.KEY_DOWN then
- i = i + 1
- local prev = buffer
- buffer = history[i%#history+1]
- io.write(("\b"):rep(#prev), (" "):rep(#prev), ("\b"):rep(#prev), buffer)
- end
- if key == e.KEY_BACK then
- buffer = buffer:sub(0, -2)
- io.write("\b \b")
- end
- if key == e.KEY_RETURN then
- io.write("\n")
- local ok, err = console.CallCommandLine(buffer)
- if not ok then
- print(err)
- end
- table.insert(history, buffer)
- buffer = ""
- end
- end
- console.AddCommand("lua_run", function(...)
- print(...)
- end)
- end
Advertisement
Add Comment
Please, Sign In to add comment