Add luacurses to contrib
[project/luci.git] / contrib / luacurses / src / test / mouse.lua
1
2 require("curses");
3
4 function show_message(m)
5   local width = string.len(m) + 6;
6   local win = curses.newwin(5, width, (lines - 5) / 2, (cols - width) / 2);
7   win:keypad(true);
8   win:attron(curses.COLOR_PAIR(curses.COLOR_RED));
9   win:box('|', '-', '+');
10   win:mvaddstr(2, 3, m);
11   win:refresh();
12   win:getch();
13   win:delwin();
14 end
15
16 curses.initscr();
17 curses.start_color();
18 curses.init_pair(curses.COLOR_BLUE, curses.COLOR_BLUE, curses.COLOR_WHITE);
19 curses.init_pair(curses.COLOR_RED, curses.COLOR_RED, curses.COLOR_WHITE);
20 curses.cbreak();
21 curses.noecho();
22 curses.keypad(curses.stdscr(), true);
23
24 lines = curses.LINES();
25 cols = curses.COLS();
26
27 mmasks =
28 {
29     curses.BUTTON1_CLICKED,
30     curses.BUTTON2_CLICKED,
31     curses.BUTTON3_CLICKED,
32     curses.BUTTON4_CLICKED
33 };
34
35 table.foreachi(mmasks, function(_i, _m) curses.addmousemask(_m) end);
36 curses.attron(curses.COLOR_PAIR(curses.COLOR_BLUE));
37 curses.attron(curses.A_BOLD);
38 curses.mvaddstr((lines - 5) / 2, (cols - 10) / 2, "click");
39
40 curses.refresh();
41 while(true) do
42     local c = curses.getch();
43     if (c == curses.KEY_MOUSE) then
44         local r, id, x, y, z, bstate = curses.getmouse();
45         if (r) then
46             show_message("id = " .. id .. ", x = " .. x .. ", y = " .. y .. ", z = " .. z .. ", bstate = " ..
47                          string.format("0x%x", bstate));
48         end
49         break;
50     end
51 end
52
53 curses.endwin();
54