-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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 ad46d6f
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { } | ||
} | ||
|