Skip to content

Instantly share code, notes, and snippets.

@emdeeeks
Forked from KINGSABRI/cursor.rb
Created April 29, 2020 23:57
Show Gist options
  • Save emdeeeks/6b0df97510022970a9b97a931a171f80 to your computer and use it in GitHub Desktop.
Save emdeeeks/6b0df97510022970a9b97a931a171f80 to your computer and use it in GitHub Desktop.
Controlling Terminal Cursor in Ruby
class String
def mv_up(n=1)
cursor(self, "\033[#{n}A")
end
def mv_down(n=1)
cursor(self, "\033[#{n}B")
end
def mv_fw(n=1)
cursor(self, "\033[#{n}C")
end
def mv_bw(n=1)
cursor(self, "\033[#{n}D")
end
def cls_upline
cursor(self, "\e[K")
end
def cls
# cursor(self, "\033[2J")
cursor(self, "\e[H\e[2J")
end
def save_position
cursor(self, "\033[s")
end
def restore_position
cursor(self, "\033[u")
end
def cursor(text, position)
"\r#{position}#{text}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment