EDIT:
- specifically what worked for me was using the python script in this answer
- and I gave the tweaks I used to get it working for my autoloading functions in fishshell, and on ubuntu 20.04, in this comment on that answer
There are lot of commandline programs that apparently check whether their output is stdout a terminal or a pipe/file, and tweak what they output based on that. Those tweaks basically mainly being stripping out ansi-color-codes if the output is not stdout a terminal.
At least some of these programs have their own idiosyncratic configuration options to get them to still output ansi-color-codes, but is there any sort of completely program-agnostic way of doing it?
Like, something I could somehow quickly and simply "wrap around" arbitrarily any program I want the output from, in order to "fool" it into "thinking" its output was still just stdout a terminal?
Use-case example:
Say you want to take the output of eix
on Gentoo or apt search
on Ubuntu (both just search available packages by some regex),
and pipe the output into less
, but keeping the default color highlighting.
Or like, I have a shell function that takes ansi-color-code output, converts it to html (using the program "aha
"), and copies it into my clipboard.
eix
happens to have a flag --force-color
, but apt search
apparently doesn't have anything like that. (And grep
uses --color=always
, demonstrating the whole "even if a program has the option, how to use it will be idiosyncratic" thing.)
os.execvp(argv[1], argv[1:])
toos.execvp('fish', ('fish',) + tuple([ '-c', ' '.join(argv[1:]) ]))
so I can use my autoloading functions, and... that seems to work?) – Owen_AR Apr 03 '21 at 12:36stdin
/stderr
/stdout
, and the "std" is just a (synchronically arbitrary/meaningless) part of those names for historical reasons"? – Owen_AR Apr 20 '21 at 12:20File.save( "~/some_file" ){ |that_file| that_file.puts "text" }
, then thethat_file
name you chose to refer to the file within that block is probably underlying referring to file descriptor 3? (So presumably situations are very rare where a human would be working with references to any file descriptor of number 3 or above as that number itself, rather than by something like egthat_file
or whatever?) – Owen_AR Apr 22 '21 at 10:14set that_file ~/some_file ; echo "text" > $that_file
, eh? They're practically equivalent, but the ruby eg is telling it to write to file descriptor 3, whereas the fish eg is telling it to have file descriptor 2 refer to that file while executing the commandecho "text"
? (... if so, man that is... an extremely esoteric-feeling distinction.) – Owen_AR Apr 22 '21 at 10:32open()
call, and yes,that_file
is probably the file descriptor. It doesn't necessarily to be 3, the language's runtime may assign different number higher than 2, therefore we never work with explicit numbers (as they are useless before the file descriptor is actually opened), but with values obtained fromopen()
call. As for your second question,>
in shell, should redirect stdout, ie. the file descriptor 1, and assign it to$that_file
. I tried to show it explicitly on my drawing :) – raj Apr 22 '21 at 11:00echo
command in your fish example (better change it to actual program likels
) still writes to FD 1 and it doesn't know where's actually writing to. In case of your Ruby example, the program explicitly opens a named file, assigns to it a new file descriptor (besides the standard three 0, 1, 2 which are not touched; they're still opened and can be used), and writes to it (blue line on my pic). – raj Apr 22 '21 at 11:06$that_file
holds the file name, not the file descriptor. An equivalent example in Ruby would be to create a program that contains onlyputs "text"
and then run it using the command:program > $that_file
. – raj Apr 22 '21 at 11:08/dev/stdout
(itself?) would end up doing exactly the same thing as eg2.1?? (ie, rather than directly doing fd1->file, I expected it would do fd1->fd1->file (ie, ending up in the same place). But instead it results in going to the terminal). – Owen_AR Apr 22 '21 at 11:46echo
was a fish builtin covering up the actual program at/bin/echo
... but that doesn't make a difference here, right? (eg3 shows it doesn't, right?)) – Owen_AR Apr 22 '21 at 11:46File.read( path_to_some_file )
would be a blue arrow coming from "specific file named by the program, no redirection", eh? – Owen_AR Apr 22 '21 at 11:46/dev/stdout
is performed before everything is redirected totempfile.txt
so at this time/dev/stdout
is still connected to the terminal. So when redirection totempfile.txt
occurs, a new duplicate file descriptor is indirectly created that remains connected to a terminal. This is tricky programming. I would rather redirect to/dev/tty
if I wanted to write explicitly to terminal. As for eg4 and the Ruby read example, you are correct :) – raj Apr 22 '21 at 15:09