Code for Concinnity

beautiful and elegant solutions


Some great bash command line tricks I learned lately

Many adopted from Peteris Krumins’ blog post

Display mounted file systems nicely

The main point here is really about the column command:

1
2
3
4
5
6
7
8
9
10
11
$ mount
/dev/root on / type ext3 (rw)
/proc on /proc type proc (rw)
/dev/mapper/lvmraid-home on /home type ext3 (rw,noatime)

$ mount | column -t
/dev/root                 on  /      type  ext3   (rw)
/proc                     on  /proc  type  proc   (rw)
/dev/mapper/lvmraid-home  on  /home  type  ext3   (rw,noatime)

# woot, now it's printing out nicely!

Repeat arguments of the most recent command

Alt + .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ echo hello world
hello world

$ echo (Alt + .) # this becomes...
$ echo world

# Cool, but what if I wanted "hello"?

$ echo hello world
hello world

$ echo (Alt + 1)(Alt + .)
$ echo hello # here you go!

# Doing the same thing with history expansion
$ echo hello world
$ echo !!:1
echo hello
hello

# There's a shorthand for the last argument in history expansion
$ echo hello world
$ echo !$
echo world
world

Edit the whole command line in $EDITOR

This one is extremely huge. Ever got tired of doing those multiple lines long ffmpeg command lines? Here’s your salvation (and his name is vi)

1
2
$ ffmpeg -i my-input-file.avi <Ctrl+x><Ctrl+e>
# You can specify multiple command lines in your $EDITOR and they'll be executed one by one, cool!
Published by kizzx2, on April 22nd, 2010 at 2:34 am. Filled under: Interesting things Tags: , , No Comments

Firefox vs. Chrome — is Mozilla going down?

Just read an article on Tom’s Hardware: Why Mozilla Needs to go into Survival Mode. Woflgang Gruener said that “Google’s royalties account for 80-90% of Mozilla’s entire revenues.” If that’s true, then when Google’s got their own browser, Mozilla would surely go down.

Since it’s launch last year, Chrome’s been iterating extremely rapidly. Chrome v5 is about to launch, that’s five freaking versions in a year! Firefox on the other hard, pretty much (seemingly) got stuck since 3.6. This is probably the weakness of open source software — changes are so damn slow!

I would be very sad to see Firefox go down, because the market really needs more open source sofware instead of giving more personal information to Google.

Strangely enough, my personally experience with Chrome isn’t particularly spectacular. Yes, it launches (way) faster than Firefox and I use it in my eee PC 1000H, but it’s not that much more stable. Ironically with its tab-process separation thing, I’ve had Chrome crashed on me several times bringing down the whole system. Another thing is that I’ve never managed to install any extensions to Chrome, every time I bothered to try, it crashed and would possibly bring my system together with it.

Published by kizzx2, on April 19th, 2010 at 1:13 am. Filled under: Interesting things Tags: , , , , No Comments

Iterating Bash arrays with spaces

The problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/sh

$ a=(hello world foo bar)
$ for i in #{a[*]}; do echo $i; done

# Expected output:
#   hello
#   world
#   foo
#   bar

# so far so good:
#   hello
#   world
#   foo
#   bar

$ a=("hello world" "foo bar")
$ for i in #{a[*]}; do echo $i; done

# Expected output:
#   hello world
#   foo bar

# omg:
#   hello
#   world
#   foo
#   bar

The problem is caused by the affect that Bash uses space as array element separator internally.

Failed attempt

1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh

$ a=("hello world" "foo bar")
$ for i in "#{a[*]}"; do echo $i; done

# Expected output:
#   hello world
#   foo bar

# zomg!
#   hello world foo bar

The solution

The solution lies in the magical $@ expansion. When the $@ expansion is put in a quote, the shell automatically expands each element properly quoted:

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/sh

a=("hello world" "foo bar")
for i in "#{a[@]}"; do echo $i; done

# Expected output:
#   hello world
#   foo bar

# yay!
#   hello world
#   foo bar
Published by kizzx2, on April 10th, 2010 at 1:27 am. Filled under: Useful tips Tags: , , , No Comments

Muxing audio and video with ffmpeg

ffmpeg is a great codec converter, but it’s wide array of options is really daunting. I just figured out it can also be used to mux video and audio together into a file, here’s how to do it

1
2
3
4
5
6
# Simple example: mux an audio with a video file without audio track
$ ffmpeg -i audio.mp3 -i video.avi -acodec copy -vcodec copy output.avi

# Daily usage example: mux an audio with a video file _with_ an existing audio track.
# This will replace the AVI file's audio track with the MP3
$ ffmpeg -i audio.mp3 -i video-with-audio.avi -acodec copy -vcodec copy output.avi -map 0.0 -map 1.0

The key to the second command is the -map parameter. Typically, the output file would contain two streams: one audio and one video. The numbers 0.0 and 1.0 refers to the first input file and the second input file respectively.

The -map parameter is used to spell out the streams. What the above said was “use input stream 0.0 for your first output stream (which is an audio stream) and input stream 1.0 for your second output stream (which is the video stream)”.

You can see a list of stream numbers by

1
$ ffmpeg -i audio.mp3 -i video-with-audio.avi

You can add additional streams to the output file with the -newaudio and -newvideo parameters. Two audio streams make sense when you’re making a DVD rip with two sound tracks. I didn’t try it myself but it’s nice to know :P

Cheers :)

Published by kizzx2, on April 8th, 2010 at 6:10 pm. Filled under: Useful tips Tags: , , 4 Comments

Vrapper: open source vim plugin for eclipse

Found out Vrapper recently. The fact that it is open source deserves a lot of merits. For years eclipse users only had the commercial option ViPlugin which didn’t work quite well for something that charges money, considering the NetBeans folks have the excellent jVi which is also open source.

Considering eclipse is free and open source, Vrapper only seems more fitting than ViPlugin. I’ve tried it for a week and it seems most essential functions are there, and I’ve been using vi for a couple of years so I think my set of “essential functions” shouldn’t be too narrow :p

Published by kizzx2, on April 8th, 2010 at 5:49 pm. Filled under: Useful tips Tags: , , , , , No Comments

Google’s SketchUp vs Autodesk’s AutoCAD

I just used Google’s SketchUp for some quick personal project, and I was just wondering: “This is a very powerful piece of software, and it’s available for free! What’s the catch?”

I mean, there’re “commercial grade” software packages out there like Autodesk’s famous AutoCAD. How does SketchUP stack up against AutoCAD?

The information was surprisingly scarce, so I decided to collect my findings (mainly from forums) into this post:

What’s missing in SketchUp

SketchUp handles large drawings poorly

It’s been quite a well known fact that SketchUp slows down to a hog when you load a drawing with many poly counts. This effective puts SketchUp to the “sketch” category instead of a professional drawing tool.

From my experience, SketchUp’s definition of “large drawings” may actually be way smaller than you might think. For instance, importing a typical “house” sketch from the Google 3D Warehouse with a garden and a car will actually stretch SketchUp quite far to its limits.

Also, from my own experience, SketchUp as a whole is a less stable piece of software, probably because it’s new (and probably because it’s written in Ruby).

Create complex animations

You could do it with Podium’s Animate, which is a plugin to SketchUp. But it works by creating new scene for every frame — you get the idea. SketchUp’s scene based animation is only useful for simple presentation (which suits its intended purpose).

Efficient user interface

In AutoCAD you could access commands by typing it in the command line. For example I can type rect to access the rectangle tool; ext to use extrude. Whereas I have to move my left hand all the way to the P key just to use the Push Up/Down tool.

Where SketchUp is actually better than AutoCAD

Google 3D Warehouse

This is probably THE most winning feature of SketchUp. I mean, the major selling point of SketchUp is to be able to do rapid prototyping, but with practice, AutoCAD masters can probably dish out sketches as fast as SketchUp.

The 3D Warehouse is actually where SketchUp users will gain a huge edge in productivity. After several years of release, the warehouse covers an amazingly comprehensive array of objects ready to be imported to SketchUp without opening the Web browser (well, it does open an internal browser inside SketchUp).

Ruby script

SketchUp supports scripts with Ruby, so I don’t have to learn another new scripting language like I have to in AutoCAD. It looks like AutoCAD supports scripting in Ruby with IronRuby, but that’s probably not as fluid as using a native, standard scripting language.

Common myth: SketchUp’s looks and feels aren’t professional

SketchUp seems to give you a really fast and easy to use tool to do some visualization, but some people may think that SketchUp’s look and feel simply don’t look professional for a presentation. While it doesn’t come with any decent renderer out of the box, the image quality is pretty much dependent on the renderer, not the modelling software itself. Look at this render by VRay for SketchUp made with SketchUp:

A render by VRay for SketchUp

Free renderer Kerkythea for SketchUp also gives satisfactory results:

A render by Kerkythea for SketchUp

Verdict

I think what it really comes down to is your budget for money and time investment. SketchUp is free and very quick to pick up, but when it comes to large drawings or extreme details, professional packages like AutoCAD are still worth the money.

Published by kizzx2, on April 8th, 2010 at 5:45 pm. Filled under: Interesting things Tags: , , 2 Comments

Found a great programming blog

Peteris Krumins created a phenomenal blog at good coders, great reuse code. I found it so full of useful information and great posts that I’m going to go out of the way and do my share of Google “SEO voting” for it by linking to it.

It contains a lot more than just programming and is an excellent mind-food.

Published by kizzx2, on April 6th, 2010 at 2:33 pm. Filled under: Uncategorized Tags: , , , , No Comments

Installing HuffYUV on Windows 7 x64

This will probably also apply to other INF based installation for that matter. A simple command line saved me hours of trouble:

rundll32 C:\Windows\SysWOW64\setupapi.dll,InstallHinfSection DefaultInstall 0 C:\Path\To\huffyuv.inf

Run the above with admin privilege and bam, HuffYUV correctly appears in my codec list.

P.S. If you’re looking for a lossless codec, maybe take a look at Lagarith. It claims to be an improvement to HuffYUV and supports multi-processor encoding. It also comes with a dedicated installer so there’s no need to tinker with the command line just to install the codec.

Published by kizzx2, on April 5th, 2010 at 6:18 pm. Filled under: Useful tips Tags: , , , , 8 Comments