Skip to content

Instantly share code, notes, and snippets.

@egwspiti
Last active October 6, 2024 08:06
Show Gist options
  • Save egwspiti/593e13664071e35385ac to your computer and use it in GitHub Desktop.
Save egwspiti/593e13664071e35385ac to your computer and use it in GitHub Desktop.
pure ruby get terminal cursor position
require 'io/console'
class Cursor
class << self
def pos
res = ''
$stdin.raw do |stdin|
$stdout << "\e[6n"
$stdout.flush
while (c = stdin.getc) != 'R'
res << c if c
end
end
m = res.match /(?<row>\d+);(?<column>\d+)/
{ row: Integer(m[:row]), column: Integer(m[:column]) }
end
end
end
puts Cursor.pos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment