summaryrefslogtreecommitdiff
path: root/mpv/scripts/cycle-profiles.lua
blob: da846c55ae086593b99462c1f2defe4a2b77da2b (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
--[[
    script to cycle profiles with a keybind, accomplished through script messages
    available at: https://github.com/CogentRedTester/mpv-scripts
    syntax:
        script-message cycle-profiles "profile1;profile2;profile3"
    You must use semicolons to separate the profiles, do not include any spaces that are not part of the profile name.
    The script will print the profile description to the screen when switching, if there is no profile description, then it just prints the name
]]--

--change this to change what character separates the profile names
seperator = ";"

msg = require 'mp.msg'

--splits the profiles string into an array of profile names
--function taken from: https://stackoverflow.com/questions/1426954/split-string-in-lua/7615129#7615129
function mysplit (inputstr, sep)
    if sep == nil then
            sep = "%s"
    end
    local t={}
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
            table.insert(t, str)
    end
    return t
end

--table of all available profiles and options
profileList = mp.get_property_native('profile-list')

--keeps track of current profile for every unique cycle
iterator = {}

--stores descriptions for profiles
--once requested a description is stored here so it does not need to be found again
profilesDescs = {}

--if trying to cycle to an unknown profile this function is run to find a description to print
function findDesc(profile)
    msg.verbose('unknown profile ' .. profile .. ', searching for description')

    for i = 1, #profileList, 1 do
        if profileList[i]['name'] == profile then
            msg.verbose('profile found')
            local desc = profileList[i]['profile-desc']

            if desc ~= nil then
                msg.verbose('description found')
                profilesDescs[profile] = desc
            else
                msg.verbose('no description, will use name')
                profilesDescs[profile] = profile
            end
            return
        end
    end

    msg.verbose('profile not found')
    profilesDescs[profile] = "no profile '" .. profile .. "'"
end

--prints the profile description to the OSD
--if the profile has not been requested before during the session then it runs findDesc()
function printProfileDesc(profile)
    local desc = profilesDescs[profile]
    if desc == nil then
        findDesc(profile)
        desc = profilesDescs[profile]
    end

    msg.verbose('profile description: ' .. desc)
    mp.osd_message(desc)
end

function main(profileStr)
    --if there is not already an iterator for this cycle then it creates one
    if iterator[profileStr] == nil then
        msg.verbose('unknown cycle, creating new iterator')
        iterator[profileStr] = 1
    end
    local i = iterator[profileStr]

    --converts the string into an array of profile names
    local profiles = mysplit(profileStr, seperator)
    msg.verbose('cycling ' .. tostring(profiles))
    msg.verbose("number of profiles: " .. tostring(#profiles))

    --sends the command to apply the profile
    msg.info("applying profile " .. profiles[i])
    mp.commandv('apply-profile', profiles[i])

    --prints the profile description to the OSD
    printProfileDesc(profiles[i])

    --moves the iterator
    iterator[profileStr] = iterator[profileStr] + 1
    if iterator[profileStr] > #profiles then
        msg.verbose('reached end of profiles, wrapping back to start')
        iterator[profileStr] = 1
    end
end

mp.register_script_message('cycle-profiles', main)