Identify what terminal a ruby script is running in - ruby

I'm creating a gem that utilizes SGR codes. There are discrepancies between terminals that I've found myself. E.g. \e[2m makes faint text in Ubuntu's gnome terminal, but \e[2m does nothing in Windows 8.1's Command Prompt With Ruby. So my ruby program needs to know what terminal it is running in and from there, utilize (or not utilize) certain sgr codes.
Is there a magic bullet, in pure ruby or hopefully with a gem, for finding out what terminal we're running in? And I mean any terminal, no matter how exotic. I'd love be able to do this, depending on the terminal we're running my gem in (This is pseudo-code, MagicBullet is a class belonging to the gem I'm hoping you'll refer me to):
MagicBullet.identify #=> ['gnome-terminal', '6.3.2']
Or:
MagicBullet.identify #=> ['konsole', '4.5']
Or:
MagicBullet.identify #=> ['command-prompt', '13.37']
Or:
MagicBullet.identify #=> ['NeWT', '15.2']
Is there a a sure-fire way to find out regardless of OS...this information feels so readily available...maybe a gem that looks for a range of OS signals/processes that belong to a list of known terminals, something like that. I've tried ENV['TERM'], and while that returns :xterm in a gnome-terminal, I'd prefer it to return :gnome-terminal. In Command Prompt With Ruby it returns nil, which isn't very helpful considering all non-linux (I think) terminals will return nil. Of course, I could run if !ENV['TERM'] and then utilize a different set of logic to find the terminal's name, but that doesn't feel "sure fire" to me. It'll probably fail with other "exotic" terminals.
So I'd like, as an answer, either:
A gem that gives a sure-fire way to identify what terminal and what version we're running
A pure ruby way to give a sure-fire way to identify what terminal and what version we're running
An algorithm that gives me a pretty good shot at identifying what terminal and version we're running, and I'm talking all flavors of Linux, Sun Microsystems stuff, Apple's operating systems (Terminal and the third-party iTerm are two terminals for Apple's operating systems for example). Anything that MRI can be installed on.

Related

Vim and MacVim questions

Greetings fellow problem solvers.
I'll start by saying my level of expertise regarding coding/anything compsci is somewhere right above ZERO (intro to java last semester & currently teaching myself python, but that's it). Also I have a macbook air running 10.14.6(mojave). Ok, here are my questions.
What I did:
I installed macvim version 8.1.2234 before realizing my machine was shipped with vim 8.0.1365 preinstalled. I installed macvim using the download .dmg & "drag/drop to applications" method.
What I'd like to know:
Firstly, MacVim and Vim appear to be separate programs (when I type mvim -v as opposed to vim -v in the terminal, each one shows a different version number as described above). So, how can I remove/replace/update/overwrite or whatever, so that I only have either macvim or vim? I don't really care which one I have as long as I only have one
--> Before telling me to just rename an alias so that one maps to the other, please read the info below <--
I REALLY don't want two nearly identical programs on my computer because, a)it seems like a waste a space b)don't want the possibility of accidentally saving something to the wrong place, or calling something from the wrong place, but mostly c)so I can sleep at night.
That being said, do I NEED both? Does macvim rely on vim to work or do they both exist and work independently? Also does renaming an alias mean that the other will be replaced? Because I would assume that just renaming one would be a lot like having two houses, but making both driveways only go to one house.
Additional info: I have do homebrew but I didn't install macvim using homebrew. I found another post recommending to use (i don't remember exact syntax but something like..) $brew overwrite vim--with-macvim. So I thought great, I'll uninstall macvim since I haven't used it at all yet, and then I'll re-download using the homebrew overwrite method. But then I read that it's a little more complicated than moving the "Vim" folder to the trash, and after two unsuccessful attempts in the terminal that looked like this:
$brew uninstall mvim (throws no keg error)
$brew uninstall macvim (same error)
I decided to reach out for help before I break anything.
Thanks in advance for any info/advice, it is much appreciated!
I don’t have time for a longer explanation at the moment, but:
Breathe Easy
MacVim is nearly completely independent from (terminal) vim. They do share your vimrc, but MacVim also reads gvimrc, and uses its own runtime directories.
You’re not wasting much space, and it would be a Bad Idea™️ to mess with the system installation of some programs.

Ruby system call (on windows) without a popup command prompt

I am trying to tidy up a process that uses multiple system calls from inside a ruby script executed using rubyw.exe (1.8.7).
As far as I can understand the main reason for rubyw.exe is that it doesn't pop up a command prompt to distract the user. However it appears that the system calls from within that process still do generate these popups which is very distracting for the users of this process script.
Does anyone know how to do this?
There are lots of questions similar to this on SO but none which quite answers this, if I was using python this questions/answer would help (http://code.activestate.com/lists/python-list/46042/) but so far my searching hasn't found a way to accomplish this with ruby.
UPDATE: There is this thread here Hiding curl Window on Windows which is close but the only working solution there doesn't also allow you to get the standard output.
I ended up going with the win32-open3 gem. Because fork isn't implemented on windows systems you can't use the built in open3 and you'll need to do a
gem install win32-open3
However this comes with flags that you can pass to the call method, in particular
There are a couple of differences in the Windows version for open3
(which
also apply to Open4.popen4) - the mode flag and the show flag. For the
mode, you can specify either 't' (text, the default) or 'b' (binary) as a
second argument. For the show flag, you can specify either true or false,
which will show the console window, or not, depending on the value you
pass. The default is false.
Users of this particular script are no longer bombarded with 15 cmd windows that constantly steal focus, so .... win?

Intentionally cause a segfault under Ruby 1.8.7

What's the easiest way to cause a segfault intentionally in MRI Ruby 1.8.7?
You might check out segfault4r (it was last updated in 2007). You have to download and install the gem manually, as it's not available through RubyGems.
You could write a bad C module that de-references a NULL pointer.
Depending on your needs (and environment) you could get it running, find the interpreter's process ID and do:
$ kill -s SIGSEGV $pid
I just tried this on OSX with bash and it worked on an ed(1) (the one true text editor) process.
You could also run Ruby in a debugger and manually set a pointer or two to zero. One of the pointers inside a variable would do nicely. Of course, you'd probably need to have an interpreter built with debug symbols and you'd need to know a thing or two about the interpreter's internals.
If you're doing this against a specific patchlevel, look at Ruby's bug tracker for bugs that were reported after that patchlevel was created that cause a segmentation fault. As you're looking at 1.8.7, you'll probably want to look at those categorized as Ruby 1.8 .
For example, for the version of Ruby (1.9) I have on my Windows machine, Thread.kill(nil) is sufficient to cause a segfault, and I think it applies to Ruby 1.8 as well.

Is there any way to prevent ncurses based programs from running?

Hey there, I'm building a remote shell server that interfaces between a text-only client and a virtual shell.
It works perfectly when using regular shell commands, but the first thing that people try after that is vim, which promptly drives my server crazy and can't even be closed remotely.
Is there any way to detect ncurses based programs and prevent them from running in my special shell?
(the server is ruby, but any system command will do)
You can declare the capabilities your shell has, by setting the TERM environment variable to the correct value. For instance, if your shell has the same capabilities as the vt100 terminal, export TERM to the correct value, and programs like vim will respect that.
To run vim in vt100-mode, try:
TERM=vt100 vim
You could also try:
export TERM=dumb
The trick is to find a terminal that corresponds to the capabilities of what you are creating. There is a lot to choose from. On my system (Arch Linux) this gives me a long list of choices:
find /usr/share/terminfo
You might be able to find a terminal specification that corresponds to what your program can handle.
Alternatively, you may want to consider implementing terminal emulation for ansi or vt100:
http://en.wikipedia.org/wiki/ANSI_escape_code
http://www.termsys.demon.co.uk/vtansi.htm
Best of luck!

DOS window popping up when using IO.popen, etc

I'm working on a GUI for Windows XP. Everything works great, except when I run an external command through backticks, %x(), IO.popen, etc, I get a DOS window that pops up for a split second. I know this doesn't happen when I've developed on OS X and Linux. Any ideas on how to get rid of it? (Or at least hide it?)
I'm using rubyw 1.8.6 (the "GUI version" of Ruby for Windows) and GTK2 for the interface.
You can use the Win32API module, and call the windows api CreateProcess Function. It ain't pretty.
As mentioned in a comment, I never found a solution to this. In this particular circumstance, the information I needed was actually already stored on the filesystem (so I just read it in as a file). This wasn't immediately obvious before, and isn't likely to come up in all circumstances.
If anyone finds a "true" solution, I would be interested in hearing about it.

Resources