Roguelike project?

On Tue, Nov 4, 2008 at 3:19 PM, Nit K. [email protected] wrote:

Nit K. wrote:

Then for kicks, i linked only keyboard.rb to a sample program and with
only a couple lines added it worked. Prints out the control keys and ESC
key combos.
It even gets Esc-a and Esc-A separately - wonderful. (it did hang when
ESC was pressed twice).

Sorry, the hang was due to not setting half-delay. Still want to hear
from you about alt keys.

They seem to work for me just fine, what terminal are you using?
You could look which key codes are pushed into the @stack.

Nit K. wrote:

Then for kicks, i linked only keyboard.rb to a sample program and with
only a couple lines added it worked. Prints out the control keys and ESC
key combos.
It even gets Esc-a and Esc-A separately - wonderful. (it did hang when
ESC was pressed twice).

Sorry, the hang was due to not setting half-delay. Still want to hear
from you about alt keys.

Nit K. wrote:

Nit K. wrote:

NameError: uninitialized constant FFI::Platform::ARCH_
Or maybe it does not work on OS X yet.

It supports “darwin”, but not the “powerpc” cpu.

Probably just some missing build flags; libffi, on which it’s based,
does support Darwin on PPC.

Jump on Ruby-FFI lists if you think you can help debug and get it
working:

http://kenai.com/projects/ruby-ffi

  • Charlie

Charles Oliver N. wrote:

Nit K. wrote:

Nit K. wrote:

NameError: uninitialized constant FFI::Platform::ARCH_
Or maybe it does not work on OS X yet.

It supports “darwin”, but not the “powerpc” cpu.

Probably just some missing build flags; libffi, on which it’s based,
does support Darwin on PPC.

Jump on Ruby-FFI lists if you think you can help debug and get it
working:

http://kenai.com/projects/ruby-ffi

  • Charlie

The problem comes here (line 28 of platform.rb).
ARCH = case CPU.downcase
when /i?86|x86|i86pc/
“i386”
when /amd64|x86_64/
“x86_64”
else
28. raise FFI::PlatformError, “Unknown cpu architecture: #{ARCH_}”
end

When i checked my rbconfig.rb I have “powerpc” as the “host_cpu”.

So, I changed platform.rb to :

  • when /i?86|x86|i86pc/
  • when /i?86|x86|i86pc|powerpc/

… and now your sample works fine! Thanks.

Forgive me for being spoilt, but is there some script that takes a
header file, or C source and generates the attach_function lines ?

Michael F. wrote:

On Tue, Nov 4, 2008 at 3:19 PM, Nit K. [email protected] wrote:

Nit K. wrote:

Then for kicks, i linked only keyboard.rb to a sample program and with
only a couple lines added it worked. Prints out the control keys and ESC
key combos.
It even gets Esc-a and Esc-A separately - wonderful. (it did hang when
ESC was pressed twice).

Sorry, the hang was due to not setting half-delay. Still want to hear
from you about alt keys.

They seem to work for me just fine, what terminal are you using?

I am using “screen”. Its the only one which supported the F1… keys. My
default xterm-color does not.

You could look which key codes are pushed into the @stack.

okay, will try that.

Nit K. wrote:

Michael F. wrote:

However, it does not accept/print the Alt-keys - is that deliberate, or
did have you not looked into it.

I have had problems with alt-keys (since they generate 2 bytes and that
goes thru my getch() loop twice, often leaving an extra char in the edit
field.

manver, I have figured out one thing about alt keys. To do this i had to
make: PRINTABLE = ASCII
until someone can tell me why irb != ruby.

In osx, there is a terminal option “Use alt as meta key”. Now “alt”
gives me the same result as using Esc. So now, alt-a and Esc-a will both
generate “M-a”.
Without this setting, alt-a gives 165, alt-b = 171, alt-c = 167 which is
not contiguous, and generates some junk chars.

So now i’ve basically duplicate esc and alt. But i accidentally
discovered something … I can press “Esc ^A”, or “Alt ^A” so i do have
a bunch more of keys i can map! Hope there’s nothing more i’ve missed.

Your file keyboard.rb rocks, please make it a gem, so others CUI
programmers can benefit from it. Preferably standalone.

Nit K. wrote:

Nit K. wrote:

Michael F. wrote:

manver – a little bit of feedback regarding keyboard.rb.

  1. it returns strings. If you use ncurses form drivers these take
    integers. So one has to convert the value sent to an int to avoid
    errors.

So typically in the start of the press method, I have done:

ret = ret[0] if ret.length == 1
ret = 32 if ret == 'space'

Even then one has to watch out for others that are not single length
tab/return/bs/ etc… will have to check for others. Some of these are
separately handled in the case, such as tab and return so thats okay.
Others such as backspace and delete etc which were passed straight to
the driver, need to be watched out for.

  1. You check for @stopping? inside the while char loop immediately after
    while.
    To me this means that after the user has entered the Exit key, he still
    has to press one more key to actually exit. I’ve put the break before
    the end of the loop. Would like you to confirm this.

Nit K. wrote:

So, I changed platform.rb to :

  • when /i?86|x86|i86pc/
  • when /i?86|x86|i86pc|powerpc/

… and now your sample works fine! Thanks.

Can you submit this to Ruby FFI, as a bug or on the mailing list? I’m
not sure Wayne M. monitors ruby-talk.

Forgive me for being spoilt, but is there some script that takes a
header file, or C source and generates the attach_function lines ?

There’s some generation logic for at least struct layout, but I’m not
sure if it does general function wiring. It was written by the Rubinius
guys, and there’s not a lot of documentation for it I could see. Good
idea though.

I expect we’ll see a flurry of fully-wired FFI-based libraries getting
released one after another very soon here, and each library is one less
library to wire.

  • Charlie

Michael F. wrote:
Still want to hear

from you about alt keys.

They seem to work for me just fine, what terminal are you using?
You could look which key codes are pushed into the @stack.

  1. manver. what do the alt-keys print for you (or send to press() ) ?

  2. I have found the issue:
    when you generate PRINTABLE_KEYS as follows:

    PRINTABLE.each do |key|
    code = key.unpack(‘c’)[0] # using unpack to be compatible with 1.9
    PRINTABLE_KEYS[code] = key
    MOD_KEYS[[ESC, code]] = “M-#{key}” unless key == ‘[’ # don’t map
    esc
    end

in line: PRINTABLE_KEYS[code] = key

for ALT-A which is 165, 165.chr = “\245” (slash 245)
the key unpack line generates “-91”.
So PRINTABLE_KEYS has: -91 => “\245”
instead of : 165 => “\245”

Thus, nothing is being picked up in line 41:

   NCURSES_KEYS[char] || CONTROL_KEYS[char] || PRINTABLE_KEYS[char]

If I do: “\245”[0], I get 165. btw, I am using ruby 1.8.7 not 1.9.

I did a quick check with:

key.unpack(‘C’)[0]
and this gives me 165. So perhaps, instead of:

  •  code = key.unpack('c')[0] # using unpack to be compatible with 
    

1.9

it should be (C - unsigned int)

  •  code = key.unpack('C')[0] # using unpack to be compatible with 
    

1.9


I cannot verify this due to something extremely baffling to me:

in IRB,
ASCII = (0…255).map{|c| c.chr }
PRINTABLE = ASCII.grep(/[[:print:]]/)
PRINTABLE.length

191

However, inside the ruby program PRINTABLE.length only gives 95 !! ???

#!/opt/local/bin/ruby
ASCII = (0…255).map{|c| c.chr }
puts(ASCII.length)
PRINTABLE = ASCII.grep(/[[:print:]]/)
puts(PRINTABLE.length)

So the alt-codes are lost. I am using the same terminal for both. I am
using the same ruby interpreter in the sample program, as the one irb is
using.

Can someone tell me why the grep command returns a different result in
irb vs a ruby program ???

On Mon, Nov 10, 2008 at 12:16 AM, Nit K. [email protected]
wrote:

Wondering if you can give a small example that installs and uses keys
using these classes so i can use it. I am finding myself having to
reinvent the wheel since I am unable to grasp the code of ver.

Hope this helps:
http://p.ramaze.net/10515

Sorry, it’s quite late here, can’t go into more explanation for now.
And yeah, the code is quite hard to grasp, i’m not going to change it
anymore though since it works really well for what i need, the main
difficulty is with the keymapping but i couldn’t come up with anything
more elegant, the Keyboard is tied to the VER concept a bit as well,
but you should be able to improve on that easily.

^ manveru

Michael F. wrote:

manver, I mentioned to your earlier that I am intending to use
keyboard.rb of the ver project.

I have also been studying keymap.rb and its associated programs so i can
get more keys like in vi and emacs. However, I understand these classes
in chunks but can’t piece it together.

Wondering if you can give a small example that installs and uses keys
using these classes so i can use it. I am finding myself having to
reinvent the wheel since I am unable to grasp the code of ver.

Thanks in advance.

Michael F. wrote:

On Mon, Nov 10, 2008 at 12:16 AM, Nit K. [email protected]
wrote:

Wondering if you can give a small example that installs and uses keys
using these classes so i can use it. I am finding myself having to
reinvent the wheel since I am unable to grasp the code of ver.

Hope this helps:
http://p.ramaze.net/10515

Thanks a million. I really appreciate this. I should be able to get my
head around this.
Cheers.

On Mon, Nov 10, 2008 at 4:57 AM, Nit K. [email protected]
wrote:

let is not a method is was map. and map should be key.
Not sure if my corrections are correct, they are based on vi.rb.

Pull from the repo, there have been major changes and improvments to
VER.

Nit K. wrote:

Michael F. wrote:

On Mon, Nov 10, 2008 at 12:16 AM, Nit K. [email protected]
wrote:

Hope this helps:
http://p.ramaze.net/10515

manver,
I have got the above sample working (after making some corrections. e.g.
let is not a method is was map. and map should be key.
Not sure if my corrections are correct, they are based on vi.rb.

The sample prints all keys, including Esc combinations, but does not use
C-c as a control, although the mapping is there.

key :esc, :into_control_mode
key ‘C-c’, :into_control_mode # esc is slow due to timeout

I also did this:

VER.map :control do
key ‘C-q’, :stop
key ‘q’, :stop
key ‘r’, :close
end

I assumed that under C-c I could press C-q or q or r. But these are not
mapped to C-c or Esc.
Any tips on what to do for C-c would be great.

Thanks a lot.
p.s. I have attached the file I modified from the link you gave me as
test.rb.

Michael F. wrote:

On Mon, Nov 10, 2008 at 4:57 AM, Nit K. [email protected]
wrote:

let is not a method is was map. and map should be key.
Not sure if my corrections are correct, they are based on vi.rb.

Pull from the repo, there have been major changes and improvments to
VER.
Works like a charm :smiley:

On Tue, Nov 11, 2008 at 12:28 AM, Nit K. [email protected]
wrote:

I integrated your example into one of my programs. (Works fine. Changed
But apparently the stack reads one digit, and goes into ready().

  1. I just want to check if multiple digits work with you, am i doing
    something wrong.

Yeah, it has to be:
map([/^\d$/, /^\d$/, ‘k’]){ @args.join.to_i.times{ view.up }}
which is a PITA, so i made a shortcut for it, see below

  1. I would like to know if you modify or improve/debug this further. Is
    there some list or webpage for “ver”?

I fixed that just today, check it out :slight_smile:

^ manveru

Michael F. wrote:

On Mon, Nov 10, 2008 at 4:57 AM, Nit K. [email protected]
wrote:

let is not a method is was map. and map should be key.
Not sure if my corrections are correct, they are based on vi.rb.

Pull from the repo, there have been major changes and improvments to
VER.

manver.
I integrated your example into one of my programs. (Works fine. Changed
VER.stopping and VER.info the @focus.stopping and view.info.)

I was trying out the n-action feature: e.g 3j 10j etc.
It works fine for single digits but when i try double digits, i get:
No mapping for [“1”, “2”].
I tried combinations such as :

map([/^(\d\d?)$/, ‘k’]){ @arg.to_i.times {view.up}}
or \d+ etc.

But apparently the stack reads one digit, and goes into ready().

  1. I just want to check if multiple digits work with you, am i doing
    something wrong.

  2. I would like to know if you modify or improve/debug this further. Is
    there some list or webpage for “ver”?

Thanks a lot for your help.

Michael F. wrote:

On Tue, Nov 11, 2008 at 12:28 AM, Nit K. [email protected]
wrote:

I integrated your example into one of my programs. (Works fine. Changed
But apparently the stack reads one digit, and goes into ready().

  1. I just want to check if multiple digits work with you, am i doing
    something wrong.

Yeah, it has to be:
map([/^\d$/, /^\d$/, ‘k’]){ @args.join.to_i.times{ view.up }}
which is a PITA, so i made a shortcut for it, see below

  1. I would like to know if you modify or improve/debug this further. Is
    there some list or webpage for “ver”?

I fixed that just today, check it out :slight_smile:
easy count prefix via count_map: 10j, 200b, 20%, 42g, 7up, ... · manveru/ver@9fb04aa · GitHub

^ manveru

I’ve only integrated the count_map as yet.

Absolutely brilliant. Works like a dream :smiley:

Michael F. wrote:

I fixed that just today, check it out :slight_smile:
easy count prefix via count_map: 10j, 200b, 20%, 42g, 7up, ... · manveru/ver@9fb04aa · GitHub

^ manveru

Manveru

I have a question regarding the scope of the keybindings. Being new to
ruby am confused.

Are these done in a global/application scope. I mean :

 @keyhandler = VER::KeyHandler.new(self)
 VER.let :insert do
  map(/^([[:print:]])$/){ view.show(@arg) }

Here, i have defined a key handler, but i don’t see an object for my key
mappings.

My application is slightly different from vi - its multi-screen. Take
alpine e.g. Each screen has its own mappings. I go from menu, to screen
A, then screen B, then back to menu. All objects in screen A are
released so the mappings too are. Thus each screen has its own bindings
mappings.

Am i correct in thinking that as per VER, my entire app would have to
share the same bindings?

thanks.

On Tue, Nov 11, 2008 at 6:01 PM, Nit K. [email protected]
wrote:

alpine e.g. Each screen has its own mappings. I go from menu, to screen
A, then screen B, then back to menu. All objects in screen A are
released so the mappings too are. Thus each screen has its own bindings
mappings.

Am i correct in thinking that as per VER, my entire app would have to
share the same bindings?

No, mappings are done per mode, and the handling is done via Keyboard,
where you assign the object you want to send your keys to via focus=.
That means, if you go to a menu, menu has to get focus and it should
have a mode, like :menu. Then the mappings from VER.let(:menu){} are
used, those might give focus to another window, which has a mode like
:window…
So bindings are bundled into modes, and if you switch the mode you get
different bindings.
You can also inherit bindings from other modes to keep things DRY, like
this:

VER.let :foo do
map(‘f’){ view.show(‘from foo’) }
end

VER.let :bar do
map(‘b’){ view.show(‘from bar’) }
end

VER.let :duh => [:foo, :bar] do
map(‘d’){ view.show(‘from duh’) }
end

This would give :duh the mappings f, b, and d, while :bar only has b
and foo only has f.
All of the blocks are instance_evaled in the scope of KeyHandler,
which provides the possibility for shortcuts to view or other
convenience-methods.

So if you have a view like this:

class Foo
attr_accessor :mode

def initialize(message)
@mode = :example
@keyhandler = VER::KeyHandler.new(self)
@message = message
end

def hello
window.printw(@message)
end

def other(message)
Keyboard.focus = self.class.new(message)
end

def press(key)
@keyhandler.press(key)
end
end

you can do following:

VER.let :example do
map(‘h’){ hello }
map(‘o’){ other(‘From Russia, with love’) }
end

Keyboard.focus = Foo.new(‘Hello, World!’)

Please note that I didn’t really run the code above, but it should
work like that in case i didn’t mess up something real bad.
I know that this is quite some overhead, but VER is highly dynamic in
regards to keymappings, just about any key can switch to a different
window, view, or mode, the keyboard focus changes quite often as well.
I also didn’t intend this to be used by some other application, so
some of the implementation is quite specific to VER and i’m not sure
how it would work out in another context.
If you have further issues, please consider forking the project and
work it into a shape that suits you better, possibly a library that
VER and your project and just about any app based on ncurses can
share. Handlings keys is a PITA, and this really is just the usually
piling of layers of abstraction, which I don’t like, but it seems to
be the only way until someone comes up with a unified way of handling
those keys (maybe next generation terminals in the year 2100).
This also has no handling of unicode yet, which I will need soon, so
no idea how the API will change by then.

^ manveru