• Home
  • Blogger
  • Github
  • Travel
  • The Tank Project
skip to main | skip to sidebar

Steven Occhipinti

A braindump.

Printing over previously printed characters and lines

I recently wrote small Ruby Gem that provides a command line interface to Melbourne's TramTracker service.

One main feature I wanted was to allow the script to poll TramTracker on a regular basis.
In the past, I used the command line utility watch to achieve this, but I couldn't find a way to enforce a "max iterations" option. If it gets forgotten about in the background, TramTracker will eventually block that IP address!

So I turned to Ruby. Now instead of printing the same block of text over and over again and filling up the terminal, I thought it would be better to just have that text overwrite itself in place.

In this post, I'll outline a couple of ways of achieving this.


Using the \b (backspace) character

Printing a \b character does the same thing as pressing backspace. This is really simple to use and is great for little "progress" spinners. For example, a character that cycles through '|', '/', '-', '\'. For example:
puts "foo\b\b\bbar"   # => bar
("|/-\\"*10).chars.each {|c| print c; sleep 1; print "\b"}  # => Cheesy, old-school spinner
There are two main shortfalls with this approach, one is that it only works on a single line and two is that if you want to overwrite lots of characters, you'd need just as many \b characters (you would probably introduce a loop).
puts "foo\n\b\b\bbar"  # => foo\nbar

Using the \r (carriage return) character

To avoid having to repeat just as many '\b' characters, a simple alternative would be to return the cursor to the start of the line and write over the top of the existing characters. For example:
100.times {|p| print "\rDownloading %#{p+1}..."; sleep 1}
This approach still won't work across multiple lines, but it has another subtle shortfall too, it doesn't clear the entire line. For example:
puts "foo\rp" # => poo
A common work around for this is to pad the end of the string with spaces, but there are better ways (keep reading ;)


Using curses

The most common answer to overwriting characters across multiple lines, is to use curses.
Curses allows you to pick exactly with character you want to update and makes menu driven Text User Interfaces (TUI's) easier to develop.
Ruby happens to have a curses module built in to its standard library, which provides a simple API, for example:

setpos(lines/2, cols/2) # Start in the middle
addstr("Hello world")

I created a more complete (simple) example of using curses in a gist here.

Curses is pretty good for this sort of stuff, it gives you a lot for free.
The thing that I didn't like about using the curses library was that when it initialises, it clears the screen.
This behaviour suits a TUI, and the old contents is still restored when you return, but I was being picky and didn't want that.


Using individual cursor movement characters

This last option is the most fundamental of the bunch. Terminals generally support characters that will allow you to move the cursor around at will and would be the basis of things like curses, although not as commonly used. Here are the most common characters to move the cursor around:
Move the cursor to line L, column C
\033[<L>;<C>H
Move the cursor up N lines
\033[<N>A
Move the cursor down N lines
\033[<N>B
Move the cursor forward N columns
\033[<N>C
Move the cursor backward N columns
\033[<N>D
Clear the screen, move to (0,0)
\033[2J
Erase to end of line
\033[K
You can read more about these "escape sequences" here.
I ended up using the '\033[K' ("Erase to end of line") and the '\033[A' ("Move the cursor up") characters in my TramTracker gem and it worked a treat!


Now go and make a cool retro user interface! :)


Posted by Steve at 01:27 0 comments
Labels: bash , linux , programming , ruby Email This BlogThis! Share to X Share to Facebook

"Safe mode" on the Samsung Galaxy S3

I recently had some trouble with my Samsung Galaxy S3. I'm still not sure what caused it, but quite frequently, it would completely freeze up, even after a fresh reboot. It may have been a dodgy app, or something deeper in android, but in the process of fixing it, I was introduced to "safe mode".

"Safe mode" here behaves like safe mode in Windows. It basically lets you use your phone just like normal, but without starting the usual plethora of background processes. You can still get to your settings, apps and files, but things won't start automatically.

To enter safe mode on the Galaxy S3 is really simple:

  1. Restart the phone
  2. While it is booting up (and you see the Samsung logo) simply hold the volume down button


When it boots, you should have the stock wallpaper, your shortcuts will be gone and you should see a little watermark in the bottom left corner that says "Safe mode".



From here you can do everything like normal, browse the net, make calls, etc. but more importantly, you can uninstall dodgy apps, change settings, even factory reset the device.

Posted by Steve at 14:36 0 comments
Labels: android , Samsung Galaxy S3 , troubleshooting Email This BlogThis! Share to X Share to Facebook

How to make tomato sauce

This time of year is "tomato season" and it's an Italian tradition of ours to spend a weekend together and make enough tomato sauce to last us the year. Of course, everyone does this differently, but here I'll outline how our family does it.

1. Buy some "sauce tomatoes"

If you go to any suburbs that have an Italian community you'll most probably see signs for people selling "sauce tomatoes". Sauce tomatoes are normally more ripe than usual, which makes them ideal to turn into sauce. We bought 16 boxes of tomatoes from the local Italian social club.


2. Wash and cut the tomatoes

This part is where most of the effort is spent!
We use large plastic tubs, fill them with two thirds water and empty some tomatoes in. We then pull out a tomato one by one, cut it in half and then cut the hard stem out from the middle, putting all the stems in the compost bin and the washed, cut tomatoes in another bucket. This makes it easier to mince later, but also gives us a chance to inspect the tomato to ensure the inside isn't rotten.
Rotten tomatoes are easy to spot, they are either black on the inside or they stink! If ever you're not sure, giving it a smell is normally the easiest way to tell - and "When in doubt, throw it out".


3. Pre-boil them if they are firm

"Sauce tomatoes" are normally very ripe and soft, but sometimes they are quite firm. If this is the case they won't go through the mincer as well. So to help with this, we used a couple of large drums filled with water on a flame to lightly boil them a little. The idea is to only soften them up, so once the skin starts peeling off, you can pull them out of the water and drain them to remove as much as the water as possible (otherwise the sauce will be watery).

Pre-boiling the tomatoes
4. Mince the tomatoes

Now that the tomatoes are soft, washed and cut they can be minced. We scoop out some tomatoes and put them in the mincer. The mincer will separate the tomato sauce from the skin and the seeds. We generally fill one tub at a time.

The machine!
Separating the skin, seeds, etc. from the sauce
Gently pushing the tomatoes down in to the mincer
The left over skin, seeds, etc. from the mincer
5. Second mince

Once you have a (mostly) full tub of sauce, we actually put the left over skin and seed back through one more time and mix it with the rest of the sauce in the tub. This produces a thicker consistency and really gets the most out of the tomatoes. Be sure to mix the tub up well, otherwise the consistency will not be evenly spread.

The second run - putting the left overs back through one more time
6. Bottling the sauce

This year we used three different types of bottles: The usual large beer bottles with bottle caps, large jars with screw on lids and for the first time we tried using a few small (crown lager) beer bottles.
The first step is to put a spoon of salt and two basil leaves in each bottle, then fill the bottles with the sauce, leaving at least 5cm of an air gap from the top of the bottle.
Throughout the year, we collect up the beer bottles so we can re-use them for the next year. Be careful of this though, the bottles shouldn't be reused for more than about three times or so. Every time they are boiled, the bottles get weaker, so if they are reused too many times, they will eventually shatter when they are boiled.

Filling the bottles with the sauce
The beer bottles we use for the sauce
The jars we use for the sauce
7. Cap the bottles

The jars with the screw on lids are the most risky. They need to be sealed tightly, otherwise they will not preserve correctly and will make a mess in the final stages. The beer bottles on the other hand are more reliable. You can buy bags of new bottle caps fairly cheaply and then using a "capper", they normally seal quite well. Just be sure to check they are tight as you go.

The 'capper'

8. Boil the bottles

After the sauce bottles are all sealed and ready to go, they are gently placed in a drum where they are boiled. We generally put a blanket in the bottom of the barrel, then a layer of bottles standing upright, then another blanket, another layer of bottles, etc. Then fill the drum with water and turn the flame on. As this is a large volume to heat up, it takes quite a while to start to boil, but once the boiling and/or steaming has started, we keep the flame on for another two hours. It's very important that once they have boiled for two hours that you leave them in the water until the water goes cold - this could be a whole day. Taking them out too early can cause the bottles to shatter.
This process is what preserves and seals the bottles properly.

Boiling the bottles to seal and preserve them
9. Enjoy!

After the water has gone cold, the bottles can be pulled out and put away. We try to make enough to last us a whole year, so we put them into crates, keep one crate in the kitchen and store the rest of them in a cool place.
When you want to use the sauce, it still needs to be cooked, so you can experiment with different recipes at the time - you can add your herbs, spices, wine, etc. and let it cook for anywhere between half an hour and two hours.

Now you know exactly what goes into your sauce, no preservatives and in my opinion, it tastes so much better than the ready made sauces from the shops that you'll never want to buy that stuff again!
Posted by Steve at 23:13 0 comments
Email This BlogThis! Share to X Share to Facebook

Script to transfer photos in linux

Every time I go on holiday, I end up writing the same bash script! ... I know, how nerdy is that, I code on holidays.

I really should save the script and bring it with me each time, instead I just re-invent the wheel everytime, so I thought I would write a quick post to record it for next time.
Luckily it's a really simple script to write, all it does is move the photos from my camera's memory card to the laptop HDD, but it puts them in a sub-directory for that particular day, then sorts the files into a 'jpg' or 'raw' sub-directory.

The script is quite dumb, it just creates a directory for the current date. On one of the trips, I had the script pull the date the photo was taken from the EXIF information, but when a series of photos goes over mid-night, it splits them up and I didn't really like that.
Posted by Steve at 03:38 0 comments
Labels: bash , photography Email This BlogThis! Share to X Share to Facebook
Newer Posts Older Posts Home

Blog Archive

  • ▼  2013 (4)
    • ▼  June (1)
      • Printing over previously printed characters and lines
    • ►  May (1)
      • "Safe mode" on the Samsung Galaxy S3
    • ►  March (1)
      • How to make tomato sauce
    • ►  January (1)
      • Script to transfer photos in linux
  • ►  2012 (17)
    • ►  December (1)
    • ►  October (1)
    • ►  September (2)
    • ►  August (2)
    • ►  July (1)
    • ►  May (2)
    • ►  April (2)
    • ►  March (3)
    • ►  February (3)
  • ►  2011 (33)
    • ►  December (1)
    • ►  November (3)
    • ►  October (3)
    • ►  September (4)
    • ►  August (3)
    • ►  July (4)
    • ►  June (6)
    • ►  May (6)
    • ►  April (3)

Labels

android (5) apache (1) arch linux (1) arduino (1) bash (11) calendar (1) compiz (1) design (1) diff (1) email (1) gimp (1) git (3) github (2) gnome3 (1) Google (2) hacking (1) hardware (4) howto (1) htpc (1) java (1) lamp (1) linux (28) Mac (2) minecraft (2) mysql (1) netduino (1) nfs (1) parallel port (1) patch (1) photography (4) php (1) pidgin (2) printer (1) programming (6) python (1) rails (1) regex (5) review (3) ruby (3) Samsung Galaxy S3 (2) Samsung Series 9 (1) security (1) sed (3) ssh (1) sudo (1) tank (2) Toshiba Portege (1) troubleshooting (1) ubuntu (16) ui (2) unity (2) vim (5) webcam (1) websites (3) xbmc (1) xclip (1) xul (1)

Total Pageviews

Sparkline
 
Copyright (c) 2010 Steven Occhipinti. Designed by Conveyancing
High Deductible Health Insurance, Purchase Beats