The Accidental Rubyist

invalid byte sequence in UTF-8

Archive for May 2010

CSV file reading in ruby 1.9 (fastercsv)

with one comment

Fastercsv, which is the default in ruby 1.9, uses a hash for specifying the column separator.
Here, we read a tab delimited file and push each row into an array. We also print the first 2 columns.


require ‘csv’

ctr = 0
@data = []
CSV.foreach(@file,:col_sep => “\t”) do |row|
@data << row
puts ” #{row[0]} | #{row[1]} ” #unless row[1] =~ /^\[x\]/
ctr += 1
end
puts ” #{ctr} rows “

To see the other options, do ri CSV::new in a Terminal. Some options are row_sep, field_size_limit, converters, headers (:first_row means the first row is treated as a header, if set to array then the array is used as a header), skip_blanks (true), force_quotes (true), write_headers (true), and a few others.


# git patching
URL
— Nick Quaranto

Dude, that’s nothing. I used to work across the hall from Linus Torvalds, and he used to pull down his pants and shake his balls at passers-by through his office window.
— barfolomew

Advertisements

Written by totalrecall

May 28, 2010 at 12:43 pm

Posted in ruby

A vim-like listbox in rbcurse (ruby ncurses)

leave a comment »

Lots of terminal application users like applications to use vim-like keys. So I’ve played around with the Listbox of rbcurse, and added some mappings to give it a very vim (and emacsy) feel. The result is on github (master19 branch) as examples/testlistbox.rb.

What did i do:

1. require "rbcurse/vieditable"

This adds a whole lots of vim like bindings to lists. Since, this file is shared by textareas and lists, I’ve put in some separate mappings for listboxes in there. This allows us to use “gg” “G”, “dd”, o, O, j,k and plenty of other keys.

Note than normally entering into a cell of a listbox makes it editable in-place. Exiting saves the value. But being terminal freaks, we don’t like that behavior ! We want keys to be used for navigation and search etc. So for changing contents I can press a “C”. And edit box at the bottom of the screen (ugh! reminds you of alpine, doesn’t it). Similarly, to add a row above or below the present you can press “o” or “O”.

Please note, to keep the library and my life simple, delete is “dd”, So to delete 5 rows its “5dd” not “5d”, To yank 5 rows it is “5yy” not “5y”. Maybe in the future, I’ll look into this.

2. $catch_alt_digits = true; # emacs like alt-1..9 numeric arguments

For the emacs crowd. You like to use C-u for numeric arguments. In a rbcurse app, C-u no longer does an undo, its always used for numeric arguments, each successive press of C-u uses a range: 4, 16, 64 etc (I’ve detailed this in a previous post)

However. catch_alt_digits allows us to use numeric arguments like Alt-9 9 for 99. To go down 99 rows, an emacs guy can do Alt-99 j. Of course, vim guys can just do “99j”.
The advantage of catch_alt_digits is that it can be used in editable boxes too, such as textareas.

3. cell_editing_allowed false

In order that keys can be mapped to functions or blocks, I’ve disabled cell_editing. If I did not, then j or k would insert a j or k in the box.

4. listb.one_key_selection = false # this allows us to map keys to methods

By default, if cell_editing is false, typing a key takes us to the first item that starts with that key. That’s pretty friendly behavior, but it does not allow us to map keys. So setting this as false, means we can map keys. Now use “f” to go to the first row, as “fn” or “f <char>”.

5. The mappings. Rather than put the mappings in the sample file, I’ve added them to the vieditable.rb file, so they may be reused. So lets call that method.

listb.vieditable_init_listbox

6. We want to have infinite linear undo and redo. So let’s instantiate our undo manager and give it our list.

undom = SimpleUndo.new listb

Now we can use “u” and “C-r” for undo and redo. The emacs key “C-_” can also be used. You can do single or multi-row deletes and undo. “12dd”, “u”. For this don’t forget to require the undo manager at the top.

require 'rbcurse/undomanager'

That’s about it. However, there’s more in the mappings. I’ve adopted emacs’s kill-ring concept. So “yy” does a kill-ring-save. “p” and “P” of course do a paste (emacs calls it a yank, for vimmers a yank is a copy). For emacsers, M-w does a kill-ring-save, C-y does a yank (paste), and M-y does a yank-pop. Again, this is explained in a previous post, but it helps to save multiple rows into a ring, and select which one one wishes to paste. We first have to type C-y to paste the last copy, and then M-y to cycle through previous copies. I think this gives the best of both worlds without a vimmer having to change his habit of yy and p/P.

This sample is not included in the rbcurse gem 1.1.3. Fetch it from github, it will be included in 1.1.4 when i release it shortly.

Written by totalrecall

May 24, 2010 at 11:47 am

Posted in ncurses, rbcurse, ruby

Extended features for tables (ncurses)

with 12 comments

Instead of cluttering up Table, or expecting a user to hack such functions, I am putting some stuff in tableextended.rb. One may include this after opening Table in one’s app. As may be seen in viewtodo.rb, one may bind a key to increasing or decreasing a column’s width. This function checks of numeric arguments so one may do: 15+ or 20- instead of pressing the key 20 times.

class Table
    # so we can increase and decrease column width using keys
    include TableExtended
end

After creating a table, I can do:


atable.configure() do
    bind_key(?+) {
        # this automatically takes care of numeric arguments such as 12+, 22+ etc
        atable.increase_column
    }
    bind_key(?-) {
        # this automatically takes care of numeric arguments such as 12-, 22- etc
        atable.decrease_column
    }
end

The advantage of doing this, as against 20 times pressing + or -, is that i can now distribute the 20 across the other columns. Earlier if a user pressed + 20 times, i simply had no way of knowing how to distribute each increase or decrease.
Please see master19 branch on github.

Written by totalrecall

May 19, 2010 at 11:42 am

Posted in ncurses, rbcurse, ruby

ffmpeg – output buffer too small – solved

with one comment

Lame 3.98 is the problem.

Go to macports, search for lame, go to revision history and select the portfile for 3.97b2 (click on @16761).
Download the portfile clicking on Original Format (bottom of page).

Preferably, put the portfile in a folder. cd to the folder and run “sudo port install

Now port deactivate the 3.98 port and port activate the @3.97b2_0 one.

Now run the ffmpeg command and you should not get the output buffer small error.

(In my case, I was using ffmpeg to extract mp3 from flv. The mp3 was usable from iTunes but *not* from iPod shuffle).

Written by totalrecall

May 17, 2010 at 12:29 pm

Posted in unix

Perils of gem creation and release

leave a comment »

Seems every now and then someone comes up with a new way of creating a gem. However, these ways get obsolete very fast, so it can be frustrating. e.g. gemcutter’s gem tumble command is outdated, by the way. Or was it migrate (sorry cant remember), why not remove it, so squeamish people like me don’t squeam.

Every time I am releasing a new version of my gem, I start getting errors from hoe since they’ve changed their syntax. This time round, I was just unable to get it to work — some parameter mismatch.

A site I just looked at points me to the Gemspec Reference, but that doesn’t seem to give a sample. Hoe was giving errors since it keeps changing and the new suggested syntax is now suddenly borking. So i’ve gone ahead with Jeweler.

Jeweler gives me this error:
Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com
Okay, so here goes:
sudo gem install technicalpickles-jeweler -s http://gems.github.com
Password:
ERROR: Error installing technicalpickles-jeweler:
technicalpickles-jeweler requires git (>= 1.2.1, runtime)

So i thought, lets just try jeweler itself …

$ sudo gem install jeweler
Successfully installed git-1.2.5
Successfully installed jeweler-1.4.0
2 gems installed

Phew. So now I can just create a gemspec using “rake gemspec” and then “rake build” to build it. I hope i won’t have to keep increment version numbers in gemspecs and the ruby file. version_bump should do the trick. I hope by the time I’m ready for a new release, I won;t have to rewrite my Rakefile. The good thing is that my gemspec does not have a dependency with Jeweler (it did with hoe). Sorry for the rant !

Written by totalrecall

May 8, 2010 at 7:57 pm

Posted in ruby

rbcurse 1.1.2 released

with 2 comments

Ha ! Writing the tutorial helped bring out a typo in listbox which prevented numeric arguments to vim keys from working. So in 5k, 5j the 5 was getting ignored.
Tiny error. 1.1.2 is on gemcutter.org.

So I guess its just as well. The other day, I came across charva which is a ncurses package for Java. I think i saw this 6 years back or more when i used to work on Java. You can write a program in Swing and run it in charva just replacing java with charva. However, although I think its a great accomplishment (not used it, btw), the programmer has duplicated the GUI way of working in ncurses. In rbcurse, I am trying to go away from GUI metaphors and ways of working. In this age, people who would like to use ncurses/terminal, would probably like vim or emacs like keys, and a lot of power. That’s what I’ve been trying to look at in the last few changes made, and last few features added. Otherwise, rbcurse will just be a very poor second cousin to the GUI.

We’ve got to think of very different widgets/ components, so that users can work a lot faster in rbcurse than they would in a GUI. A totally different concept, not just a poor imitation of other toolkits.

I saw a charva screenshot, i noticed that in the JTable, he puts the header text on the top border, whereas i have a box as a header which eats up 3 lines. I think I can cut down the 2 lines.

Written by totalrecall

May 7, 2010 at 8:40 pm

Posted in ncurses, rbcurse, ruby

rbcurse 1.1.1 released

leave a comment »

I should have released this ages ago, but after moving my code to 1.9.1, I just could not get out of adding new components, features or cleaning up, or outright rewriting.

So here is 1.1.1 which is the 1.9 version. Please note that 0.1.3 is the 1.8.7 version. I believe 1.1.1 also runs on 1.8.6/7 but I’ve not fully tested it on 1.8. I’ve moved over to the Snow Leopard (Intel) machine — no longer on the old PowerPC, although its lying on my bed collecting dust.

Please use the master19 branch on github if you go there.

Written by totalrecall

May 6, 2010 at 11:48 pm

Posted in ncurses, rbcurse, ruby