summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-04-30 20:30:38 +0800
committerruki <[email protected]>2017-04-30 20:30:38 +0800
commit2d077a73b654541e76107c5bb01c402572e484c1 (patch)
tree16dbbd7665ce6922d9a72ad3752960529b678030
parent0ecc2b7e4b94ab333b0c1c860de322668009f844 (diff)
enable getwinsize for macosx
-rw-r--r--core/src/xmake/os/getwinsize.c44
-rw-r--r--xmake/core/base/option.lua99
-rw-r--r--xmake/core/project/task.lua6
3 files changed, 85 insertions, 64 deletions
diff --git a/core/src/xmake/os/getwinsize.c b/core/src/xmake/os/getwinsize.c
index 3f721c12f..041150070 100644
--- a/core/src/xmake/os/getwinsize.c
+++ b/core/src/xmake/os/getwinsize.c
@@ -33,37 +33,45 @@
* includes
*/
#include "prefix.h"
-#ifdef TB_CONFIG_OS_LINUX
-#include <sys/ioctl.h>
-#include <errno.h> // for errno
-#include <unistd.h> // for STDOUT_FILENO
+#ifndef TB_CONFIG_OS_WINDOWS
+# include <sys/ioctl.h>
+# include <errno.h> // for errno
+# include <unistd.h> // for STDOUT_FILENO
#endif
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
+
+// get console window size
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;
+ // init default window size
+ unsigned short w = -1, h = -1;
// get winsize
-# ifdef TB_CONFIG_OS_LINUX
- struct winsize size;
- if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &size)==0){
- w=size.ws_col;
- h=size.ws_row;
- }else if(errno == ENOTTY)
- w=h=-1; // set to INF if stdout is not a tty
- //
- // if stdout is a file there is no
- // need to consider winsize limit
-# endif
+#ifndef TB_CONFIG_OS_WINDOWS
+ struct winsize size = {0};
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0)
+ {
+ w = size.ws_col;
+ h = size.ws_row;
+ }
+#else
+ // we will not consider winsize limit if cannot get it
+#endif
- // done os.getwinsize()
+ /* local winsize = os.getwinsize()
+ *
+ * return
+ * {
+ * width = -1 or ..
+ * , height = -1 or ..
+ * }
+ */
lua_newtable(lua);
lua_pushstring(lua, "width");
lua_pushinteger(lua, w);
diff --git a/xmake/core/base/option.lua b/xmake/core/base/option.lua
index 352e64cc0..a2388c91e 100644
--- a/xmake/core/base/option.lua
+++ b/xmake/core/base/option.lua
@@ -131,6 +131,51 @@ function option._bindings(name)
end
end
+-- get line length
+function option._get_linelen(st)
+ local poss = st:reverse():find("\n")
+ if not poss then return (#st) end
+ local start_pos, _ = poss
+ return start_pos - 1
+end
+
+-- get last space
+function option._get_lastspace(st)
+ local poss = st:reverse():find("[%s-]")
+ if not poss then return (#st) end
+ local start_pos, _ = poss
+ return (#st) - start_pos + 1
+end
+
+-- append spaces in width
+function option._inwidth_append(dst, st, padding, width, remain_width)
+
+ if padding >= width then
+ return dst .. st
+ end
+
+ local white_padding = string.rep(" ", padding)
+ if remain_width == nil then
+ -- TODO because of colored string, it's wrong sometimes
+ remain_width = width - option._get_linelen(dst)
+ end
+
+ if remain_width <= 0 then
+ return option._inwidth_append(dst .. "\n" .. white_padding, st, padding, width, width - padding)
+ end
+
+ if (#st) <= remain_width then
+ return dst .. st
+ end
+
+ local lastspace = option._get_lastspace(st:sub(1, remain_width))
+ if lastspace + 1 > (#st) then
+ return dst .. st
+ else
+ return option._inwidth_append(dst .. st:sub(1, lastspace) .. "\n" .. white_padding, st:sub(lastspace + 1):ltrim(), padding, width, width - padding)
+ end
+end
+
-- save context
function option.save(taskname)
@@ -874,38 +919,6 @@ function option.show_menu(task)
end
end
-function get_linelen(st)
- local poss = string.find(string.reverse(st), "\n")
- if not poss then return (#st) end
- local start_pos, _ = poss
- return start_pos - 1
-end
-function inwidth_append(dst, st, padding, width, remain_width)
- function get_lastspace(st)
- local poss = string.find(string.reverse(st), "[%s-]")
- if not poss then return (#st) end
- local start_pos, _ = poss
- return (#st) - start_pos + 1
- end
- if padding >= width then
- return dst .. st
- end
- local white_padding = string.rep(" ", padding)
- if remain_width == nil then remain_width = width - get_linelen(dst) end -- because of colored string, it's wrong sometimes
- if remain_width <= 0 then
- return inwidth_append(dst .. "\n" .. white_padding, st, padding, width, width - padding)
- end
- if (#st) <= remain_width then
- return dst .. st
- end
- local lastspace = get_lastspace(string.sub(st, 1, remain_width))
- if lastspace + 1 > (#st) then
- return dst .. st
- else
- return inwidth_append(dst .. string.sub(st, 1, lastspace) .. "\n" .. white_padding, string.ltrim(string.sub(st, lastspace + 1)), padding, width, width - padding)
- end
-end
-
-- show the main menu
function option.show_main()
@@ -1012,7 +1025,7 @@ function option.show_main()
-- append the task description
if taskinfo.description then
- taskline = inwidth_append(taskline, taskinfo.description, padding + 1 - 18, console_width, console_width - padding - 1 + 18)
+ taskline = option._inwidth_append(taskline, taskinfo.description, padding + 1 - 18, console_width, console_width - padding - 1 + 18)
end
-- print task line
@@ -1087,19 +1100,19 @@ function option.show_options(options)
-- append the option description
local description = opt[5]
if description then
- option_info = inwidth_append(option_info, description, padding + 1, console_width, console_width - padding - 1)
+ option_info = option._inwidth_append(option_info, description, padding + 1, console_width, console_width - padding - 1)
end
-- append the default value
local default = opt[4]
if default then
- option_info = inwidth_append(option_info, " (default: ", padding + 1, console_width)
- local origin_width = get_linelen(option_info)
- option_info = option_info .. "${bright}"
- option_info = inwidth_append(option_info, tostring(default), padding + 1, console_width, console_width - origin_width)
- origin_width = option._ifelse(origin_width + (#(tostring(default))) > console_width, get_linelen(option_info), origin_width + (#(tostring(default))))
- option_info = option_info .. "${clear}"
- option_info = inwidth_append(option_info, ")", padding + 1, console_width, console_width - origin_width)
+ option_info = option._inwidth_append(option_info, " (default: ", padding + 1, console_width)
+ local origin_width = option._get_linelen(option_info)
+ option_info = option_info .. "${bright}"
+ option_info = option._inwidth_append(option_info, tostring(default), padding + 1, console_width, console_width - origin_width)
+ origin_width = option._ifelse(origin_width + (#(tostring(default))) > console_width, option._get_linelen(option_info), origin_width + (#(tostring(default))))
+ option_info = option_info .. "${clear}"
+ option_info = option._inwidth_append(option_info, ")", padding + 1, console_width, console_width - origin_width)
end
-- print option info
@@ -1127,7 +1140,7 @@ function option.show_options(options)
end
-- print this description
- print(inwidth_append(spaces, description, padding + 1, console_width))
+ print(option._inwidth_append(spaces, description, padding + 1, console_width))
-- the description is table?
elseif type(description) == "table" then
@@ -1142,7 +1155,7 @@ function option.show_options(options)
end
-- print this description
- print(inwidth_append(spaces, v, padding + 1, console_width))
+ print(option._inwidth_append(spaces, v, padding + 1, console_width))
end
end
end
diff --git a/xmake/core/project/task.lua b/xmake/core/project/task.lua
index 904ca81d6..35c051ef9 100644
--- a/xmake/core/project/task.lua
+++ b/xmake/core/project/task.lua
@@ -142,9 +142,9 @@ function task._translate_menu(menu)
table.insert(options, 8, {'F', "file", "kv", nil, "Read a given xmake.lua file." })
table.insert(options, 9, {'P', "project", "kv", nil, "Change to the given project directory."
, "Search priority:"
- , " 1. The Given Command Argument"
- , " 2. The Envirnoment Variable: XMAKE_PROJECT_DIR"
- , " 3. The Current Directory" })
+ , " 1. The Given Command Argument"
+ , " 2. The Envirnoment Variable: XMAKE_PROJECT_DIR"
+ , " 3. The Current Directory" })
table.insert(options, 10, {})
end