The Accidental Rubyist

invalid byte sequence in UTF-8

Friendlier curses with ruby

with 6 comments

I’ve been working for the last month or two, on an application builder for ncurses, using ruby. I’ve been learning both ncurses and ruby and thus have written and rewritten and refactored my work, so many times ….

Finally, the other day I thought i would just try out creating a field in pure ruby (on an ncurses window). (Putting aside my 2 months work) In about a couple hours, I HAD a working field. Along with a FORM and navigation etc. All in ruby, you can create the FIELD whenever you want, change all attributes whenever you want … none of the complicated ncurses hassles and sequencing etc.

Inactive field, integer field, char field, label linked to variable
Inactive field, integer field, char field, label linked to variable

Best of all the code is very small, and one can improve, debug etc it, whereas with ncurses you have to accept what is provided with all its limitations. Don’t get me wrong, I have enjoyed working with forms and fields and even multiline ones, but ultimately even after writing umpteen wrappers every which way, it still was NOT fun.

So then I created a label, and then a button. It was quick! All in my hands. I looked up the TK interface in the Pickaxe and made mine similar. In ONE DAY i had all this ready.

Button with focus
Button with focus, second button is based on a Variable

Day 2:
Got stuck making a menu bar. A single level was easy, but submenus really had me. Took a full day to get that done. That was the hardest.

Checkbox
Checkbox (Please click me)
clicked checkbox
clicked checkbox, results in change in button title

Day 3: Zipped through a message box, then took 5 minutes to extend the button and make a proper checkbox. Took 2 minutes to extend that and make radio buttons!!!

Message box with custom buttons
Message box with custom buttons

Day 4: I made a multiple line field (Textarea) – again tired of the limitations of the ncurses ones … spent quite some time doing word wrapping while entry, editing, deleting etc. This is one of the tough ones that needs a lot of work and testing. You should be able to dump data from outside and write it out and have it just perfect. The current one has no limitation as far as size is concerned. However, with all the situations I am trying to cover, it could be buggy yet.

Somewhere in there, i also made a scrollable list box.

scrollable list

The whole thing actually comes to two pretty small files, almost no dependencies, looks very much like the “links” browser’s menu bar and message box.

These look much cleaner and more profession that what CDK looks like.

I am going to start putting up examples, code samples and images in the next few days.

Advertisements

Written by totalrecall

November 24, 2008 at 10:21 pm

Posted in ncurses, rbcurse, ruby

Tagged with ,

6 Responses

Subscribe to comments with RSS.

  1. This is an absolutely wonderful effort! If only I had this about a year ago I would have kept working on my curses-based RSS feed reader. Keep up the awesome work!

    Ardekantur

    November 25, 2008 at 2:19 am

  2. This looks awesome. I was working on something similar, but only got as far as one refactoring so far. Are you planning on releasing the code at some point? Put up a repo on github perhaps?

    Casper

    November 25, 2008 at 5:08 am

  3. Thanks for the appreciation.
    1. Yes, whatever I am doing is *solely* for releasing and sharing. I am using “git” on my laptop, and will try figuring out how to release on github in the next few days. Until then will put up a tgz file.
    Currently, the status of the code is like “just born”, not even a week old.

    totalrecall

    November 25, 2008 at 10:24 am

  4. Thanks for the appreciation.
    1. Yes, whatever I am doing is *solely* for releasing and sharing. I am using “git” on my laptop, and will try figuring out how to release on github in the next few days. Until then will put up a tgz file.
    Currently, the status of the code is like “just born”, not even a week old.

    totalrecall

    November 25, 2008 at 10:26 am

  5. Hi!
    I’ve problems with Fedora 11 and your gem 0.13:
    ————————————————————-
    ./testcurses.rb
    /usr/lib/ruby/gems/1.8/gems/rbcurse-0.1.3/lib/rbcurse/colormap.rb:29: warning: parenthesize argument(s) for future version
    /usr/lib/ruby/gems/1.8/gems/rbcurse-0.1.3/lib/rbcurse/rwidget.rb:476: warning: parenthesize argument(s) for future version
    /usr/lib/ruby/gems/1.8/gems/rbcurse-0.1.3/lib/rbcurse/rlistbox.rb:147: warning: parenthesize argument(s) for future version
    /usr/lib/ruby/gems/1.8/gems/rbcurse-0.1.3/lib/rbcurse/rlistbox.rb:727: warning: parenthesize argument(s) for future version
    /usr/lib/ruby/gems/1.8/gems/rbcurse-0.1.3/lib/rbcurse/rlistbox.rb:727: warning: parenthesize argument(s) for future version
    /usr/lib/ruby/gems/1.8/gems/rbcurse-0.1.3/lib/rbcurse/rlistbox.rb:739: warning: parenthesize argument(s) for future version
    /usr/lib/ruby/gems/1.8/gems/rbcurse-0.1.3/lib/ver/window.rb:241:in `default_for’: undefined method `stdscr’ for Ncurses:Module (NoMethodError)
    from /usr/lib/ruby/gems/1.8/gems/rbcurse-0.1.3/lib/ver/window.rb:232:in `layout_value’
    from /usr/lib/ruby/gems/1.8/gems/rbcurse-0.1.3/lib/ver/window.rb:226:in `reset_layout’
    from /usr/lib/ruby/gems/1.8/gems/rbcurse-0.1.3/lib/ver/window.rb:225:in `each’
    from /usr/lib/ruby/gems/1.8/gems/rbcurse-0.1.3/lib/ver/window.rb:225:in `reset_layout’
    from /usr/lib/ruby/gems/1.8/gems/rbcurse-0.1.3/lib/ver/window.rb:18:in `initialize’
    from ./testcurses.rb:7:in `new’
    from ./testcurses.rb:7
    ————————————————————-

    the source:
    ————————————————————-

    require ‘rubygems’
    require ‘rbcurse’

    @layout = { :height => 0, :width => 0, :top => 0, :left => 0 }
    @win = VER::Window.new(@layout)
    @form = Form.new @win

    mylist = []
    0.upto(100) { |v| mylist << "#{v} scrollable data" }

    field = Listbox.new @form do
    name "mylist"
    row r
    col 1
    width 40
    height 10
    list mylist
    end
    field.insert 5, "hello ruby", "so long python", "farewell java", "RIP .Net"
    ————————————————————-

    Thanks

    Giuseppe

    November 9, 2009 at 6:28 pm

  6. Guiseppe, sorry for the delay, was in hospotal without internet connection.

    Essentially, you have to see the full examples such as testkeypress.rb or test2.rb since there are more initializations required. I think the sample programs in the README should be updated too.

    I have corrected your program.

    require ‘rubygems’
    require ‘ncurses’
    require ‘logger’
    require ‘rbcurse’
    require ‘rbcurse/rlistbox’

    include RubyCurses
    include RubyCurses::Utils
    begin
    @layout = { :height => 0, :width => 0, :top => 0, :left => 0 }
    VER::start_ncurses # this is initializing colors via ColorMap.setup
    $log = Logger.new(“view.log”)
    $log.level = Logger::DEBUG
    @win = VER::Window.new(@layout)
    @form = Form.new @win

    mylist = []
    0.upto(100) { |v| mylist < ex
    ensure
    @win.destroy if !@win.nil?
    VER::stop_ncurses
    p ex if ex
    p(ex.backtrace.join(“\n”)) if ex
    #$log.debug( ex) if ex
    #$log.debug(ex.backtrace.join(“\n”)) if ex
    end

    totalrecall

    November 11, 2009 at 8:14 pm


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: