diff options
| author | TitanSnow <[email protected]> | 2017-04-30 12:28:47 +0800 |
|---|---|---|
| committer | TitanSnow <[email protected]> | 2017-04-30 12:29:43 +0800 |
| commit | 73eea1fccb4e11f3678eaddaad22cae5ab79106c (patch) | |
| tree | 56c06f69df8dfb331c941cf2ec975fa71b616ae9 /core/src | |
| parent | 90d69cadfa5f5846e311876bfbd4a298c666fc8e (diff) | |
add os.getwinsize()
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/machine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 1 | ||||
| -rw-r--r-- | core/src/xmake/os/getwinsize.c | 72 |
3 files changed, 75 insertions, 0 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 2db633946..d1e86ab68 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -79,6 +79,7 @@ tb_int_t xm_os_setenv(lua_State* lua); tb_int_t xm_os_getenv(lua_State* lua); tb_int_t xm_os_emptydir(lua_State* lua); tb_int_t xm_os_strerror(lua_State* lua); +tb_int_t xm_os_getwinsize(lua_State* lua); // the path functions tb_int_t xm_path_relative(lua_State* lua); @@ -126,6 +127,7 @@ static luaL_Reg const g_os_functions[] = , { "getenv", xm_os_getenv } , { "emptydir", xm_os_emptydir } , { "strerror", xm_os_strerror } +, { "getwinsize", xm_os_getwinsize} , { tb_null, tb_null } }; diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 0d0399bb1..4b7c5192e 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -35,6 +35,7 @@ xmake_C_FILES += \ os/getenv \ os/emptydir \ os/strerror \ + os/getwinsize \ path/relative \ path/absolute \ path/translate \ diff --git a/core/src/xmake/os/getwinsize.c b/core/src/xmake/os/getwinsize.c new file mode 100644 index 000000000..c541f1e0e --- /dev/null +++ b/core/src/xmake/os/getwinsize.c @@ -0,0 +1,72 @@ +/*!The Make-like Build Utility based on Lua + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015 - 2017, TBOOX Open Source Group. + * + * @author TitanSnow + * @file getwinsize.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "getwinsize" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef TB_CONFIG_OS_LINUX +#include <sys/ioctl.h> +#include <termios.h> +#include <unistd.h> +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_os_getwinsize(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // def w&h + unsigned short w=80, h=40; + + // get winsize +# ifdef TB_CONFIG_OS_LINUX + struct winsize size; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &size); + w=size.ws_col; + h=size.ws_row; +# endif + + // done os.getwinsize() + lua_newtable(lua); + lua_pushstring(lua, "width"); + lua_pushinteger(lua, w); + lua_settable(lua, -3); + lua_pushstring(lua, "height"); + lua_pushinteger(lua, h); + lua_settable(lua, -3); + + // ok + return 1; +} |
