Module: RatatuiRuby::Event::Key::Dwim
- Included in:
- RatatuiRuby::Event::Key
- Defined in:
- lib/ratatui_ruby/event/key/dwim.rb
Overview
DWIM predicates for common key patterns.
These predicates anticipate what developers intuitively try. Space bars, character categories, Unix signals, and Vim-style navigation.
Constant Summary collapse
- NAVIGATION_KEYS =
:nodoc:
%w[up down left right home end page_up page_down].freeze
- ARROW_KEYS =
:nodoc:
%w[up down left right].freeze
- VIM_MOVEMENT_KEYS =
:nodoc:
%w[h j k l w b g G].freeze
- PUNCTUATION_NAMES =
Punctuation name predicates - generated at load time for performance. Maps intuitive names to their symbol characters. :nodoc:
{ # Navigation shortcuts (the original use case!) tilde: "~", slash: "/", forwardslash: "/", backslash: "\\", # Common punctuation comma: ",", period: ".", dot: ".", colon: ":", semicolon: ";", # Question and exclamation question: "?", questionmark: "?", exclamation: "!", exclamationmark: "!", exclamationpoint: "!", bang: "!", # Programming symbols at: "@", atsign: "@", hash: "#", pound: "#", numbersign: "#", dollar: "$", dollarsign: "$", percent: "%", caret: "^", circumflex: "^", ampersand: "&", asterisk: "*", star: "*", # Arithmetic and comparison underscore: "_", hyphen: "-", dash: "-", minus: "-", plus: "+", equals: "=", equalsign: "=", pipe: "|", bar: "|", lessthan: "<", lt: "<", greaterthan: ">", gt: ">", # Brackets and parens lparen: "(", leftparen: "(", openparen: "(", leftparenthesis: "(", openparenthesis: "(", rparen: ")", rightparen: ")", closeparen: ")", rightparenthesis: ")", closeparenthesis: ")", lbracket: "[", leftbracket: "[", openbracket: "[", leftsquarebracket: "[", opensquarebracket: "[", rbracket: "]", rightbracket: "]", closebracket: "]", rightsquarebracket: "]", closesquarebracket: "]", lbrace: "{", leftbrace: "{", openbrace: "{", leftcurlybrace: "{", opencurlybrace: "{", rbrace: "}", rightbrace: "}", closebrace: "}", rightcurlybrace: "}", closecurlybrace: "}", # Quotes backtick: "`", grave: "`", singlequote: "'", apostrophe: "'", doublequote: "\"", }.freeze
Instance Method Summary collapse
-
#alphanumeric? ⇒ Boolean
Returns true if the key is alphanumeric.
-
#arrow? ⇒ Boolean
Returns true if key is an arrow key.
-
#cancel? ⇒ Boolean
Returns true for cancel (Esc or Ctrl+C).
-
#cr? ⇒ Boolean
(also: #carriagereturn?, #linefeed?, #newline?, #lf?)
Returns true if the key is Enter.
-
#digit? ⇒ Boolean
Returns true if the key is a digit (0-9).
-
#eof? ⇒ Boolean
Returns true for end-of-file (Ctrl+D).
-
#interrupt? ⇒ Boolean
Returns true for interrupt (Ctrl+C).
-
#letter? ⇒ Boolean
Returns true if the key is a single letter (a-z, A-Z).
-
#navigation? ⇒ Boolean
Returns true if key is a navigation key.
-
#punctuation? ⇒ Boolean
Returns true if the key is punctuation.
-
#quit? ⇒ Boolean
(also: #sigquit?)
Returns true for SIGQUIT (Ctrl+).
-
#quote? ⇒ Boolean
quote? matches both single and double quotes.
-
#sigint? ⇒ Boolean
(also: #int?)
Returns true for SIGINT (Ctrl+C).
-
#space? ⇒ Boolean
(also: #spacebar?)
Returns true if the key is a space character.
-
#suspend? ⇒ Boolean
(also: #sigtstp?, #tstp?)
Returns true for SIGTSTP (Ctrl+Z) - suspend/stop.
-
#vim? ⇒ Boolean
Returns true if key is a Vim movement key.
-
#vim_bottom? ⇒ Boolean
Returns true for Vim go to bottom (G).
-
#vim_down? ⇒ Boolean
Returns true for Vim down (j).
-
#vim_left? ⇒ Boolean
Returns true for Vim left (h).
-
#vim_right? ⇒ Boolean
Returns true for Vim right (l).
-
#vim_top? ⇒ Boolean
Returns true for Vim go to top (gg pattern, here just g).
-
#vim_up? ⇒ Boolean
Returns true for Vim up (k).
-
#vim_word_backward? ⇒ Boolean
Returns true for Vim word backward (b).
-
#vim_word_forward? ⇒ Boolean
Returns true for Vim word forward (w).
-
#whitespace? ⇒ Boolean
Returns true if the key is whitespace (space, enter, tab).
Instance Method Details
#alphanumeric? ⇒ Boolean
54 55 56 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 54 def alphanumeric? letter? || digit? end |
#arrow? ⇒ Boolean
136 137 138 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 136 def arrow? ARROW_KEYS.include?(@code) && @modifiers.empty? end |
#cancel? ⇒ Boolean
Returns true for cancel (Esc or Ctrl+C).
event.cancel? # => true for Esc or Ctrl+C
90 91 92 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 90 def cancel? (@code == "esc" && @modifiers.empty?) || interrupt? end |
#cr? ⇒ Boolean Also known as: carriagereturn?, linefeed?, newline?, lf?
Returns true if the key is Enter. Alias for carriage return.
event.cr? # => true for enter
28 29 30 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 28 def cr? @code == "enter" && @modifiers.empty? end |
#digit? ⇒ Boolean
47 48 49 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 47 def digit? @code.length == 1 && @code.match?(/\A[0-9]\z/) end |
#eof? ⇒ Boolean
Returns true for end-of-file (Ctrl+D).
event.eof? # => true for Ctrl+D
83 84 85 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 83 def eof? @code == "d" && @modifiers == ["ctrl"] end |
#interrupt? ⇒ Boolean
Returns true for interrupt (Ctrl+C).
event.interrupt? # => true for Ctrl+C
76 77 78 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 76 def interrupt? @code == "c" && @modifiers == ["ctrl"] end |
#letter? ⇒ Boolean
40 41 42 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 40 def letter? @code.length == 1 && @code.match?(/\A[A-Za-z]\z/) end |
#navigation? ⇒ Boolean
127 128 129 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 127 def NAVIGATION_KEYS.include?(@code) && @modifiers.empty? end |
#punctuation? ⇒ Boolean
61 62 63 64 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 61 def punctuation? return false unless @code.length == 1 !letter? && !digit? && !whitespace? end |
#quit? ⇒ Boolean Also known as: sigquit?
Returns true for SIGQUIT (Ctrl+).
event.quit? # => true for Ctrl+\
116 117 118 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 116 def quit? @code == "\\" && @modifiers == ["ctrl"] end |
#quote? ⇒ Boolean
quote? matches both single and double quotes
295 296 297 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 295 def quote? @code == "'" || @code == "\"" end |
#sigint? ⇒ Boolean Also known as: int?
Returns true for SIGINT (Ctrl+C).
event.sigint? # => true for Ctrl+C
97 98 99 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 97 def sigint? interrupt? end |
#space? ⇒ Boolean Also known as: spacebar?
Returns true if the key is a space character.
event.space? # => true for " "
19 20 21 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 19 def space? @code == " " && @modifiers.empty? end |
#suspend? ⇒ Boolean Also known as: sigtstp?, tstp?
Returns true for SIGTSTP (Ctrl+Z) - suspend/stop.
event.suspend? # => true for Ctrl+Z
106 107 108 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 106 def suspend? @code == "z" && @modifiers == ["ctrl"] end |
#vim? ⇒ Boolean
145 146 147 148 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 145 def vim? return true if VIM_MOVEMENT_KEYS.include?(@code) && @modifiers.empty? @code == "G" && @modifiers == ["shift"] end |
#vim_bottom? ⇒ Boolean
Returns true for Vim go to bottom (G).
186 187 188 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 186 def vim_bottom? @code == "G" && @modifiers == ["shift"] end |
#vim_down? ⇒ Boolean
Returns true for Vim down (j).
156 157 158 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 156 def vim_down? @code == "j" && @modifiers.empty? end |
#vim_left? ⇒ Boolean
Returns true for Vim left (h).
151 152 153 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 151 def vim_left? @code == "h" && @modifiers.empty? end |
#vim_right? ⇒ Boolean
Returns true for Vim right (l).
166 167 168 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 166 def vim_right? @code == "l" && @modifiers.empty? end |
#vim_top? ⇒ Boolean
Returns true for Vim go to top (gg pattern, here just g).
181 182 183 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 181 def vim_top? @code == "g" && @modifiers.empty? end |
#vim_up? ⇒ Boolean
Returns true for Vim up (k).
161 162 163 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 161 def vim_up? @code == "k" && @modifiers.empty? end |
#vim_word_backward? ⇒ Boolean
Returns true for Vim word backward (b).
176 177 178 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 176 def vim_word_backward? @code == "b" && @modifiers.empty? end |
#vim_word_forward? ⇒ Boolean
Returns true for Vim word forward (w).
171 172 173 |
# File 'lib/ratatui_ruby/event/key/dwim.rb', line 171 def vim_word_forward? @code == "w" && @modifiers.empty? end |