The Accidental Rubyist

invalid byte sequence in UTF-8

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

After creating a table, I can do:

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

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

12 Responses

Subscribe to comments with RSS.

  1. tried out a couple of the examples – rbcurse looks great and really easy to use.

    question – does it handle unicode as well? I tried to add some non-standard latin characters like German and Japanese, saving the file as utf-8 (emacs changed the first line to # -*- coding: utf-8 -*- automatically), but rbcurse shows gibberish like “�~\” for all non-simple latin characters.

    Any idea?


    December 27, 2010 at 7:16 pm

  2. P.S. I was using rbcurse-1.1.5


    December 27, 2010 at 7:18 pm

  3. You should try using the latest. There’s a pre release gem on gemcuter or you can download from github.

    I think unicode display is dependent not on rbcurse but on your terminal and perhaps ncurses-ruby.

    I also need to know your ruby version. I have used unicode in some samples like the mail reader and gmail client (see prerelease or github).

    Can you confirm whether your shell is able to echo unicode chars using the echo command ?

    Are you able to display unicode characters from the sample ncurses-ruby programs. You can replace any string in them with a unicode/multibyte character and run it.

    If all this works but it fails in rbcurse you can post your code to a gist and give me the link. I’ll check it here.

    whats your config ? OS, ruby version.


    December 27, 2010 at 7:31 pm

  4. I just upgraded to ncurses 1.2.4 using your excellent doc (big thanks, otherwise installing ruby apps is never fun, always issues with version incompatibilities)

    ~$ gem list –local

    *** LOCAL GEMS ***

    ncurses (1.2.4)
    rake (0.8.7)

    ~$ ruby –version
    ruby 1.8.7 (2010-08-16 patchlevel 302) [i486-linux]

    And I tried to use the git sources, but don’t know how to install, there’s no ‘install’ target in the makefile, so this is my failed attempt:

    ~$ git clone

    Cloning into rbcurse…
    remote: Counting objects: 7154, done.
    remote: Compressing objects: 100% (2380/2380), done.
    remote: Total 7154 (delta 4759), reused 7150 (delta 4755)
    Receiving objects: 100% (7154/7154), 1.53 MiB | 503 KiB/s, done.
    Resolving deltas: 100% (4759/4759), done.

    ~$ cd rbcurse

    ~/rbcurse$ ruby -I lib examples/testtable.rb

    ncurses stopped
    “./lib/rbcurse/colormap.rb:8:in `get_color_const’\n./lib/rbcurse/colormap.rb:70:in `setup’\n./lib/ver/ncurses.rb:19:in `start_ncurses’\nexamples/testtable.rb:39”
    examples/testtable.rb:259: undefined method `debug’ for nil:NilClass (NoMethodError)

    Is there an easier way to test your prerelease?


    December 28, 2010 at 5:39 pm

  5. You can do a gem install — but you have to use the “–pre” flag.

    gem install rbcurse –pre

    With the git clone, you would still have to (i think) build a gem and install it. There should be rake tasks to do that. “rake build” and “rake install”.


    December 28, 2010 at 6:17 pm

  6. Answering your other questions – yes, I do unicode on the cmd line no prob, using echo etc.

    ~/rbcurse$ locale

    ~/rbcurse$ uname -a
    Linux snappian 2.6.32-5-686 #1 SMP Sat Oct 30 22:47:19 UTC 2010 i686 GNU/Linux

    I also just tried a ncurses-ruby example and places some Japanese characters into strings like in
    scr.addstr(“Press a key to 勿論 continue”)

    Same problem in ncurses-ruby… So I guess I’d have to ask the ncurses-ruby, which will stop my investigation of this whole business right here 😦 I just don’t want to spend hours and hours into a lib before even knowing if it will work for me in the end. Otherwise, rbcurse really seems to rock.


    December 28, 2010 at 6:19 pm

  7. Please note that in the previous post, pre requires TWO dashes or hyphens.

    Also, i think you have to be INSIDE the examples folder to run the examples. I think this is more so with 1.9.2. I have been off for a couple months so am a bit rusty.

    But keep mailing me, will be glad to help.


    December 28, 2010 at 6:20 pm

  8. Please could you post your query on the ruby forum.

    Since i have been off for a couple of months I am a bit lost. I am quite certain that we _are_ able to do unicode, but i just cannot recall the details.

    Perhaps some methods in ncurses allow it and some dont. Perhaps print_string like methods dont but you can try some like addch or addchar.

    I am absolutely positively sure i have done this somewhere.

    Give me an hour, let me dig thru and see. Also, checking might yield some results.


    December 28, 2010 at 6:25 pm

  9. I am still a little lost. I cut paste the string into irb or run onthe prompt and it works.

    However, if i even define a string in my ruby program and run it, i get an error:

    appemail.rb:70: invalid multibyte char (US-ASCII)
    appemail.rb:70: invalid multibyte char (US-ASCII)
    appemail.rb:70: syntax error, unexpected $end, expecting keyword_end
    ss = “Press a key to 勿論 continue”

    I once used the following to get sanskrit characters. You may need to change UTF8 with 16 or Shift-JIS. I have never done this, perhaps somewhere on google this has been done.

    require ‘jcode’
    $KCODE = ‘UTF8’


    December 28, 2010 at 6:48 pm

  10. OK I GOT IT !!!!

    ruby -KU programname.rb

    Now it shows your string on my screen.

    Hope you are able to get it working.


    December 28, 2010 at 6:56 pm

  11. thanks for your support, rkumar,

    the -KU doesn’t work for me, maybe your ruby is newer than mine? but what you posted is definitely encouraging – will have another look and google in the next couple of days.


    December 28, 2010 at 7:51 pm

  12. There is quite a bit on shades_of_gray blog. I found this on that, in the google cache. I was googling “ruby KCODE kanji”.

    I don’t think ruby version matters since -KU is very old. I tested out on:

    ruby 1.9.2p0 (2010-08-18 revision 29034) [x86_64-darwin10]


    December 28, 2010 at 8:30 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: