[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

curses and the ACS_* chars

Markus Koenig

11/3/2004 3:02:00 AM

Hi.

I recently did some Curses programming (for the Sokoban Quiz) when I
found that the 'curses' extension does not include the ACS_* constants
which would have been very useful (see `man curs_addch`).

Because I decided not to use only ASCII (that looked a bit strange)
I created my own extension, 'curses_acs'. Below is the source of it.
Could that please be included in the standard 'curses' extension? Matz?

Markus


markus@g204 curses_acs $ ruby -e "puts RUBY_VERSION"
1.8.2
markus@g204 curses_acs $ cat example.rb
require 'curses'
require 'curses_acs'

Curses.init_screen
begin
Curses.define_acs
Curses.addstr 'The greek letter pi: '
Curses.addch_gen Curses::ACS_PI
Curses.addch ?\n
Curses.addstr "A box:\n"
Curses.addch_gen Curses::ACS_ULCORNER
Curses.addch_gen Curses::ACS_URCORNER
Curses.addch ?\n
Curses.addch_gen Curses::ACS_LLCORNER
Curses.addch_gen Curses::ACS_LRCORNER
Curses.getch
ensure
Curses.close_screen
end

markus@g204 curses_acs $ cat extconf.rb
require 'mkmf'
have_library('ncurses', 'initscr') or have_library('curses', 'initscr')
have_header('curses.h')
create_makefile('curses_acs')

markus@g204 curses_acs $ cat curses_acs.c
#include <stdlib.h>
#include <curses.h>
#include <ruby.h>

/*
* Curses#define_acs
* Add the ACS constants to Curses.
*/
VALUE curses_define_acs(VALUE self) {
rb_define_const(self, "ACS_BLOCK", ULONG2NUM(ACS_BLOCK));
rb_define_const(self, "ACS_BOARD", ULONG2NUM(ACS_BOARD));
rb_define_const(self, "ACS_BTEE", ULONG2NUM(ACS_BTEE));
rb_define_const(self, "ACS_BULLET", ULONG2NUM(ACS_BULLET));
rb_define_const(self, "ACS_CKBOARD", ULONG2NUM(ACS_CKBOARD));
rb_define_const(self, "ACS_DARROW", ULONG2NUM(ACS_DARROW));
rb_define_const(self, "ACS_DEGREE", ULONG2NUM(ACS_DEGREE));
rb_define_const(self, "ACS_DIAMOND", ULONG2NUM(ACS_DIAMOND));
rb_define_const(self, "ACS_GEQUAL", ULONG2NUM(ACS_GEQUAL));
rb_define_const(self, "ACS_HLINE", ULONG2NUM(ACS_HLINE));
rb_define_const(self, "ACS_LANTERN", ULONG2NUM(ACS_LANTERN));
rb_define_const(self, "ACS_LARROW", ULONG2NUM(ACS_LARROW));
rb_define_const(self, "ACS_LEQUAL", ULONG2NUM(ACS_LEQUAL));
rb_define_const(self, "ACS_LLCORNER", ULONG2NUM(ACS_LLCORNER));
rb_define_const(self, "ACS_LRCORNER", ULONG2NUM(ACS_LRCORNER));
rb_define_const(self, "ACS_LTEE", ULONG2NUM(ACS_LTEE));
rb_define_const(self, "ACS_NEQUAL", ULONG2NUM(ACS_NEQUAL));
rb_define_const(self, "ACS_PI", ULONG2NUM(ACS_PI));
rb_define_const(self, "ACS_PLMINUS", ULONG2NUM(ACS_PLMINUS));
rb_define_const(self, "ACS_PLUS", ULONG2NUM(ACS_PLUS));
rb_define_const(self, "ACS_RARROW", ULONG2NUM(ACS_RARROW));
rb_define_const(self, "ACS_RTEE", ULONG2NUM(ACS_RTEE));
rb_define_const(self, "ACS_S1", ULONG2NUM(ACS_S1));
rb_define_const(self, "ACS_S3", ULONG2NUM(ACS_S3));
rb_define_const(self, "ACS_S7", ULONG2NUM(ACS_S7));
rb_define_const(self, "ACS_S9", ULONG2NUM(ACS_S9));
rb_define_const(self, "ACS_STERLING", ULONG2NUM(ACS_STERLING));
rb_define_const(self, "ACS_TTEE", ULONG2NUM(ACS_TTEE));
rb_define_const(self, "ACS_UARROW", ULONG2NUM(ACS_UARROW));
rb_define_const(self, "ACS_ULCORNER", ULONG2NUM(ACS_ULCORNER));
rb_define_const(self, "ACS_URCORNER", ULONG2NUM(ACS_URCORNER));
rb_define_const(self, "ACS_VLINE", ULONG2NUM(ACS_VLINE));
return Qnil;
}

/*
* Curses#addch_gen
* addch for generic char values (Curses#addch truncates to 8 bit)
*/
static VALUE curses_addch_gen(VALUE self, VALUE c) {
addch(NUM2ULONG(c));
return Qnil;
}

/* This is called upon extension loading. */
void Init_curses_acs(void) {
VALUE curses_cls;
rb_require("curses");
curses_cls = rb_eval_string("Curses");
rb_define_singleton_method(curses_cls, "define_acs",
curses_define_acs, 0);
rb_define_singleton_method(curses_cls, "addch_gen",
curses_addch_gen, 1);
}

2 Answers

Alexander Kellett

11/3/2004 12:19:00 PM

0

on this note it would be lovely to see access to the
following field also in the binding:

extern NCURSES_EXPORT_VAR(int) ESCDELAY; /* ESC expire time in
milliseconds */

unless someone knows an alternative way to use this.
basically after pressing escape in ruvi without a dl
hack to access the above on linux (konsole at least)
you see a pause before the reaction to the escape
key press happens.

aredridel? did u have any time to look into this yet?

cheers,
Alex


Tobias Peters

11/4/2004 9:48:00 PM

0

Markus Koenig wrote:
> I recently did some Curses programming (for the Sokoban Quiz) when I
> found that the 'curses' extension does not include the ACS_* constants
> which would have been very useful (see `man curs_addch`).
>
> Because I decided not to use only ASCII (that looked a bit strange)
> I created my own extension, 'curses_acs'. Below is the source of it.
> Could that please be included in the standard 'curses' extension? Matz?

Maybe it is not as simple as that.

The ncurses C library provides macros starting with ACS_ which are not
constants, their value depends on the terminal in use. Thus, they should
not be used before initscr() has been called. The set_term() function
will also change the values returned by the ACS_ macros.

The ncurses-ruby bindings (http://ncurses-ruby.b...) provide
access to ACS_ macros: Constants of the Ncurses module are initialized
during the Ncurses.initscr call, while ACS_ macros for terminals opened
via Ncurses.newterm can be accessed as instance methods of Ncurses::SCREEN.

Tobias