Created
November 5, 2015 02:58
-
-
Save hx/9b7a0a89d90ec0f786df to your computer and use it in GitHub Desktop.
OS detection and TIOCGWINSZ in Ruby
This file contains 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
module OS | |
class << self | |
def windows? | |
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil | |
end | |
def mac? | |
(/darwin/ =~ RUBY_PLATFORM) != nil | |
end | |
def unix? | |
!windows? | |
end | |
def linux? | |
unix? and not mac? | |
end | |
end | |
# Running this C code tells you what TIOCGWINSZ is on your platform: | |
# | |
# #include <sys/ioctl.h> | |
# #include <stdio.h> | |
# #include <unistd.h> | |
# | |
# int main(int argc, char *argv[]) | |
# { | |
# printf("0x%x\n", (int) TIOCGWINSZ); | |
# return 0; | |
# } | |
TIOCGWINSZ = | |
if mac? | |
0x40087468 | |
elsif windows? | |
0x5401 | |
else | |
0x5413 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment