<commit_before># ruby-ffi wrapper for ncurses
#
# <NAME>
#
# repo: http://github.com/seanohalpin/ffi-ncurses
#
# - version 0.1.0 - 2008-12-04
# - version 0.2.0 - 2009-01-18
# - version 0.3.0 - 2009-01-31
# - version 0.3.3 - 2010-08-24
# - version 0.3.4 - 2010-08-28

require 'ffi'

module FFI
  module NCurses
    VERSION = "0.3.4"
    extend FFI::Library

    # Use `RUBY_FFI_NCURSES_LIB` to specify a colon-separated list of
    # libs you want to try to load, e.g.
    #
    #     RUBY_FFI_NCURSES_LIB=XCurses:ncurses
    #
    # to try to load XCurses (from PDCurses) first, then ncurses.
    if ENV["RUBY_FFI_NCURSES_LIB"].to_s != ""
      LIB_HANDLE = ffi_lib(ENV["RUBY_FFI_NCURSES_LIB"].split(/:/)).first
    else
      LIB_HANDLE = ffi_lib(['ncursesw', 'ncurses']).first
    end

    begin
      # These three global variables are defined in `ncurses.h`:
      #<table>
      # <tr><td><tt>stdscr</tt></td><td>represents the whole screen</td></tr>
      # <tr><td><tt>curscr</tt></td><td>used in some low-level routines</td></tr>
      # <tr><td><tt>newscr</tt></td><td>used in the <tt>[gs]etsyx</tt> routines</td></tr>
      # </table>
      #
      symbols = [ "stdscr", "curscr", "newscr" ]
      if LIB_HANDLE.respond_to?(:find_symbol)
        SYMBOLS = Hash[*symbols.map { |sym|
                         [sym, LIB_HANDLE.find_symbol(sym)]}.compact.flatten
                      ]
        SYMBOLS.keys.each do |sym|
          # Note that the symbol table entry in a shared lib for an
          # exported variable contains a *pointer to the address* of
          # the variable which will be initialized in the process's
          # bss (uninitialized) data segment when the process is
          # initialized.

          # This is unlike methods, where the symbol table entry points to
          # the entry point of the method itself.

          # Variables need another level of indirection because they are
          # *not* shared between process instances - only code is shared.

          # So we define convenience methods to perform the lookup for
          # us. We can't just stash the value returned by
          # `read_pointer` at load time because it's not initialized
          # until after `initscr` has been called.
          define_method sym do
            SYMBOLS[sym].read_pointer
          end
          module_function sym
        end
      else
        warn "#find_symbol not available - #{symbols.inspect} not defined"
      end
    rescue => e
    end

    # This list of function signatures was originally generated by the
    # file `generate-ffi-ncurses-function-signatures.rb` (in the
    # `./tools` directory), inserted here by hand then edited a bit.
    functions =
      [
       [:COLOR_PAIR, [:int], :int],
       [:PAIR_NUMBER, [:int], :int],
       [:add_wchnstr, [:pointer, :int], :int],
       [:add_wch, [:pointer], :int],
       [:add_wchstr, [:pointer], :int],
       [:addchnstr, [:pointer, :int], :int],
       [:addchstr, [:pointer], :int],
       [:addch, [:uint], :int],
       [:addnstr, [:string, :int], :int],
       [:addstr, [:string], :int],
       [:assume_default_colors, [:int, :int], :int],
       [:attr_get, [:pointer, :pointer, :pointer], :int],
       [:attr_off, [:uint, :pointer], :int],
       [:attr_on, [:uint, :pointer], :int],
       [:attr_set, [:uint, :short, :pointer], :int],<commit_msg>changed the way variables are handled and added some more (e.g. COLS, LINES)<commit_after># ruby-ffi wrapper for ncurses
#
# <NAME>
#
# repo: http://github.com/seanohalpin/ffi-ncurses
#
# - version 0.1.0 - 2008-12-04
# - version 0.2.0 - 2009-01-18
# - version 0.3.0 - 2009-01-31
# - version 0.3.3 - 2010-08-24
# - version 0.3.4 - 2010-08-28

require 'ffi'

module FFI
  module NCurses
    VERSION = "0.3.4"
    extend FFI::Library

    # Use `RUBY_FFI_NCURSES_LIB` to specify a colon-separated list of
    # libs you want to try to load, e.g.
    #
    #     RUBY_FFI_NCURSES_LIB=XCurses:ncurses
    #
    # to try to load XCurses (from PDCurses) first, then ncurses.
    if ENV["RUBY_FFI_NCURSES_LIB"].to_s != ""
      LIB_HANDLE = ffi_lib(ENV["RUBY_FFI_NCURSES_LIB"].split(/:/)).first
    else
      LIB_HANDLE = ffi_lib(['ncursesw', 'ncurses']).first
    end

    begin
      # These global variables are defined in `ncurses.h`:
      #
      #     chtype acs_map[];
      #     WINDOW * curscr;
      #     WINDOW * newscr;
      #     WINDOW * stdscr;
      #     char ttytype[];
      #     int COLORS;
      #     int COLOR_PAIRS;
      #     int COLS;
      #     int ESCDELAY;
      #     int LINES;
      #     int TABSIZE;
      #
      # Note that the symbol table entry in a shared lib for an
      # exported variable contains a *pointer to the address* of
      # the variable which will be initialized in the process's
      # bss (uninitialized) data segment when the process is
      # initialized.

      # This is unlike methods, where the symbol table entry points to
      # the entry point of the method itself.

      # Variables need another level of indirection because they are
      # *not* shared between process instances - only code is shared.

      # So we define convenience methods to perform the lookup for
      # us. We can't just stash the value returned by
      # `read_pointer` at load time because it's not initialized
      # until after `initscr` has been called.
      symbols = [
                 ["acs_map", :pointer],
                 ["curscr", :pointer],
                 ["newscr", :pointer],
                 ["stdscr", :pointer],
                 ["ttytype", :string],
                 ["COLORS", :int],
                 ["COLOR_PAIRS", :int],
                 ["COLS", :int],
                 ["ESCDELAY", :int],
                 ["LINES", :int],
                 ["TABSIZE", :int],
                ]
      if LIB_HANDLE.respond_to?(:find_symbol)
        symbols.each do |sym, type|
          if handle = LIB_HANDLE.find_symbol(sym)
            define_method sym do
              handle.send("read_#{type}")
            end
            module_function sym
          end
        end
      else
        warn "#find_symbol not available - #{symbols.inspect} not defined"
      end
    rescue => e
    end

    # This list of function signatures was originally generated by the
    # file `generate-ffi-ncurses-function-signatures.rb` (in the
    # `./tools` directory), inserted here by hand then edited a bit.
    functions =
      [
       [:COLOR_PAIR, [:int], :int],
       [:PAIR_NUMBER, [:int], :int],
       [:add_wchnstr, [:pointer, :int], :int],
       [:add_wch, [:pointer], :int],
       [:add_wchstr, [:pointer], :int],
       [:addchnstr, [:pointer, :int], :int],
       [:addchstr, [:pointer], :int],
       [:addch, [:uint], :int],
       [:addnstr, [:string, :int], :int],
       [:addstr, [:string], :int],
       [:assume_default_colors, [:int, :int], :int],
       [:attr_get, [:pointer, :pointer, :pointer], :int],
       [:attr_off, [:uint, :pointer], :int],
       [:attr_on, [:uint, :pointer], :int],
       [:attr_set, [:uint, :short, :pointer], :int],
{'fraction_non_alphanumeric': 0.13571730254821907, 'fraction_numerical': 0.016331127692524285, 'mean_word_length': 2.189941625505164, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7513497', 'n_tokens_mistral': 2478, 'n_tokens_neox': 2316, 'n_words': 667}