Sha256: 7a8f21890408227c550aa38be31c4a6ea97c3803c45ca840a23a6978f3edf259

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require "ffi-ncurses"

require_relative "rurses/version"
require_relative "rurses/panel_stack"
require_relative "rurses/window"

module Rurses
  SPECIAL_KEYS = Hash[
    FFI::NCurses::KeyDefs
      .constants
      .select { |name| name.to_s.start_with?("KEY_") }
      .map    { |name|
        [ FFI::NCurses::KeyDefs.const_get(name),
          name.to_s.sub(/\AKEY_/, "").to_sym ]
      }
  ]

  module_function

  def curses
    FFI::NCurses
  end

  def program(modes: [ ])
    @stdscr = Window.new(curses_ref: curses.initscr, standard_screen: true)
    @stdscr.change_modes(modes)
    yield(@stdscr)
  ensure
    curses.endwin
  end

  def stdscr
    @stdscr
  end

  def get_key
    case (char = curses.getch)
    when curses::KeyDefs::KEY_CODE_YES..curses::KeyDefs::KEY_MAX
      SPECIAL_KEYS[char]
    when curses::ERR
      nil
    when 1
      :CTRL_A
    when 5
      :CTRL_E
    when 10
      :ENTER
    when 11
      :CTRL_K
    when 14
      :CTRL_N
    when 23
      :CTRL_W
    when 24
      :CTRL_X
    when 127
      :BACKSPACE
    else
      char.chr
    end
  end

  def update_screen
    curses.doupdate
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
terminal-notes-0.0.1 lib/terminal-notes/rurses.rb