summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThijs <thijs@thijsschreijer.nl>2023-11-12 23:53:26 +0100
committerThijs Schreijer <thijs@thijsschreijer.nl>2023-11-15 19:17:57 +0100
commit5f3951a942fdc4bf489d8d590bfc891ac9548a23 (patch)
tree8d0b7d2e10b45f022d8562cd974a3b7537d1dd7c
parentd45768de3e6f7b28bfecf4d19b192ccac9ce5dc2 (diff)
downloadluasystem-5f3951a942fdc4bf489d8d590bfc891ac9548a23.zip
feat(tty): add isatty()
-rw-r--r--luasystem-scm-0.rockspec9
-rw-r--r--spec/01-time_spec.lua5
-rw-r--r--src/Makefile2
-rw-r--r--src/core.c2
-rw-r--r--src/term.c37
5 files changed, 50 insertions, 5 deletions
diff --git a/luasystem-scm-0.rockspec b/luasystem-scm-0.rockspec
index 96f10ae..86209a6 100644
--- a/luasystem-scm-0.rockspec
+++ b/luasystem-scm-0.rockspec
@@ -45,7 +45,14 @@ local function make_platform(plat)
return {
modules = {
['system.core'] = {
- sources = { 'src/core.c', 'src/compat.c', 'src/time.c', 'src/environment.c', 'src/random.c' },
+ sources = {
+ 'src/core.c',
+ 'src/compat.c',
+ 'src/time.c',
+ 'src/environment.c',
+ 'src/random.c',
+ 'src/term.c',
+ },
defines = defines[plat],
libraries = libraries[plat],
},
diff --git a/spec/01-time_spec.lua b/spec/01-time_spec.lua
index 80faa75..1607cca 100644
--- a/spec/01-time_spec.lua
+++ b/spec/01-time_spec.lua
@@ -16,7 +16,6 @@ describe('Test time functions', function()
describe("time()", function()
it('returns current time', function()
- wait_for_second_rollover()
local expected_time = wait_for_second_rollover()
local received_time = system.gettime()
assert.is.near(expected_time, received_time, 0.02)
@@ -51,7 +50,7 @@ describe('Test time functions', function()
system.sleep(1, 1)
local end_time = system.gettime()
local elapsed_time = end_time - start_time
- assert.is.near(elapsed_time, 1, 0.01)
+ assert.is.near(elapsed_time, 1, 0.2) -- large marging of error due to CI priorities
end)
@@ -60,7 +59,7 @@ describe('Test time functions', function()
system.sleep(0.5, 1)
local end_time = system.gettime()
local elapsed_time = end_time - start_time
- assert.is.near(0.5, elapsed_time, 0.01)
+ assert.is.near(0.5, elapsed_time, 0.2) -- large marging of error due to CI priorities
end)
diff --git a/src/Makefile b/src/Makefile
index 119f95e..b4ed16f 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -217,7 +217,7 @@ LUALIB= $(LUALIB_$(PLAT))
#------
# Objects
#
-OBJS=core.$(O) compat.$(O) time.$(O) environment.$(O) random.$(O)
+OBJS=core.$(O) compat.$(O) time.$(O) environment.$(O) random.$(O) term.$(O)
#------
# Targets
diff --git a/src/core.c b/src/core.c
index fffedd1..87fd105 100644
--- a/src/core.c
+++ b/src/core.c
@@ -15,6 +15,7 @@
void time_open(lua_State *L);
void environment_open(lua_State *L);
void random_open(lua_State *L);
+void term_open(lua_State *L);
/*-------------------------------------------------------------------------
* Initializes all library modules.
@@ -33,6 +34,7 @@ LUAEXPORT int luaopen_system_core(lua_State *L) {
lua_rawset(L, -3);
time_open(L);
random_open(L);
+ term_open(L);
environment_open(L);
return 1;
}
diff --git a/src/term.c b/src/term.c
new file mode 100644
index 0000000..2adb1e9
--- /dev/null
+++ b/src/term.c
@@ -0,0 +1,37 @@
+/// @submodule system
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+#include "compat.h"
+
+#ifndef _MSC_VER
+# include <unistd.h>
+#endif
+
+
+/***
+Checks if a file-handle is a TTY.
+
+@function isatty
+@tparam file file the file-handle to check
+@treturn boolean true if the file is a tty
+*/
+static int lua_isatty(lua_State* L) {
+ FILE **fh = (FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
+ lua_pushboolean(L, isatty(fileno(*fh)));
+ return 1;
+}
+
+
+
+static luaL_Reg func[] = {
+ { "isatty", lua_isatty },
+ { NULL, NULL }
+};
+
+/*-------------------------------------------------------------------------
+ * Initializes module
+ *-------------------------------------------------------------------------*/
+void term_open(lua_State *L) {
+ luaL_setfuncs(L, func, 0);
+}