sup

A curses threads-with-tags style email client

ncursesw-ruby.git

git clone https://supmua.dev/git/ncursesw-ruby/

lib/ncurses_sugar.rb (8683B) - raw

      1 # encoding: utf-8
      2 # ncurses-ruby is a ruby module for accessing the FSF's ncurses library
      3 # (C) 2002, 2003, 2004 Tobias Peters <t-peters@users.berlios.de>
      4 # (C) 2004 Simon Kaczor <skaczor@cox.net>
      5 # (C) 2005 2011 Tobias Herzke
      6 #
      7 # This module is free software; you can redistribute it and/or
      8 # modify it under the terms of the GNU Lesser General Public
      9 # License as published by the Free Software Foundation; either
     10 # version 2 of the License, or (at your option) any later version.
     11 #
     12 # This module is distributed in the hope that it will be useful,
     13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15 # Lesser General Public License for more details.
     16 #
     17 # You should have received a copy of the GNU Lesser General Public
     18 # License along with this module; if not, write to the Free Software
     19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
     20 
     21 # $Id: ncurses_sugar.rb,v 1.1 2011-05-30 23:05:50 t-peters Exp $
     22 
     23 # Ncurses constants with leading underscore
     24 def Ncurses._XOPEN_CURSES
     25   Ncurses::XOPEN_CURSES
     26 end
     27 def Ncurses._SUBWIN
     28   Ncurses::SUBWIN
     29 end
     30 def Ncurses._ENDLINE
     31   Ncurses::ENDLINE
     32 end
     33 def Ncurses._FULLWIN
     34   Ncurses::FULLWIN
     35 end
     36 def Ncurses._SCROLLWIN
     37   Ncurses::SCROLLWIN
     38 end
     39 def Ncurses._ISPAD
     40   Ncurses::ISPAD
     41 end
     42 def Ncurses._HASMOVED
     43   Ncurses::HASMOVED
     44 end
     45 def Ncurses._WRAPPED
     46   Ncurses::WRAPPED
     47 end
     48 def Ncurses._NOCHANGE
     49   Ncurses::NOCHANGE
     50 end
     51 def Ncurses._NEWINDEX
     52   Ncurses::NEWINDEX
     53 end
     54 
     55 
     56 module Ncurses
     57   module Destroy_checker; def destroyed?; @destroyed; end; end
     58   class WINDOW
     59     include Destroy_checker
     60     def method_missing(name, *args, &block)
     61       name = name.to_s
     62       if (name[0,2] == "mv")
     63         test_name = name.dup
     64         test_name[2,0] = "w" # insert "w" after"mv"
     65         if (Ncurses.respond_to?(test_name))
     66           return Ncurses.send(test_name, self, *args, &block)
     67         end
     68       end
     69       test_name = "w" + name
     70       if (Ncurses.respond_to?(test_name))
     71         return Ncurses.send(test_name, self, *args, &block)
     72       end
     73       Ncurses.send(name, self, *args, &block)
     74     end
     75     def respond_to?(name)
     76       name = name.to_s
     77       if (name[0,2] == "mv" && Ncurses.respond_to?("mvw" + name[2..-1]))
     78         return true
     79       end
     80       Ncurses.respond_to?("w" + name) || Ncurses.respond_to?(name)
     81     end
     82     def del
     83       Ncurses.delwin(self)
     84     end
     85     alias delete del
     86     def WINDOW.new(*args)
     87       Ncurses.newwin(*args)
     88     end
     89   end
     90   class SCREEN
     91     include Destroy_checker
     92     def del
     93       Ncurses.delscreen(self)
     94     end
     95     alias delete del
     96   end
     97   class MEVENT
     98     attr_accessor :id, :x,:y,:z, :bstate
     99   end
    100   GETSTR_LIMIT = 1024
    101 
    102   module Panel
    103     class PANEL; end
    104   end
    105 
    106   module Form
    107     class FORM
    108       attr_reader :user_object
    109 
    110       # This placeholder replaces the form_userptr function in curses
    111       def user_object=(obj)
    112         @user_object = obj
    113       end
    114     end
    115 
    116     class FIELD
    117       attr_reader :user_object
    118 
    119       # This placeholder replaces the field_userptr function in curses
    120       def user_object=(obj)
    121         @user_object = obj
    122       end
    123     end
    124 
    125     class FIELDTYPE
    126     end
    127   end
    128 
    129   module Menu
    130     class MENU
    131       attr_reader :user_object
    132 
    133       # This placeholder replaces the menu_userptr function in curses
    134       def user_object=(obj)
    135         @user_object = obj
    136       end
    137     end
    138 
    139     class ITEM
    140       attr_reader :user_object
    141 
    142       # This placeholder replaces the item_userptr function in curses
    143       def user_object=(obj)
    144         @user_object = obj
    145       end
    146     end
    147   end
    148 end
    149 def Ncurses.inchnstr(str,n)
    150   Ncurses.winchnstr(Ncurses.stdscr, str, n)
    151 end
    152 def Ncurses.inchstr(str)
    153   Ncurses.winchstr(Ncurses.stdscr, str)
    154 end
    155 def Ncurses.mvinchnstr(y,x, str, n)
    156   Ncurses.mvwinchnstr(Ncurses.stdscr, y,x, str, n)
    157 end
    158 def Ncurses.mvinchstr(y,x, str)
    159   Ncurses.mvwinchstr(Ncurses.stdscr, y,x, str)
    160 end
    161 def Ncurses.mvwinchnstr(win, y,x, str, n)
    162   if (Ncurses.wmove(win,y,x) == Ncurses::ERR)
    163     Ncurses::ERR
    164   else
    165     Ncurses.winchnstr(win,str,n)
    166   end
    167 end
    168 def Ncurses.mvwinchstr(win, y,x, str)
    169   maxy = []; maxx = []; getmaxyx(win, maxy,maxx)
    170   return Ncurses::ERR if (maxx[0] == Ncurses::ERR)
    171   Ncurses.mvwinchnstr(win, y,x, str, maxx[0]+1)
    172 end
    173 def Ncurses.winchstr(win, str)
    174   maxy = []; maxx = []; getmaxyx(win, maxy,maxx)
    175   return Ncurses::ERR if (maxx[0] == Ncurses::ERR)
    176   Ncurses.winchnstr(win, str, maxx[0]+1)
    177 end
    178 
    179 def Ncurses.getnstr(str,n)
    180   Ncurses.wgetnstr(Ncurses.stdscr, str, n)
    181 end
    182 def Ncurses.mvgetnstr(y,x, str, n)
    183   Ncurses.mvwgetnstr(Ncurses.stdscr, y,x, str, n)
    184 end
    185 def Ncurses.mvwgetnstr(win, y,x, str, n)
    186   if (Ncurses.wmove(win,y,x) == Ncurses::ERR)
    187     Ncurses::ERR
    188   else
    189     Ncurses.wgetnstr(win,str,n)
    190   end
    191 end
    192 
    193 def Ncurses.innstr(str,n)
    194   Ncurses.winnstr(Ncurses.stdscr, str, n)
    195 end
    196 def Ncurses.instr(str)
    197   Ncurses.winstr(Ncurses.stdscr, str)
    198 end
    199 def Ncurses.mvinnstr(y,x, str, n)
    200   Ncurses.mvwinnstr(Ncurses.stdscr, y,x, str, n)
    201 end
    202 def Ncurses.mvinstr(y,x, str)
    203   Ncurses.mvwinstr(Ncurses.stdscr, y,x, str)
    204 end
    205 def Ncurses.mvwinnstr(win, y,x, str, n)
    206   if (Ncurses.wmove(win,y,x) == Ncurses::ERR)
    207     Ncurses::ERR
    208   else
    209     Ncurses.winnstr(win,str,n)
    210   end
    211 end
    212 def Ncurses.mvwinstr(win, y,x, str)
    213   maxy = []; maxx = []; getmaxyx(win, maxy,maxx)
    214   return Ncurses::ERR if (maxx[0] == Ncurses::ERR)
    215   Ncurses.mvwinnstr(win, y,x, str, maxx[0]+1)
    216 end
    217 def Ncurses.winstr(win, str)
    218   maxy = []; maxx = []; getmaxyx(win, maxy,maxx)
    219   return Ncurses::ERR if (maxx[0] == Ncurses::ERR)
    220   Ncurses.winnstr(win, str, maxx[0]+1)
    221 end
    222 
    223 def Ncurses.mouse_trafo(pY, pX, to_screen)
    224   Ncurses.wmouse_trafo(Ncurses.stdscr, pY, pX, to_screen)
    225 end
    226 
    227 def Ncurses.getcurx(win)
    228   x = []; y = []; Ncurses.getyx(win, y,x); x[0]
    229 end
    230 def Ncurses.getcury(win)
    231   x = []; y = []; Ncurses.getyx(win, y,x); y[0]
    232 end
    233 def Ncurses.getbegx(win)
    234   x = []; y = []; Ncurses.getbegyx(win, y,x); x[0]
    235 end
    236 def Ncurses.getbegy(win)
    237   x = []; y = []; Ncurses.getbegyx(win, y,x); y[0]
    238 end
    239 def Ncurses.getmaxx(win)
    240   x = []; y = []; Ncurses.getmaxyx(win, y,x); x[0]
    241 end
    242 def Ncurses.getmaxy(win)
    243   x = []; y = []; Ncurses.getmaxyx(win, y,x); y[0]
    244 end
    245 def Ncurses.getparx(win)
    246   x = []; y = []; Ncurses.getparyx(win, y,x); x[0]
    247 end
    248 def Ncurses.getpary(win)
    249   x = []; y = []; Ncurses.getparyx(win, y,x); y[0]
    250 end
    251 def Ncurses.erase
    252   Ncurses.werase(Ncurses.stdscr)
    253 end
    254 def Ncurses.getstr(str)
    255   Ncurses.getnstr(str, Ncurses::GETSTR_LIMIT)
    256 end
    257 def Ncurses.mvgetstr(y,x, str)
    258   Ncurses.mvgetnstr(y,x, str, Ncurses::GETSTR_LIMIT)
    259 end
    260 def Ncurses.mvwgetstr(win, y,x, str)
    261   Ncurses.mvwgetnstr(win, y,x, str, Ncurses::GETSTR_LIMIT)
    262 end
    263 def Ncurses.wgetstr(win, str)
    264   Ncurses.wgetnstr(win, str, Ncurses::GETSTR_LIMIT)
    265 end
    266 
    267 def Ncurses.scanw(format, result)
    268   Ncurses.wscanw(Ncurses.stdscr, format, result)
    269 end
    270 def Ncurses.mvscanw(y,x, format, result)
    271   Ncurses.mvwscanw(Ncurses.stdscr, y,x, format, result)
    272 end
    273 def Ncurses.mvwscanw(win, y,x, format, result)
    274   if (Ncurses.wmove(win, y,x) == Ncurses::ERR)
    275     Ncurses::ERR
    276   else
    277     Ncurses.wscanw(win, format, result)
    278   end
    279 end
    280 def Ncurses.wscanw(win, format, result)
    281   str = ""
    282   if (Ncurses.wgetstr(win, str) == Ncurses::ERR)
    283     Ncurses::ERR
    284   else
    285     require "scanf.rb" # Use ruby's implementation of scanf
    286     result.replace(str.scanf(format))
    287   end
    288 end
    289 
    290 def Ncurses.mvprintw(*args)
    291   Ncurses.mvwprintw(Ncurses.stdscr, *args)
    292 end
    293 def Ncurses.mvwprintw(win, y,x, *args)
    294   if (Ncurses.wmove(win,y,x) == Ncurses::ERR)
    295     Ncurses::ERR
    296   else
    297     wprintw(win, *args)
    298   end
    299 end
    300 def Ncurses.printw(*args)
    301   Ncurses.wprintw(Ncurses.stdscr, *args)
    302 end
    303 def Ncurses.touchline(win, start, count)
    304   Ncurses.wtouchln(win, start, count, 1)
    305 end
    306 def Ncurses.touchwin(win)
    307   wtouchln(win, 0, getmaxy(win), 1)
    308 end
    309 
    310 module Ncurses
    311   Ncurses = self # for accessing Ncurses from a Module that includes Ncurses
    312 
    313   # Some users like to include ncurses names despite namespace pollution
    314   # This module is for them
    315   module Namespace
    316     def self.append_features(target)
    317       # include constants
    318       unless target.ancestors.member?(Ncurses)
    319         target.__send__(:include, Ncurses)
    320       end
    321 
    322       # make methods available
    323       unless target.respond_to?(:pre_Ncurses_method_missing)
    324         target.module_eval{
    325           alias pre_Ncurses_method_missing method_missing
    326           def method_missing(name, *args, &block)
    327             if Ncurses.respond_to?(name)
    328               Ncurses.send(name, *args, &block)
    329             else
    330               pre_Ncurses_method_missing(name, *args, &block)
    331             end
    332           end
    333         }
    334       end
    335     end
    336     def self.entend_object(object)
    337       class << object
    338         self
    339       end.__send__(:include, self)
    340     end
    341   end
    342 end