Skip to content

Commit

Permalink
[Terminal] Throw together a prototype library for working with the te…
Browse files Browse the repository at this point in the history
…rminal. Right now, it's like a non-portable ncurses-lite, because I'm too lazy to write a proper ncurses library
  • Loading branch information
Whiteknight committed Sep 5, 2012
1 parent 91d12c8 commit ad46d6f
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
6 changes: 6 additions & 0 deletions setup.winxed
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,12 @@ function setup_experimental_libraries(var rosella)
"feed/Entry",
"feed/Writer"
);

setup_unstable_lib(rosella, "terminal", ["Core"],
"terminal/Includes",
"terminal/Terminal",
"terminal/Decorations"
);
}

function setup_utilities(var rosella)
Expand Down
20 changes: 20 additions & 0 deletions src/unstable/terminal/Decorations.winxed
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Rosella.Terminal.Decorations
{
function cycle_while(var term, string s, var cond, float frame_time = 0.1)
{
term.print(" ");
while(cond() == true) {
for (int i = 0; i < length(s); i++) {
term.print("\b" + substr(s, i, 1));
term.flush();
sleep(frame_time);
}
}
term.print("\b ");
}

function rotate_waiter_while(var term, var cond)
{
cycle_while(term, "\\|/-", cond);
}
}
28 changes: 28 additions & 0 deletions src/unstable/terminal/Includes.winxed
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const int CON_ATTR_NORMAL = 0;
const int CON_ATTR_BOLD = 1;
const int CON_ATTR_DIM = 2;
const int CON_ATTR_UNDERLINE = 3;
const int CON_ATTR_BLINK = 5;
const int CON_ATTR_REVERSE = 7;
const int CON_ATTR_HIDDEN = 8;

const int CON_FG_BLACK = 30;
const int CON_FG_RED = 31;
const int CON_FG_GREEN = 32;
const int CON_FG_YELLOW = 33;
const int CON_FG_BLUE = 34;
const int CON_FG_MAGENTA = 35;
const int CON_FG_CYAN = 36;
const int CON_FG_WHITE = 37;

const int CON_BG_BLACK = 40;
const int CON_BG_RED = 41;
const int CON_BG_GREEN = 42;
const int CON_BG_YELLOW = 43;
const int CON_BG_BLUE = 44;
const int CON_BG_MAGENTA = 45;
const int CON_BG_CYAN = 46;
const int CON_BG_WHITE = 47;

// For more info, see:
// http://ascii-table.com/ansi-escape-sequences-vt-100.php
91 changes: 91 additions & 0 deletions src/unstable/terminal/Terminal.winxed
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
namespace Rosella.Terminal
{
function wrap_handle(var handle)
{
// TODO: We probably need a more robust way to test for terminal-ability
if (handle == null || handle.is_tty())
return new Rosella.Terminal(handle);
return new Rosella.Terminal.NonTty(handle);
}
}

class Rosella.Terminal
{
// TODO: Can we find a way to get the terminal height/width?

var handle;

function Terminal(var handle = getstdout())
{
self.handle = handle;
}

function set_color(int attr, int fg, int bg)
{
self.handle.print(sprintf("\033[%d;%d;%dm", [attr, fg, bg]));
}

function reset_color()
{
self.handle.print("\033[m");
}

function print(string s, var args [slurpy])
{
if (elements(args) > 0)
s = sprintf(s, args);
self.handle.print(s);
}

function position(int row, int col)
{
self.handle.print(sprintf("\033[%d;%dH", [row, col]));
}

function clear()
{
self.handle.print("\033[2J");
}

function reset()
{
self.handle.print("\033c");
}

function flush()
{
self.handle.flush();
}
}

class Rosella.Terminal.NonTty : Rosella.Terminal
{
function NonTty(var handle)
{
self.handle = handle;
}

function set_color(int attr, int fg, int bg) { }

function reset_color() { }

function print(string s, var args [slurpy])
{
if (elements(args) > 0)
s = sprintf(s, args);
self.handle.print(s);
}

function position(int row, int col)
{
Rosella.Error.error("Cannot set position on non-tty terminal");
}

function clear()
{
Rosella.Error.error("Cannot clear a non-tty terminal");
}

function reset() { }
}

0 comments on commit ad46d6f

Please sign in to comment.