Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Curses about libcurse

Reply
Thread Tools

Curses about libcurse

 
 
zimba-tm
Guest
Posts: n/a
 
      12-03-2005
------=_Part_6032_7248194.1133577613783
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hello,

I hate curses. Not that much, but it's really not that easy to build
ruby console applications using it.

Are there other libraries I could use instead of libcurses ?

My main complains are :
* you can't access the screen like a character matrix
* i want callbacks (onresize)
* you need to move the cursor where you want to write
* the "window"ing system is not cool
* it really feels like an old-skool C library in ruby

I started overriding the curses library to implement what I think is
nicer (which is subjective). Is anybody interested in this ? Also,
what is so special in the libcurse ? Is it because it supports VT100
that people are still using it ?

The joined file containes the modified curses class plus an class that
demonstrates how to use it (however it doesn't run)

--
Cheers,
zimba

http://zimba.oree.ch

------=_Part_6032_7248194.1133577613783
Content-Type: application/octet-stream; name=interface.rb
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="interface.rb"

require 'curses'

# curses sanitization
module Curses
@windows = Array.new
def self.windows
@windows
end

class Window

def init(buffer_size = 100, wrap = true)
@buffer = Array.new buffer_size, ''
@buffer_x = 0
@buffer_y = 0
@cursor_x = 0
@cursor_y = 0
@windows = Array.new
@line_wrap = wrap
Curses.windows.push self
end

def [](line_num)
@buffer[line_num]
end

def []=(line_num, line)
# TODO: check if data is in screen
@buffer[line_num] = line
old_setpos(line_num, 0)
if @line_wrap && line.length > maxx
addstr line[0..maxx] + "\n"
addstr line[maxx..line.length]
else
addstr line
end

# fill
addstr " " * (maxx - line.length)
old_setpos(line_num, line.length)
end

def puts(line)
# storage
if @cursor_x == @buffer.length-1
@buffer.pop
@buffer.push line
else
@cursor_x += 1
@buffer[@cursor_x] = line
end
# draw
if @line_wrap && line.length > maxx
addstr line[0..maxx] + "\n"
addstr line[maxx..line.length]
else
addstr line
end
end
alias :<< uts

def putc(char, y=nil, x=nil)
if y == nil
y = @cursor_y
end
if x == nil
x = @cursor_x
end
diff = @buffer[x].length - y
if diff < 0
@buffer[y] += " " * diff
end
@buffer[y][x] << char
end

alias utc :addch
alias :gets :getstr
alias :getc :getch

alias ld_refresh :refresh
def refresh
old_refresh
end

alias ld_clear :clear
def clear
setpos(0,0)
@buffer.fill('')
super
end

alias ld_setpos :setpos
def setpos(y, x)
@cursor_y = y
@cursor_x = x
old_setpos y, x
end

alias ld_move :move
def move(y, x)
setpos(@cursor_y + y, @cursor_x + x)
end

alias ld_subwin :subwin
def subwin(height, width, top, left)
subwin = old_subwin height, width, top, left
subwin.init
@windows.push subwin
return subwin
end

alias ld_close :close
def close
@windows.each do |window|
window.close
end
Curses.windows.delete self
Curses.close_screen if Curses.windows.length == 0
old_close
end
end

end

module Interface

class Cursed

def initialize(server)
@server = server

Signal.trap('INT') do
exit
end
end

def run
Curses::init_screen # binds curses to the current interface
Curses::raw # cbreak or raw, allows direct char handling
Curses::noecho # we will manage user output ourselves
# keypad(true) # allow handling of the keypad keys
Curses::nonl

lines = Curses::lines
cols = Curses::cols

# interface setup
@w = Curses::Window.new(lines, cols, 0, 0)
@w.init
@title = @w.subwin(1, cols, 0, 0)
@title[0] = "*** Welcome in zchat ***"
@title.refresh
@main = @w.subwin(lines-3, cols, 1, 0)
@main.scrollok(true)
@main.refresh
@status = @w.subwin(1, cols, lines-2, 0)
@status[0] = @server.hostname + ": " + @server.channel.keys.join(', ')
@status.refresh
@input = @w.subwin(1, cols, lines-1, 0)
@input[0] = ENV['USER']+'@'(E-Mail Removed)+'/#test$ '
@input.refresh

@server.channel["#test"].on_read do |line|
@main.puts line
@main.refresh
@input.refresh
end

# keyboard input handling
t = Thread.new do
buffer = ''
line = @input[0]
while true

chr = @input.getc
case chr
when 13 # ?\n
case buffer
when /^ls/
$server['#test'].puts '/NAMES #test'
when /^echo\s/
command = buffer[5..buffer.length-1]
@server['#test'].puts command + "\n" if command.length > 0
when /^join\s/
command = buffer[5..buffer.length-1]
@server.puts '/j ' + command
when '\quit', ':q', 'exit', 'quit'
exit
else
@status[0] = "command not found..."
@status.refresh
end

buffer = ''
@input.setpos(0, 0)
when 279168 # Curses::KEY_LEFT
@input.move(0, -1)
when 279167 # Curses::KEY_RIGHT
@input.move(0, +1)
when ?\C-c
exit
when ?\C-? # Curses::KEY_BACKSPACE
if buffer.length > 1
buffer = buffer[0..buffer.length-2] if buffer.length > 1
@input.move(0, -1)
else
buffer = ''
@input.setpos(0, 0)
end
else
buffer << chr
end

@input[0] = line + buffer
@input.refresh
end
end

t.join
end

def exit
@w.close
puts "Exiting zchat..."
@server.exit
Kernel.exit
end
end

end



------=_Part_6032_7248194.1133577613783--


 
Reply With Quote
 
 
 
 
Tsume
Guest
Posts: n/a
 
      12-03-2005
On Saturday 03 December 2005 11:40 am, zimba-tm wrote:
> Hello,
>
> I hate curses. Not that much, but it's really not that easy to build
> ruby console applications using it.
>


Join the club

> Are there other libraries I could use instead of libcurses ?
>


http://raa.ruby-lang.org/project/ncurses-ruby/

Tsume


 
Reply With Quote
 
 
 
 
Reyn Vlietstra
Guest
Posts: n/a
 
      12-03-2005
------=_Part_19452_21180194.1133592585224
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

http://rubyforge.org/projects/rush

would benefit greatly from some sort of improved text library

On 12/3/05, Tsume <(E-Mail Removed)> wrote:
>
> On Saturday 03 December 2005 11:40 am, zimba-tm wrote:
> > Hello,
> >
> > I hate curses. Not that much, but it's really not that easy to build
> > ruby console applications using it.
> >

>
> Join the club
>
> > Are there other libraries I could use instead of libcurses ?
> >

>
> http://raa.ruby-lang.org/project/ncurses-ruby/
>
> Tsume
>
>



--
Reyn Vlietstra

------=_Part_19452_21180194.1133592585224--


 
Reply With Quote
 
Richard Lyman
Guest
Posts: n/a
 
      12-03-2005
Is there something specifically that you've been trying to do but couldn't?

Is it just the quality or speed of development that is annoying you?

I ask only because I'm thinking about writing an Ncurses app, so feel
free to 'dump'.

-Rich



On 12/3/05, zimba-tm <(E-Mail Removed)> wrote:
> Hello,
>
> I hate curses. Not that much, but it's really not that easy to build
> ruby console applications using it.
>
> Are there other libraries I could use instead of libcurses ?
>
> My main complains are :
> * you can't access the screen like a character matrix
> * i want callbacks (onresize)
> * you need to move the cursor where you want to write
> * the "window"ing system is not cool
> * it really feels like an old-skool C library in ruby
>
> I started overriding the curses library to implement what I think is
> nicer (which is subjective). Is anybody interested in this ? Also,
> what is so special in the libcurse ? Is it because it supports VT100
> that people are still using it ?
>
> The joined file containes the modified curses class plus an class that
> demonstrates how to use it (however it doesn't run)
>
> --
> Cheers,
> zimba
>
> http://zimba.oree.ch
>
>
>



 
Reply With Quote
 
zimba-tm
Guest
Posts: n/a
 
      12-03-2005
On 03/12/05, Reyn Vlietstra <(E-Mail Removed)> wrote:
> http://rubyforge.org/projects/rush
>
> would benefit greatly from some sort of improved text library


Maybe we can work on something. I looked at rush some time ago and it
looked really promising.

> On 12/3/05, Tsume <(E-Mail Removed)> wrote:
> >
> > On Saturday 03 December 2005 11:40 am, zimba-tm wrote:
> > > Hello,
> > >
> > > I hate curses. Not that much, but it's really not that easy to build
> > > ruby console applications using it.
> > >

> >
> > Join the club
> >
> > > Are there other libraries I could use instead of libcurses ?
> > >

> >
> > http://raa.ruby-lang.org/project/ncurses-ruby/


Thanks for your reply.

The links on that page are not accessible to me. Are you part of that "club=
" ?

> >
> > Tsume
> >
> >

>
>
> --
> Reyn Vlietstra
>
>



--
Cheers,
zimba

http://zimba.oree.ch


 
Reply With Quote
 
zimba-tm
Guest
Posts: n/a
 
      12-03-2005
Hi Richard,

in brief, you can do anything with curses. You can write text on the
whole screen, use colors, the mouse. So it's not about technical
missing features.

However, the library makes it hard to do some things. First of all,
the library is seriously lacking in documentation. Just take a look to
the curses stdlib rdoc[1] to have an idea. I couldn't go really
further without looking at the provided examples.

Curses integrates a concept of windows. You set a window with top,
left, width and height. Then everything you write inside won't get out
of that box and it can be scrolled. The idea is pretty neat, but it
suffers some limitations. Because you don't have callbacks, you have
to make a loop to track the different changes (like mouse moves and
term resizes). And because it's not well documented, I found it was
hard to have consistent behavior.

It takes much too long to write a simple GUI.

[1] http://www.ruby-doc.org/stdlib/libdo...es/Curses.html

On 03/12/05, Richard Lyman <(E-Mail Removed)> wrote:
> Is there something specifically that you've been trying to do but couldn'=

t?
>
> Is it just the quality or speed of development that is annoying you?
>
> I ask only because I'm thinking about writing an Ncurses app, so feel
> free to 'dump'.
>
> -Rich


--
Cheers,
zimba

http://zimba.oree.ch


 
Reply With Quote
 
Reyn Vlietstra
Guest
Posts: n/a
 
      12-04-2005
------=_Part_28875_5177766.1133687055541
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Oh, I missed the replies.

The biggest issue with ruSH is that obviously we need to run existing
console binaries on it.
I ran into issues with things like vi etc. I needed to exit curses before
entering vi, or vi would moan and/or break. That made me introduce the ^
character for all the things that needed to exit curses. (eg. ^vi)

Other commands like say for instance 'ls' needs to be run with popen4 which
grabs the output and writes it to the curses screen, so anything that needs
input immediately hangs.

The other thing is that something like ls --color emits color codes, it's
been a while since I worked on this so my minds a bit sketchy. I was having
issues with the number of colors I could define and the fact that if I
change a color it changes all the colors with that number on the screen ...

errrm, so what I basically need is for curses and the console apps to play
together nicely, surely it's possible ?

Tell me if you cant get ruSH running to look at it, it's quite a mission at
the moment

We ... I need a next gen shell !

On 12/3/05, zimba-tm <(E-Mail Removed)> wrote:
>
> Hi Richard,
>
> in brief, you can do anything with curses. You can write text on the
> whole screen, use colors, the mouse. So it's not about technical
> missing features.
>
> However, the library makes it hard to do some things. First of all,
> the library is seriously lacking in documentation. Just take a look to
> the curses stdlib rdoc[1] to have an idea. I couldn't go really
> further without looking at the provided examples.
>
> Curses integrates a concept of windows. You set a window with top,
> left, width and height. Then everything you write inside won't get out
> of that box and it can be scrolled. The idea is pretty neat, but it
> suffers some limitations. Because you don't have callbacks, you have
> to make a loop to track the different changes (like mouse moves and
> term resizes). And because it's not well documented, I found it was
> hard to have consistent behavior.
>
> It takes much too long to write a simple GUI.
>
> [1] http://www.ruby-doc.org/stdlib/libdo...es/Curses.html
>
> On 03/12/05, Richard Lyman <(E-Mail Removed)> wrote:
> > Is there something specifically that you've been trying to do but

> couldn't?
> >
> > Is it just the quality or speed of development that is annoying you?
> >
> > I ask only because I'm thinking about writing an Ncurses app, so feel
> > free to 'dump'.
> >
> > -Rich

>
> --
> Cheers,
> zimba
>
> http://zimba.oree.ch
>
>



--
Reyn Vlietstra

------=_Part_28875_5177766.1133687055541--


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Replacing curses (Was: Re: Problem with curses and UTF-8) Jean-Paul Calderone Python 2 02-09-2006 08:29 AM
Curses chgat Michael J. Barillier Perl 0 09-12-2004 05:10 AM
curses and color Skeleton Man Perl 0 05-25-2004 10:14 AM
Boxline in curses ( Linux ) M.Senthil Kumar C Programming 7 02-23-2004 11:39 AM
Curses module. Alex Python 2 07-17-2003 07:25 AM



Advertisments