Skip to content

Instantly share code, notes, and snippets.

@hx
Created November 5, 2015 02:58
Show Gist options
  • Save hx/9b7a0a89d90ec0f786df to your computer and use it in GitHub Desktop.
Save hx/9b7a0a89d90ec0f786df to your computer and use it in GitHub Desktop.
OS detection and TIOCGWINSZ in Ruby
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