Skip to content

Commit

Permalink
feat(tty): add isatty()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Nov 13, 2023
1 parent 30679fc commit c345239
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
9 changes: 8 additions & 1 deletion luasystem-scm-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -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],
},
Expand Down
5 changes: 2 additions & 3 deletions spec/01-time_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)


Expand All @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
37 changes: 37 additions & 0 deletions src/term.c
Original file line number Diff line number Diff line change
@@ -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 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);
}

0 comments on commit c345239

Please sign in to comment.