Last active
October 6, 2024 08:06
-
-
Save egwspiti/593e13664071e35385ac to your computer and use it in GitHub Desktop.
pure ruby get terminal cursor position
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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