aboutsummaryrefslogtreecommitdiff
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.tar.gz
luasystem-5f3951a942fdc4bf489d8d590bfc891ac9548a23.tar.bz2
luasystem-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)
45 return { 45 return {
46 modules = { 46 modules = {
47 ['system.core'] = { 47 ['system.core'] = {
48 sources = { 'src/core.c', 'src/compat.c', 'src/time.c', 'src/environment.c', 'src/random.c' }, 48 sources = {
49 'src/core.c',
50 'src/compat.c',
51 'src/time.c',
52 'src/environment.c',
53 'src/random.c',
54 'src/term.c',
55 },
49 defines = defines[plat], 56 defines = defines[plat],
50 libraries = libraries[plat], 57 libraries = libraries[plat],
51 }, 58 },
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()
16 describe("time()", function() 16 describe("time()", function()
17 17
18 it('returns current time', function() 18 it('returns current time', function()
19 wait_for_second_rollover()
20 local expected_time = wait_for_second_rollover() 19 local expected_time = wait_for_second_rollover()
21 local received_time = system.gettime() 20 local received_time = system.gettime()
22 assert.is.near(expected_time, received_time, 0.02) 21 assert.is.near(expected_time, received_time, 0.02)
@@ -51,7 +50,7 @@ describe('Test time functions', function()
51 system.sleep(1, 1) 50 system.sleep(1, 1)
52 local end_time = system.gettime() 51 local end_time = system.gettime()
53 local elapsed_time = end_time - start_time 52 local elapsed_time = end_time - start_time
54 assert.is.near(elapsed_time, 1, 0.01) 53 assert.is.near(elapsed_time, 1, 0.2) -- large marging of error due to CI priorities
55 end) 54 end)
56 55
57 56
@@ -60,7 +59,7 @@ describe('Test time functions', function()
60 system.sleep(0.5, 1) 59 system.sleep(0.5, 1)
61 local end_time = system.gettime() 60 local end_time = system.gettime()
62 local elapsed_time = end_time - start_time 61 local elapsed_time = end_time - start_time
63 assert.is.near(0.5, elapsed_time, 0.01) 62 assert.is.near(0.5, elapsed_time, 0.2) -- large marging of error due to CI priorities
64 end) 63 end)
65 64
66 65
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))
217#------ 217#------
218# Objects 218# Objects
219# 219#
220OBJS=core.$(O) compat.$(O) time.$(O) environment.$(O) random.$(O) 220OBJS=core.$(O) compat.$(O) time.$(O) environment.$(O) random.$(O) term.$(O)
221 221
222#------ 222#------
223# Targets 223# 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 @@
15void time_open(lua_State *L); 15void time_open(lua_State *L);
16void environment_open(lua_State *L); 16void environment_open(lua_State *L);
17void random_open(lua_State *L); 17void random_open(lua_State *L);
18void term_open(lua_State *L);
18 19
19/*------------------------------------------------------------------------- 20/*-------------------------------------------------------------------------
20 * Initializes all library modules. 21 * Initializes all library modules.
@@ -33,6 +34,7 @@ LUAEXPORT int luaopen_system_core(lua_State *L) {
33 lua_rawset(L, -3); 34 lua_rawset(L, -3);
34 time_open(L); 35 time_open(L);
35 random_open(L); 36 random_open(L);
37 term_open(L);
36 environment_open(L); 38 environment_open(L);
37 return 1; 39 return 1;
38} 40}
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 @@
1/// @submodule system
2#include <lua.h>
3#include <lauxlib.h>
4#include <lualib.h>
5#include "compat.h"
6
7#ifndef _MSC_VER
8# include <unistd.h>
9#endif
10
11
12/***
13Checks if a file-handle is a TTY.
14
15@function isatty
16@tparam file file the file-handle to check
17@treturn boolean true if the file is a tty
18*/
19static int lua_isatty(lua_State* L) {
20 FILE **fh = (FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
21 lua_pushboolean(L, isatty(fileno(*fh)));
22 return 1;
23}
24
25
26
27static luaL_Reg func[] = {
28 { "isatty", lua_isatty },
29 { NULL, NULL }
30};
31
32/*-------------------------------------------------------------------------
33 * Initializes module
34 *-------------------------------------------------------------------------*/
35void term_open(lua_State *L) {
36 luaL_setfuncs(L, func, 0);
37}