module Termbox

Constants

Colors
Keys
VERSION

Public Class Methods

initialize_library(path=nil) click to toggle source
# File lib/termbox.rb, line 33
def initialize_library path=nil
  ffi_lib path || termbox_library_path
  attach_function :tb_init,        [], :int
  attach_function :tb_shutdown,    [], :void
  attach_function :tb_width,       [], :uint
  attach_function :tb_height,      [], :uint
  attach_function :tb_clear,       [], :void
  attach_function :tb_present,     [], :void
  attach_function :tb_set_cursor,  [:int, :int], :void
  attach_function :tb_put_cell,    [:uint, :uint, :pointer], :void  #pointer follows TbCell
  attach_function :tb_change_cell, [:uint, :uint, :ulong, :uint16, :uint16], :void
  attach_function :tb_blit,        [:uint, :uint, :uint, :uint, :pointer], :void # pointer follows TbCell

  # with 0 returns current input mode
  attach_function :tb_select_input_mode, [:int], :int
  attach_function :tb_peek_event, [:pointer, :int], :int
  attach_function :tb_poll_event, [:pointer], :int
end
lookup_key(string) click to toggle source
# File lib/termbox/keys.rb, line 86
def lookup_key string
  if string =~ /CTRL/i
    if string =~ /CTRL\s\+\s/i
      string.sub! /CTRL\s\+\s/i, ''  # CTRL + y
      return Keys["CTRL"][string.upcase]
    else
      string.sub! /CTRL\s/i, ''      # CTRL x
      return Keys["CTRL"][string.upcase]
    end
  end

  # assume other keys like "ESC"
  return Keys[string.upcase]
end
reverse_key_lookup(num) click to toggle source
# File lib/termbox/keys.rb, line 101
def reverse_key_lookup num  # 102 => f
  # Quick check of top level keys
  match = Keys.select{|k,v| v.is_a?(Numeric) }.detect{|k,v| v == num }
  return match.shift if match

  # Now look through Ctrl sequences
  combo = Keys.select{|k,v| v.is_a?(Hash) }
  if combo.any?
    combo.each do |k,v| #k == CTRL
      match = v.detect{|k,v| v == num }
      if match
        return "CTRL + #{match.shift}"
      else
        return nil
      end
    end
  end
end
termbox_library_path(path=nil) click to toggle source
# File lib/termbox.rb, line 25
def termbox_library_path path=nil
  if path
    @library_path = path
  end

  @library_path || "libtermbox"
end

Private Instance Methods

initialize_library(path=nil) click to toggle source
# File lib/termbox.rb, line 33
def initialize_library path=nil
  ffi_lib path || termbox_library_path
  attach_function :tb_init,        [], :int
  attach_function :tb_shutdown,    [], :void
  attach_function :tb_width,       [], :uint
  attach_function :tb_height,      [], :uint
  attach_function :tb_clear,       [], :void
  attach_function :tb_present,     [], :void
  attach_function :tb_set_cursor,  [:int, :int], :void
  attach_function :tb_put_cell,    [:uint, :uint, :pointer], :void  #pointer follows TbCell
  attach_function :tb_change_cell, [:uint, :uint, :ulong, :uint16, :uint16], :void
  attach_function :tb_blit,        [:uint, :uint, :uint, :uint, :pointer], :void # pointer follows TbCell

  # with 0 returns current input mode
  attach_function :tb_select_input_mode, [:int], :int
  attach_function :tb_peek_event, [:pointer, :int], :int
  attach_function :tb_poll_event, [:pointer], :int
end
lookup_key(string) click to toggle source
# File lib/termbox/keys.rb, line 86
def lookup_key string
  if string =~ /CTRL/i
    if string =~ /CTRL\s\+\s/i
      string.sub! /CTRL\s\+\s/i, ''  # CTRL + y
      return Keys["CTRL"][string.upcase]
    else
      string.sub! /CTRL\s/i, ''      # CTRL x
      return Keys["CTRL"][string.upcase]
    end
  end

  # assume other keys like "ESC"
  return Keys[string.upcase]
end
reverse_key_lookup(num) click to toggle source
# File lib/termbox/keys.rb, line 101
def reverse_key_lookup num  # 102 => f
  # Quick check of top level keys
  match = Keys.select{|k,v| v.is_a?(Numeric) }.detect{|k,v| v == num }
  return match.shift if match

  # Now look through Ctrl sequences
  combo = Keys.select{|k,v| v.is_a?(Hash) }
  if combo.any?
    combo.each do |k,v| #k == CTRL
      match = v.detect{|k,v| v == num }
      if match
        return "CTRL + #{match.shift}"
      else
        return nil
      end
    end
  end
end
termbox_library_path(path=nil) click to toggle source
# File lib/termbox.rb, line 25
def termbox_library_path path=nil
  if path
    @library_path = path
  end

  @library_path || "libtermbox"
end