filetype and corresponding program that will honor control characters - node.js

I am using Node.js to write to log files, using the colors module which I believe inserts control characters into strings, for coloring/text formatting which will display in a terminal application.
When I write to the terminal directly, it shows colors, but when I write to a .log file and then tail the log file with either Terminal.app or iterm2, it does not show colors/text formatting. Does anybody know why this is? My guess is that when you write to the log file the control characters don't get saved? In that way, when tailing they won't display at all?
Perhaps if I write to .txt file or some other type of file, the control characters will remain?
How does this work exactly? At some point the control characters are getting stripped or ignored and I am not sure how or when.

See this code.
It checks if the output is going to a terminal (by checking process.stdout.isTTY) or to somewhere else, like a file. If the latter, no color codes are outputted.

Related

Change default text color in Bash/Zsh

What I want to do is to set the default color (like if output \e[0m) for EVERY output. For some reasons I can’t change it in terminal settings and only hope that Bash can
One way I think it might be possible is to redirect output to temporary file (or file descriptor, or function if possible) and print everything char-by-char or something and removing the reset sequence, but I’m not sure it’s the best way
So can Bash or Zsh do it quickly?

Hooking Kernel sys_read() Not Affecting Text Editors

So, I've been doing a little kernel module programming and I have a working module installed that screens text files with a certain name and replaces any occurrence of a word with another. I do this by tracking the files I want in the module through hooking sys_open() and then I do the rewrite in my hook of sys_read().
However, the effect is only seen when I cat the file (or maybe use awk or print from bash), but opening the screened file in a text editor just displays the unfiltered text.
My question is, why doesn't hooking sys_read() affect the output of text editors? I have tried: vi, vim, gedit and nano. Are they obtaining the file contents in another way? I know it is calling sys_read() because my printk debug message appears in dmesg, but maybe it is throwing away the read buffer and using another technique?
Just wondering what is happening.
It could be that your text editor is using a separate temporary file when you are editing the file and only saving it under the real name while closing/exiting the editor.

how to print file containing text with ANSI escapes

I would like to print a file containing text with ANSI escapes.
Here is file content (generated with bash script):
\033[1m bold message example \033[0m
normal font message
When printing file to screen in terminal, it works nice:
cat example.txt
shows:
bold message example
normal font message
But my problem when I try to send it to a printer:
lp example.txt
prints:
1mbold message example2m
normal font message
Is there a way to print this file correctly ? Maybe with groff (can be used to print a styled man page), but I did not manage to get anything efficient with it...
Maybe a2ps might be able to handle that (but I am not sure, you should try).
And I would rather suggest changing the way you get such a file with ANSI escapes (that is, also provide some alternative output format).
I mean that the program producing such a file (or such an output) could instead produce a more printable output (perhaps by generating some intermediate form, e.g. LaTeX, or Lout, or groff or HTML format, then forking the appropriate command to print it. That program could also generate directly PDF thru libharu or poppler, etc....)
Also, it might depend upon your printer, and the driver.

How do I view a log file generated by screen (screenlog.0)

So I just found out I can create log files of everything I do in screen (C-a H). Sounds like a nice way to keep track of potential goofs in a particular screen session. However, when I went to try it out the logfile is reported as being a binary file (and can't be viewed like a regular text as such). So am I missing something? A quick man page looksee and searching Google (and SO) turns up nothing about this.
So my question is: How do I generate plain text log files in screen?
Assuming the answer is "What a noob... how about you try making them? RTFM." my question becomes: How do I use less to view screen logfiles I've created (since less screenlog.0 does not work on a binary file)?
EDIT: So cat works fine but less complains that the file is binary... why?
SOLUTION: as jcomeau_ictx helpfully pointed out, you can view these logfiles fine with cat or more but with less you must add the -r flag less -r screenlog.0
I just found a screenlog.0 on the net; it is plain text, with some escape sequences. Just 'cat' the file, you should be able to view it just fine.
[after more checking]
Control-A H is what generates the screenlog on my system. And though 'cat' works, you'll miss a lot of data. Use 'more' instead of 'less' to interpolate the escape codes.
I found neither less nor more nor cat to be an ideal solution for viewing screenlog files. All "replay" some of the control character so that e.g. screen deletions as produced by "clear" (don't remember the corresponding control character) are beeing shown, hiding what has been cleared.
What i know works great is: use "view" or "vi", it just shows the control character in escaped notation. Probably any other text editor works, too (not tested).
-L logs to file,
tail -f 'logfilename' to monitor this file

Auto format a file to print in Vim

Sometimes I work with a file that contains source code, columns, plain text, sometimes all 3. It looks great on the screen. However, when I send it to a printer, it comes out a mess: columns/tables are misalignment, code looks like a spaghetti, etc.
I use Vim (7.2). How do I reformat the file to please the printer?
Perhaps I should shorten the length of a line?
How do you send it to printer? Try :hardcopy command.
You can also lookup printing-related options printfont, printdevice, printoptions, etc.
See also printoptions and others on vimdoc.sourceforge.net
It's not a pure-Vim solution, but I've had good experiences with GNU a2ps for converting (relatively) poorly formatted text documents (a couple Project Gutenberg titles, to be specific) to a nice, printable pdf/postscript file.
If you aren't worried about having to have a one-step process with no intermediary between Vim and the printer, here is a fairly flexible strategy.
If you have a dark background in Vim with light foreground but would like to print black-on-white since white-on-black is great for terminals but not so great for printed media, try colorscheme shine. (Another nice colorscheme for this is Hemisu, which is superior to Shine for printing diffs.)
Use :TOhtml to convert the document to HTML.
Save the file and open it in your browser.
Open print preview from your browser and set up the window appropriately.
For example, I just printed a nearly 200 column file brought in from Vim this way, and it worked out fine (both in the sense of "well" and in the sense of "small print" :) because I was able to use Firefox's print preview to set the file to print in landscape mode.
Print the file from your browser.
I agree there is a problem. yim has 'formatoptions' to 'wrap' lines together the way you want and break lines at appropriate places ('linebreak', 'breakat') which would give you an elementary wysiwyg word-processing capability, except that it only works on the display and has no effect when sent off to print.

Resources