diff options
author | Steven Barth <steven@midlink.org> | 2008-08-31 11:14:47 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2008-08-31 11:14:47 +0000 |
commit | 36c76090638e80d1cb9ad2dd060aae65976f24c8 (patch) | |
tree | eacc03904f5b6d649e71975dbfacd836fbb149ed /contrib/luacurses/src/test | |
parent | f28422324d9f8aa6ac0af58dca431fa466e8b183 (diff) |
Add luacurses to contrib
Diffstat (limited to 'contrib/luacurses/src/test')
-rw-r--r-- | contrib/luacurses/src/test/filter.lua | 49 | ||||
-rw-r--r-- | contrib/luacurses/src/test/getnstr.lua | 12 | ||||
-rw-r--r-- | contrib/luacurses/src/test/getyx.lua | 13 | ||||
-rw-r--r-- | contrib/luacurses/src/test/hello.lua | 20 | ||||
-rw-r--r-- | contrib/luacurses/src/test/mouse.lua | 54 | ||||
-rw-r--r-- | contrib/luacurses/src/test/pair.lua | 18 | ||||
-rw-r--r-- | contrib/luacurses/src/test/rain.lua | 89 |
7 files changed, 255 insertions, 0 deletions
diff --git a/contrib/luacurses/src/test/filter.lua b/contrib/luacurses/src/test/filter.lua new file mode 100644 index 0000000000..f8f6ef5824 --- /dev/null +++ b/contrib/luacurses/src/test/filter.lua @@ -0,0 +1,49 @@ + +require("curses"); + +function read_cmd() + curses.attron(curses.A_BOLD); + curses.addstr("Command: "); + curses.attron(underline); + local s = ""; + while (true) do + local c = string.char(curses.getch()); + if (c == '\n') then break; end + s = s .. c; + end + curses.attroff(underline); + curses.attroff(curses.A_BOLD); + curses.addch("\n"); + + return s; +end + + +curses.filter(); +curses.initscr(); +curses.cbreak(); +curses.keypad(curses.stdscr(), TRUE); + +if (curses.has_colors()) then + curses.start_color(); + curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK); + underline = curses.COLOR_PAIR(1); +else + underline = curses.A_UNDERLINE; +end + +while (true) do + local s = read_cmd(); + if (s == "exit") then break; end + curses.reset_shell_mode(); + io.write("\n"); + io.flush(io.stdout); + os.execute(s); + curses.reset_prog_mode(); + curses.touchwin(curses.stdscr()); + curses.erase(); + curses.refresh(); +end + +curses.endwin(); + diff --git a/contrib/luacurses/src/test/getnstr.lua b/contrib/luacurses/src/test/getnstr.lua new file mode 100644 index 0000000000..172365ea90 --- /dev/null +++ b/contrib/luacurses/src/test/getnstr.lua @@ -0,0 +1,12 @@ + +require("curses"); + +curses.initscr(); + +curses.keypad(curses.stdscr(), true); +s = curses.mvgetnstr(10, 10, 10); +curses.addstr(s); +curses.getch(); + +curses.endwin(); + diff --git a/contrib/luacurses/src/test/getyx.lua b/contrib/luacurses/src/test/getyx.lua new file mode 100644 index 0000000000..4600236bb1 --- /dev/null +++ b/contrib/luacurses/src/test/getyx.lua @@ -0,0 +1,13 @@ + +require("curses"); + +curses.initscr(); +while (true) do + local s = curses.getnstr(1000); + curses.addstr(s); + curses.addstr(":" .. table.concat({curses.getyx(curses.stdscr())}, ' ') .. "\n"); + if (s == "exit") then break; end +end + +curses.endwin(); + diff --git a/contrib/luacurses/src/test/hello.lua b/contrib/luacurses/src/test/hello.lua new file mode 100644 index 0000000000..6a1d28a216 --- /dev/null +++ b/contrib/luacurses/src/test/hello.lua @@ -0,0 +1,20 @@ + +require("curses"); + +function show_message(message) + local width = string.len(message) + 6; + win = curses.newwin(5, width, (curses.LINES() - 5) / 2, (curses.COLS() - width) / 2); + win:box('|', '-'); + win:mvaddstr(2, 3, message); + win:getch(); + win:delwin(); +end + +curses.initscr(); +curses.cbreak(); +curses.mvaddstr((curses.LINES() - 5) / 2, (curses.COLS() - 10) / 2, "Hit any key"); +curses.getch(); +show_message("Hello, World!") + +curses.endwin(); + diff --git a/contrib/luacurses/src/test/mouse.lua b/contrib/luacurses/src/test/mouse.lua new file mode 100644 index 0000000000..0129cd8c42 --- /dev/null +++ b/contrib/luacurses/src/test/mouse.lua @@ -0,0 +1,54 @@ + +require("curses"); + +function show_message(m) + local width = string.len(m) + 6; + local win = curses.newwin(5, width, (lines - 5) / 2, (cols - width) / 2); + win:keypad(true); + win:attron(curses.COLOR_PAIR(curses.COLOR_RED)); + win:box('|', '-', '+'); + win:mvaddstr(2, 3, m); + win:refresh(); + win:getch(); + win:delwin(); +end + +curses.initscr(); +curses.start_color(); +curses.init_pair(curses.COLOR_BLUE, curses.COLOR_BLUE, curses.COLOR_WHITE); +curses.init_pair(curses.COLOR_RED, curses.COLOR_RED, curses.COLOR_WHITE); +curses.cbreak(); +curses.noecho(); +curses.keypad(curses.stdscr(), true); + +lines = curses.LINES(); +cols = curses.COLS(); + +mmasks = +{ + curses.BUTTON1_CLICKED, + curses.BUTTON2_CLICKED, + curses.BUTTON3_CLICKED, + curses.BUTTON4_CLICKED +}; + +table.foreachi(mmasks, function(_i, _m) curses.addmousemask(_m) end); +curses.attron(curses.COLOR_PAIR(curses.COLOR_BLUE)); +curses.attron(curses.A_BOLD); +curses.mvaddstr((lines - 5) / 2, (cols - 10) / 2, "click"); + +curses.refresh(); +while(true) do + local c = curses.getch(); + if (c == curses.KEY_MOUSE) then + local r, id, x, y, z, bstate = curses.getmouse(); + if (r) then + show_message("id = " .. id .. ", x = " .. x .. ", y = " .. y .. ", z = " .. z .. ", bstate = " .. + string.format("0x%x", bstate)); + end + break; + end +end + +curses.endwin(); + diff --git a/contrib/luacurses/src/test/pair.lua b/contrib/luacurses/src/test/pair.lua new file mode 100644 index 0000000000..f269800613 --- /dev/null +++ b/contrib/luacurses/src/test/pair.lua @@ -0,0 +1,18 @@ + +require("curses"); + +curses.initscr(); + +curses.start_color(); +curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_YELLOW); +curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_RED); + +for i = 1, 2 do + local r, f, b = curses.pair_content(i); + curses.attrset(curses.COLOR_PAIR(i)); + curses.addstr(f .. ", " .. b .. "\n"); +end + +curses.getch(); +curses.endwin(); + diff --git a/contrib/luacurses/src/test/rain.lua b/contrib/luacurses/src/test/rain.lua new file mode 100644 index 0000000000..35e4691021 --- /dev/null +++ b/contrib/luacurses/src/test/rain.lua @@ -0,0 +1,89 @@ + +require("curses"); + +curses.initscr(); +curses.nl(); +curses.noecho(); + + +if (curses.has_colors()) then + curses.start_color(); + curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_BLACK); + curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK); +end + +curses.curs_set(0); +curses.timeout(0); + +math.randomseed(os.time()); + +lines = curses.LINES(); +cols = curses.COLS(); + +xpos = {}; +ypos = {}; +r = lines - 4; +c = cols - 4; +for i = 0, 4 do + xpos[i] = c * math.random() + 2; + ypos[i] = r * math.random() + 2; +end + +function dec(i, max) + if (curses.has_colors()) then + local z = 3 * math.random(); + local c = curses.COLOR_PAIR(z); + curses.attrset(c); + if (math.floor(z) > 0) then + curses.attron(curses.A_BOLD); + end + end + + if (i > 0) then return i - 1; + else return max; + end +end + +i = 0; +while(true) do + x = c * math.random() + 2; + y = r * math.random() + 2; + + curses.mvaddstr(y, x, "."); + + curses.mvaddstr(ypos[i], xpos[i], "o"); + + i = dec(i, 4); + curses.mvaddstr(ypos[i], xpos[i], "O"); + + i = dec(i, 4); + curses.mvaddstr(ypos[i] - 1, xpos[i], "-"); + curses.mvaddstr(ypos[i], xpos[i] - 1, "|.|"); + curses.mvaddstr(ypos[i] + 1, xpos[i], "-"); + + i = dec(i, 4); + curses.mvaddstr(ypos[i] - 2, xpos[i], "-"); + curses.mvaddstr(ypos[i] - 1, xpos[i] - 1, "/ \\"); + curses.mvaddstr(ypos[i], xpos[i] - 2, "| O |"); + curses.mvaddstr(ypos[i] + 1, xpos[i] - 1, "\\ /"); + curses.mvaddstr(ypos[i] + 2, xpos[i], "-"); + + i = dec(i, 4); + curses.mvaddstr(ypos[i] - 2, xpos[i], " "); + curses.mvaddstr(ypos[i] - 1, xpos[i] - 1, " "); + curses.mvaddstr(ypos[i], xpos[i] - 2, " "); + curses.mvaddstr(ypos[i] + 1, xpos[i] - 1, " "); + curses.mvaddstr(ypos[i] + 2, xpos[i], " "); + + + xpos[i] = x; + ypos[i] = y; + + local ch = curses.getch(); + if (ch == string.byte('q', 1)) or (ch == string.byte('Q', 1)) then break; end + curses.refresh(); + curses.napms(50); +end + +curses.endwin(); + |