/tutorial/ncurses/cursor.e

http://github.com/tybor/Liberty · Specman e · 120 lines · 90 code · 19 blank · 11 comment · 9 complexity · 14d5f7aa6652f38b8a2c22a8a082066a MD5 · raw file

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