Terminal flashlight in Bash

This one-liner prints out a series of spaces in white background, long enough to fill up entire terminal screen size:

printf "\e[47m%$((COLUMNS*LINES))s\e[0m";read

It might be required to use 256 color code, in case of the default white color isn’t actually pure white:

printf "\e[38;5;15m%$((COLUMNS*LINES))s\e[0m";read

I know some people do use mobile phone as light source in dark whenever is needed, and there seems to be some applications for phones. It is nothing special about this line or how it can be done on a computer or whatever devices if that really matters, but I do wonder how short of the code could it be?

This one, 43 characters or 48, in Bash. And I honestly don’t think I would need to take a screenshot of white image. Besides, on a blog with white background, I probably could just leave a few blank lines and tell you that there is your screenshot like the following screenshot:















screenshot of the terminal flashlight

Using something I learned a while back, which prints out a separator line. With a small change or two, it does the job. A long line with spaces, color code prefixed and reset in the end, then holding for Enter.

Let there be light!

Idea: snowy static noise in terminal

Or white noise, no signal, signal lost, or whatever you call it. It’s a screen you see when nothing on television.

I was looking for one as screensaver in terminal about a week ago, but really couldn’t find any. Here is a small piece of Bash that kind of doing it in one-liner:

while sleep .1; echo -ne '\e[2J'; do for ((y = 1; y <= LINES; y++)); do for ((x = 1; x <= COLUMNS; x++)); do ((RANDOM > 30000)) && echo -ne "\e[$y;${x}H\e[1;37m.\e[0m"; done; done; done
https://lh4.googleusercontent.com/-kYPK1fWeTa8/VEDm09VQXYI/AAAAAAAAGuU/38_nYJC62P0/s800/static-noise.gif

Break down version:

while sleep .1; echo -ne '\e[2J'; do
  for ((y = 1; y <= LINES; y++)); do
    for ((x = 1; x <= COLUMNS; x++)); do
      ((RANDOM > 30000)) && \
      echo -ne "\e[$y;${x}H\e[1;37m.\e[0m"
    done
  done
done

Change 30000 to something else for the white dots probability.

This Bash implementation is slow, I think a C would be much better, but I just didn’t want to continue, so I blog this idea. If you know a program does just this, or you want to see this happening, or team up, shoot me a message.

PORTAGE_SYNC_STALE for stale portage tree warning

When I was trying to install a library for compilation, emerge told me the portage tree was 30 days old:

$ emerge -pvt1 log4cplus
 * Last emerge --sync was 30d 9h 12m 12s ago.
[snip]

This was the first time I ever noticed it. After grepping the source code of emerge in /usr/lib/portage/pym/_emerge/sync/old_tree_timestamp.py, I found where the message was produced:

92:  out.ewarn("Last emerge --sync was %s ago." % \
95:  out.ewarn(_("Last emerge --sync was %s.") % \

Of course, this is a built-in feature of emerge to remind user to update system. The default PORTAGE_SYNC_STALE variable is set to 30 days. From emerge(1):

The PORTAGE_SYNC_STALE variable configures warnings that are shown when emerge --sync has not been executed recently.

I have been trying to do the update every 30 days for a few months, I think this message proves that switching from the previous weekly update is a good decision.

There is no reason to update that often, it probably could only start to go bad after 30 days, and that date is just for reference like the dates for imitation cheeses, you can still eat them even way beyond one year of the date if you dare.

Whether you do it every day, three days, seven days, thirty days, even odd numbered dates, Fibonacci numbered dates, random dates, flipping the coins, or whatever you choose to update, as long as you can put a number into PORTAGE_SYNC_STALE, that might remind you to do the update, because it surely did to me. I thought the next update would be around 20-something this month, but nope, it’s 13th.

Okay, maybe only the cheese, but really don’t try that on Gentoo, 30 days, maybe two months at most.

Put a number in Bash prompt

I was thinking to take a screenshot of commands and outputs, a thought or a question come to me:

Is there anyway I can refer to a line of command without (re-)pasting?

If I have some simple and short reference points, it would be easier to write for me, and clearer to read for readers.

1   IPython Prompt Numbers

Immediately, I thought of IPython, even I don’t even have it installed nor do I use it, I probably only had used it a couple of times since the day I learned Python. Nevertheless, I always could tell the console of IPython.

IPython has something called Prompt Numbers, it would look like, from Introducing IPython:

In [1]: %timeit range(1000)
100000 loops, best of 3: 7.76 us per loop

In [2]: %%timeit x = range(10000)
   ...: max(x)
   ...:
1000 loops, best of 3: 223 us per loop

As you can see, there are [1] and [2], good and clear referencing points for writing.

2   Bash Command Number

In Bash prompt, there is Command Number (\#) for PS1 (also History Number \!), which is the number of current command of current session, for example:

PS1 $ bash
PS1 $ PS1='[\#] \$ '
[2] $ true
[3] $ false
[4] $

The problem is, perhaps my lack knowledge of session, I can’t find a way to reset a session, that is resetting the number. The only way I know is starting a new shell process.

3   Numbering with Arithmetic Expansion

The following simple prompt utilizing Arithmetic Expansion does the trick, you can easily reset the number [3] without starting a new shell:

PS1 $ bash
PS1 $ PS1='[$((++N))] \$ '
[1] $ true
[2] $ false
[3] $ N=0
[1] $ true
[2] $

If you want to produce a clear output for either a screenshot or pasting the text, mis-typing happens, although you still need to re-type, but if you don’t need to start a new shell, just a quick a reset the variable, that might be more preferable, at least for me.

The only drawback I am aware of is if you just press without entering anything, the Command Number would not be increased, but the method above would, which may not be you want if you are super picky.