PageRenderTime 24ms CodeModel.GetById 20ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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