File: input.lua

package info (click to toggle)
mpv 0.40.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,676 kB
  • sloc: ansic: 152,062; python: 1,228; sh: 646; javascript: 612; cpp: 461; objc: 302; pascal: 49; xml: 29; makefile: 19
file content (84 lines) | stat: -rw-r--r-- 2,518 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
--[[
This file is part of mpv.

mpv is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

mpv is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
]]

local utils = require "mp.utils"
local input = {}

local function get_non_callbacks(t)
    local non_callbacks = {}

    for key, value in pairs(t) do
        if type(value) ~= "function" then
            non_callbacks[key] = value
        end
    end

    return non_callbacks
end

local function register_event_handler(t)
    mp.register_script_message("input-event", function (type, args)
        if t[type] then
            local completions, completion_pos, completion_append =
                t[type](unpack(utils.parse_json(args or "") or {}))

            if type == "complete" and completions then
                mp.commandv("script-message-to", "console", "complete",
                            utils.format_json(completions), completion_pos,
                            completion_append or "")
            end
        end

        if type == "closed" then
            mp.unregister_script_message("input-event")
        end
    end)
end

function input.get(t)
    t.has_completions = t.complete ~= nil

    mp.commandv("script-message-to", "console", "get-input",
                mp.get_script_name(), utils.format_json(get_non_callbacks(t)))

    register_event_handler(t)
end

input.select = input.get

function input.terminate()
    mp.commandv("script-message-to", "console", "disable")
end

function input.log(message, style, terminal_style)
    mp.commandv("script-message-to", "console", "log", utils.format_json({
                   text = message,
                   style = style,
                   terminal_style = terminal_style,
               }))
end

function input.log_error(message)
    mp.commandv("script-message-to", "console", "log",
                 utils.format_json({ text = message, error = true }))
end

function input.set_log(log)
    mp.commandv("script-message-to", "console", "set-log", utils.format_json(log))
end

return input