Class: Diakonos::InteractionHandler
- Defined in:
- lib/diakonos/interaction-handler.rb
Constant Summary collapse
- CHOICE_STRINGS =
[ '(n)o', '(y)es', '(a)ll', '(c)ancel', 'y(e)s to all', 'n(o) to all', 'yes and (s)top', '(d)elete', 'all (w)ithin selection', ]
- CHOICE_KEYS =
[ ["n".ord, "N".ord], ["y".ord, "Y".ord], ["a".ord, "A".ord], ["c".ord, "C".ord, ESCAPE, CTRL_C, CTRL_D, CTRL_Q], ["e".ord], ["o".ord], ["s".ord], ["d".ord], ["w".ord], ]
Instance Method Summary collapse
- #get_char(prompt) ⇒ Object
-
#get_choice(prompt, choices, default = nil) ⇒ Object
choices should be an array of CHOICE_* constants.
-
#initialize(win_main:, win_interaction:, cursor_manager:, testing: false, choice_delay: 3, blink_string: "*" * 60, blink_duration: 0.05) ⇒ InteractionHandler
constructor
TODO: Move win_interaction creation, etc.
-
#set_iline(string = "") ⇒ Object
Display text on the interaction line.
- #set_iline_if_empty(string) ⇒ Object
Constructor Details
#initialize(win_main:, win_interaction:, cursor_manager:, testing: false, choice_delay: 3, blink_string: "*" * 60, blink_duration: 0.05) ⇒ InteractionHandler
TODO: Move win_interaction creation, etc. into this class.
If necessary, expose it with attr_reader
e.g. for @modes[ 'input' ].window = @win_interaction
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/diakonos/interaction-handler.rb', line 53 def initialize( win_main:, win_interaction:, cursor_manager:, testing: false, choice_delay: 3, blink_string: "*" * 60, blink_duration: 0.05 ) @win_main = win_main @win_interaction = win_interaction @cursor_manager = cursor_manager @testing = testing @choice_iterations = 0 @choice_delay = choice_delay @blink_string = blink_string @blink_duration = blink_duration end |
Instance Method Details
#get_char(prompt) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/diakonos/interaction-handler.rb', line 93 def get_char(prompt) set_iline prompt char = @win_main.getch set_iline char end |
#get_choice(prompt, choices, default = nil) ⇒ Object
choices should be an array of CHOICE_* constants. default is what is returned when Enter is pressed.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/diakonos/interaction-handler.rb', line 102 def get_choice( prompt, choices, default = nil ) retval = @iterated_choice if retval @choice_iterations -= 1 if @choice_iterations < 1 @iterated_choice = nil $diakonos.do_display = true end return retval end @saved_main_x = @win_main.curx @saved_main_y = @win_main.cury msg = prompt + " " choice_strings = choices.collect do |choice| CHOICE_STRINGS[ choice ] end msg << choice_strings.join( ", " ) if default set_iline msg else msg end while retval.nil? ch = @win_interaction.getch if ch c = ch.ord else next end case c when Curses::KEY_NPAGE @cursor_manager.page_down when Curses::KEY_PPAGE @cursor_manager.page_up else if @message_expiry && Time.now < @message_expiry interaction_blink msg else case c when ENTER retval = default when '0'.ord..'9'.ord if @choice_iterations < 1 @choice_iterations = ( c - '0'.ord ) else @choice_iterations = @choice_iterations * 10 + ( c - '0'.ord ) end else choices.each do |choice| if CHOICE_KEYS[ choice ].include? c retval = choice break end end end if retval.nil? interaction_blink( msg ) end end end end set_iline if @choice_iterations > 0 @choice_iterations -= 1 @iterated_choice = retval $diakonos.do_display = false end retval end |
#set_iline(string = "") ⇒ Object
Display text on the interaction line.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/diakonos/interaction-handler.rb', line 74 def set_iline(string = "") return 0 if @testing return 0 if $diakonos.readline @iline = string Curses::curs_set 0 @win_interaction.setpos( 0, 0 ) @win_interaction.addstr( "%-#{Curses::cols}s" % @iline ) @win_interaction.refresh Curses::curs_set 1 string.length end |
#set_iline_if_empty(string) ⇒ Object
87 88 89 90 91 |
# File 'lib/diakonos/interaction-handler.rb', line 87 def set_iline_if_empty(string) if @iline.nil? || @iline.empty? set_iline string end end |