Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ab5tract/ff7ea75f0a9262d4e4a4 to your computer and use it in GitHub Desktop.
Save ab5tract/ff7ea75f0a9262d4e4a4 to your computer and use it in GitHub Desktop.
### i'm getting used to breaking moar :(
➜ lib perl6 Boxbrain.pm
clear
smcup
rmcup
sc
rc
moar(62161,0x7fff79f96300) malloc: *** error for object 0x7f8753d1b530: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
/Users/jhaltiwanger/.rakudobrew/bin/perl6: line 2: 62161 Abort trap: 6 /Users/jhaltiwanger/.rakudobrew/moar-HEAD/install/bin/perl6 "$@"
### undefined key name behavior ("sc" becomes "\x[0]\x[0]")
➜ lib perl6 Boxbrain.pm
clear
smcup
rmcup
sc
rc
("smcup" => "\x[1b][?1049h", "rmcup" => "\x[1b][?1049l", "\x[0]\x[0]" => "\x[1b]7", "rc" => "\x[1b]8", "clear" => "\x[1b][H\x[1b][2J").hash
Not a supported (or possibly valid) tput command
in sub tput at Boxbrain.pm:32
in block <unit> at Boxbrain.pm:44
### runs without breaking
➜ lib perl6 Boxbrain.pm
clear
smcup
rmcup
sc
rc
("rc" => "\x[1b]8", "sc" => "\x[1b]7", "rmcup" => "\x[1b][?1049l", "smcup" => "\x[1b][?1049h", "clear" => "\x[1b][H\x[1b][2J").hash
"\x[1b]7""\x[1b]8"
module Terminal::Control {
our %controls;
our %human-commands;
BEGIN {
%human-commands = %(
'clear' => 'clear',
'save-screen' => 'smcup',
'restore-screen' => 'rmcup',
'save-cursor' => 'sc',
'restore-cursor' => 'rc',
);
for %human-commands.values -> $command {
say $command;
%controls<<$command>> = qq:x{ tput $command };
}
%controls.perl.say;
}
our sub cursor_to( Int $x, Int $y ) {
"\e[{$x};{$y}H"; # we are using the hardcoded ANSI because it's the
# least inelegant solution
}
our sub tput( Str $command ) {
die "Not a supported (or possibly valid) tput command"
unless %controls<<$command>>;
%controls<<$command>>;
}
}
print Terminal::Control::tput('sc').perl;
constant T = ::Terminal::Control;
say T::tput('rc').perl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment