mirror of
https://github.com/mpv-player/mpv.git
synced 2026-05-07 20:02:49 +00:00
player/{lua,javascript}: add mp.utils.terminal_display_width function
This commit is contained in:
committed by
Kacper Michajłow
parent
6ab49ac5d7
commit
c202be1efe
@@ -0,0 +1,2 @@
|
||||
add `mp.utils.terminal_display_width()` function that measures the width of
|
||||
a string in terminal cells
|
||||
@@ -35,6 +35,7 @@
|
||||
#include "input/input.h"
|
||||
#include "options/path.h"
|
||||
#include "misc/bstr.h"
|
||||
#include "misc/codepoint_width.h"
|
||||
#include "osdep/timer.h"
|
||||
#include "osdep/threads.h"
|
||||
#include "stream/stream.h"
|
||||
@@ -1000,6 +1001,15 @@ static void script_get_env_list(js_State *J)
|
||||
}
|
||||
}
|
||||
|
||||
// args: text
|
||||
static void script_terminal_display_width(js_State *J)
|
||||
{
|
||||
bstr text = bstr0(js_tostring(J, 1));
|
||||
const unsigned char *cut_pos;
|
||||
int width = term_disp_width(text, INT_MAX, &cut_pos);
|
||||
js_pushnumber(J, width);
|
||||
}
|
||||
|
||||
// args: as-filename, content-string, returns the compiled result as a function
|
||||
static void script_compile_js(js_State *J)
|
||||
{
|
||||
@@ -1202,6 +1212,7 @@ static const struct fn_entry utils_fns[] = {
|
||||
FN_ENTRY(split_path, 1),
|
||||
AF_ENTRY(join_path, 2),
|
||||
FN_ENTRY(get_env_list, 0),
|
||||
FN_ENTRY(terminal_display_width, 1),
|
||||
|
||||
FN_ENTRY(read_file, 2),
|
||||
AF_ENTRY(_write_file, 3),
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
#include "input/input.h"
|
||||
#include "options/path.h"
|
||||
#include "misc/bstr.h"
|
||||
#include "misc/codepoint_width.h"
|
||||
#include "misc/json.h"
|
||||
#include "osdep/subprocess.h"
|
||||
#include "osdep/timer.h"
|
||||
@@ -1216,6 +1217,15 @@ static int script_get_env_list(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int script_terminal_display_width(lua_State *L)
|
||||
{
|
||||
bstr text = bstr0(luaL_checkstring(L, 1));
|
||||
const unsigned char *cut_pos;
|
||||
int width = term_disp_width(text, INT_MAX, &cut_pos);
|
||||
lua_pushnumber(L, width);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define FN_ENTRY(name) {#name, script_ ## name, 0}
|
||||
#define AF_ENTRY(name) {#name, 0, script_ ## name}
|
||||
struct fn_entry {
|
||||
@@ -1265,6 +1275,7 @@ static const struct fn_entry utils_fns[] = {
|
||||
AF_ENTRY(parse_json),
|
||||
AF_ENTRY(format_json),
|
||||
FN_ENTRY(get_env_list),
|
||||
FN_ENTRY(terminal_display_width),
|
||||
{0}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user