/tutorial/ncurses/cursor.e
Specman e | 120 lines | 90 code | 19 blank | 11 comment | 9 complexity | 14d5f7aa6652f38b8a2c22a8a082066a MD5 | raw file
1class CURSOR 2 -- 3 -- Playing with the CURSOR position in the NCURSES window. 4 -- 5 6insert 7 NCURSES_TOOLS 8 NCURSES_KEYS 9 CHARACTER_CONSTANTS 10 11create {} 12 main 13 14feature {} 15 main 16 do 17 ncurses.enable 18 root_window := ncurses.get_root_window 19 -- No more echo while keyboard keys are typed: 20 21 ncurses.set_echoing_policy(False) 22 23 root_window.put_string("You can move the cursor using arrows of your keyboard...%N") 24 root_window.put_string("or Ctrl-F Ctrl-B Ctrl-N Ctrl-P%N") 25 root_window.put_string("Q for quit.") 26 27 ncurses.when_key_pressed(agent key_press(?)) 28 ncurses.start 29 end 30 31 key_press (key_code: INTEGER) 32 local 33 cursor_x, cursor_y: INTEGER 34 do 35 cursor_x := root_window.cursor_x 36 cursor_y := root_window.cursor_y 37 integer_info_at_line(4, once "cursor X = ", cursor_x) 38 integer_info_at_line(5, once "cursor Y = ", cursor_y) 39 40 root_window.set_cursor(cursor_x, cursor_y) 41 42 integer_info_at_line(6, once "key code = ", key_code) 43 44 if key_code = key_resize then 45 cursor_x := cursor_x.min(root_window.width - 1) 46 cursor_y := cursor_y.min(root_window.height - 1) 47 end 48 -- Back to the cursor position: 49 root_window.set_cursor(cursor_x, cursor_y) 50 51 inspect 52 translate_to_unique_character_code(key_code) 53 when Ctrl_b then 54 -- Backward: 55 if root_window.valid_cursor_x(cursor_x - 1) then 56 cursor_x := cursor_x - 1 57 end 58 59 root_window.set_cursor(cursor_x, cursor_y) 60 when Ctrl_f then 61 -- Forward: 62 if root_window.valid_cursor_x(cursor_x + 1) then 63 cursor_x := cursor_x + 1 64 end 65 66 root_window.set_cursor(cursor_x, cursor_y) 67 when Ctrl_n then 68 -- Next line: 69 if root_window.valid_cursor_y(cursor_y + 1) then 70 cursor_y := cursor_y + 1 71 end 72 73 root_window.set_cursor(cursor_x, cursor_y) 74 when Ctrl_p then 75 -- Previous line: 76 if root_window.valid_cursor_y(cursor_y - 1) then 77 cursor_y := cursor_y - 1 78 end 79 80 root_window.set_cursor(cursor_x, cursor_y) 81 when Ctrl_a then 82 -- Beginning of line: 83 cursor_x := 0 84 root_window.set_cursor(cursor_x, cursor_y) 85 when Ctrl_e then 86 -- End of line: 87 cursor_x := root_window.width - 1 88 root_window.set_cursor(cursor_x, cursor_y) 89 else -- No move. 90 end 91 if key_code.to_character.to_upper = 'Q' then 92 ncurses.disable 93 end 94 end 95 96 root_window: NCURSES_WINDOW 97 98 integer_info_at_line (line_number: INTEGER; label: STRING; value: INTEGER) 99 do 100 root_window.put_string_at(label, 0, line_number) 101 root_window.clear_to_end_of_line 102 root_window.put_integer(value) 103 end 104 105 translate_to_unique_character_code (key_code: INTEGER): CHARACTER 106 do 107 if key_code = key_left then 108 Result := Ctrl_b 109 elseif key_code = key_right then 110 Result := Ctrl_f 111 elseif key_code = key_up then 112 Result := Ctrl_p 113 elseif key_code = key_down then 114 Result := Ctrl_n 115 else 116 Result := key_code.to_character 117 end 118 end 119 120end -- class CURSOR