✨ add native terminal_size
This commit is contained in:
parent
d37dfec225
commit
8d3f752429
|
@ -46,7 +46,7 @@ private:
|
||||||
// under limited mode, unsafe system api will be banned
|
// under limited mode, unsafe system api will be banned
|
||||||
const std::unordered_set<std::string> unsafe_system_api = {
|
const std::unordered_set<std::string> unsafe_system_api = {
|
||||||
// builtin
|
// builtin
|
||||||
"__system", "__input",
|
"__system", "__input", "__terminal_size",
|
||||||
// io
|
// io
|
||||||
"__fout", "__open", "__write", "__stat"
|
"__fout", "__open", "__write", "__stat"
|
||||||
// bits
|
// bits
|
||||||
|
|
|
@ -5,6 +5,9 @@
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#else
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace nasal {
|
namespace nasal {
|
||||||
|
@ -786,6 +789,25 @@ var builtin_set_utf8_output(context* ctx, gc* ngc) {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var builtin_terminal_size(context* ctx, gc* ngc) {
|
||||||
|
var res = ngc->alloc(vm_type::vm_hash);
|
||||||
|
#ifdef _WIN32
|
||||||
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||||
|
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
|
||||||
|
auto rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
|
||||||
|
auto cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
|
||||||
|
res.hash().elems["rows"] = var::num(rows);
|
||||||
|
res.hash().elems["cols"] = var::num(cols);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
struct winsize w;
|
||||||
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||||
|
res.hash().elems["rows"] = var::num(w.ws_row);
|
||||||
|
res.hash().elems["cols"] = var::num(w.ws_col);
|
||||||
|
#endif
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
nasal_builtin_table builtin[] = {
|
nasal_builtin_table builtin[] = {
|
||||||
{"__print", builtin_print},
|
{"__print", builtin_print},
|
||||||
{"__println", builtin_println},
|
{"__println", builtin_println},
|
||||||
|
@ -835,6 +857,7 @@ nasal_builtin_table builtin[] = {
|
||||||
{"__logtime", builtin_logtime},
|
{"__logtime", builtin_logtime},
|
||||||
{"__ghosttype", builtin_ghosttype},
|
{"__ghosttype", builtin_ghosttype},
|
||||||
{"__set_utf8_output", builtin_set_utf8_output},
|
{"__set_utf8_output", builtin_set_utf8_output},
|
||||||
|
{"__terminal_size", builtin_terminal_size},
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -85,6 +85,7 @@ var builtin_ghosttype(context*, gc*);
|
||||||
|
|
||||||
// only useful on windows platform
|
// only useful on windows platform
|
||||||
var builtin_set_utf8_output(context*, gc*);
|
var builtin_set_utf8_output(context*, gc*);
|
||||||
|
var builtin_terminal_size(context*, gc*);
|
||||||
|
|
||||||
// register builtin function's name and it's address here in this table below
|
// register builtin function's name and it's address here in this table below
|
||||||
// this table must end with {nullptr, nullptr}
|
// this table must end with {nullptr, nullptr}
|
||||||
|
|
|
@ -5,4 +5,8 @@ use std.math;
|
||||||
# when count can be divided exactly by times, return true
|
# when count can be divided exactly by times, return true
|
||||||
var times_trigger = func(times, count) {
|
var times_trigger = func(times, count) {
|
||||||
return math.mod(times, count)==0;
|
return math.mod(times, count)==0;
|
||||||
|
}
|
||||||
|
|
||||||
|
var terminal_size = func {
|
||||||
|
return __terminal_size;
|
||||||
}
|
}
|
Loading…
Reference in New Issue