/examples/curses.winxed

http://github.com/NotFound/winxed · Unknown · 157 lines · 140 code · 17 blank · 0 comment · 0 complexity · 56c2a147ba6908c2f61093341122534a MD5 · raw file

  1. #! winxed
  2. class Window
  3. {
  4. var win;
  5. var panel;
  6. function getmaxx()
  7. {
  8. using Curses.getmaxx;
  9. int x= getmaxx(self.win);
  10. return x;
  11. }
  12. function getmaxy()
  13. {
  14. using Curses.getmaxy;
  15. int y= getmaxy(self.win);
  16. return y;
  17. }
  18. function clear()
  19. {
  20. using Curses.wclear;
  21. wclear(self.win);
  22. }
  23. function mvaddstr(int y, int x, string s)
  24. {
  25. using Curses.mvwaddstr;
  26. mvwaddstr(self.win, y, x, s);
  27. }
  28. function getch()
  29. {
  30. using Curses.wgetch;
  31. string s= wgetch(self.win);
  32. return s;
  33. }
  34. function box(int verch, int horch)
  35. {
  36. using Curses.box;
  37. box(self.win, verch, horch);
  38. }
  39. function new_panel(int lines, int cols, int y, int x)
  40. {
  41. using Curses.derwin;
  42. using Curses.new_panel;
  43. var newwindow= new Window;
  44. var winscr = derwin(self.win, lines, cols, y, x);
  45. var panel= new_panel(winscr);
  46. newwindow.panel= panel;
  47. newwindow.win= winscr;
  48. return newwindow;
  49. }
  50. function top_panel()
  51. {
  52. using Curses.top_panel;
  53. top_panel(self.panel);
  54. }
  55. }
  56. class Screen : Window
  57. {
  58. function init [vtable]()
  59. {
  60. using extern Curses;
  61. using Curses.initscr;
  62. say('Hi');
  63. var win= initscr();
  64. self.win= win;
  65. }
  66. function endwin()
  67. {
  68. using Curses.endwin;
  69. endwin();
  70. }
  71. }
  72. function panel1(var p)
  73. {
  74. p.clear();
  75. p.box(0, 0);
  76. p.mvaddstr(2, 6, 'Uuuuh!');
  77. }
  78. function panel2(var p2, string s)
  79. {
  80. p2.clear();
  81. p2.box(0, 0);
  82. int width= p2.getmaxx();
  83. p2.mvaddstr(1, 1, substr(s, 0, width-3));
  84. }
  85. function test(scr, string s)
  86. {
  87. int maxx= scr.getmaxx();
  88. int maxy= scr.getmaxy();
  89. int l= length(s);
  90. int x = maxx - l;
  91. if (x < 0) x= 0;
  92. scr.mvaddstr(maxy / 2, x / 2, s);
  93. scr.getch();
  94. using Curses.update_panels;
  95. using Curses.doupdate;
  96. var p= scr.new_panel(maxy/2, maxx/2, maxy/4, maxx/4);
  97. panel1(p);
  98. update_panels();
  99. doupdate();
  100. p.getch();
  101. int width= l + 3;
  102. if( width > maxx) width= maxx;
  103. var p2= scr.new_panel(3, width, (maxy - 3)/2, (maxx - width) / 2);
  104. panel2(p2, s);
  105. update_panels();
  106. doupdate();
  107. p2.getch();
  108. p.top_panel();
  109. panel1(p);
  110. update_panels();
  111. doupdate();
  112. p.getch();
  113. p2.top_panel();
  114. panel2(p2, s);
  115. update_panels();
  116. doupdate();
  117. p2.getch();
  118. scr.endwin();
  119. say(maxx, ", ", maxy);
  120. }
  121. function main(args)
  122. {
  123. using extern Curses;
  124. using Curses.curses_version;
  125. say('Curses version: ', curses_version());
  126. string s;
  127. int argc = args;
  128. if (argc > 1) s= args[1];
  129. else s= 'Hello, world!';
  130. var scr;
  131. try {
  132. scr = new Screen;
  133. test(scr, s);
  134. }
  135. catch (e)
  136. {
  137. scr.endwin();
  138. throw e;
  139. }
  140. }