function get_cursor_position() termout("\27[6n") return interpret_cursor_position(read_cursor_position_response()) end function read_cursor_position_response() local response = "" while 1 do local char = termin() if char == "R" then break end response = response..char end return response end function interpret_cursor_position(response) local row, column = string.match(response, "\27%[(%d+);(%d+)") return { x = column, y = row } end -- basic unit test do local pos = interpret_cursor_position("\27[28;5") assert(pos.x == "5") assert(pos.y == "28") end