Permalink
Browse files

[Terminal] Throw together a prototype library for working with the te…

…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...
1 parent 91d12c8 commit ad46d6ff62e84f64fee008ecb1a01fd6a177413f @Whiteknight committed Sep 5, 2012
View
6 setup.winxed
@@ -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)
View
20 src/unstable/terminal/Decorations.winxed
@@ -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);
+ }
+}
View
28 src/unstable/terminal/Includes.winxed
@@ -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
View
91 src/unstable/terminal/Terminal.winxed
@@ -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.