|
| 1 | +--[[ |
| 2 | + An addon for mpv-file-browser which stores the last opened directory and |
| 3 | + sets it as the opened directory the next time mpv is opened. |
| 4 | +
|
| 5 | + Available at: https://github.com/CogentRedTester/mpv-file-browser/tree/master/addons |
| 6 | +]]-- |
| 7 | + |
| 8 | +local mp = require 'mp' |
| 9 | +local msg = require 'mp.msg' |
| 10 | + |
| 11 | +local fb = require 'file-browser' |
| 12 | + |
| 13 | +local state_file = mp.command_native({'expand-path', '~~state/last_opened_directory'}) |
| 14 | +msg.verbose('using', state_file) |
| 15 | + |
| 16 | +local function write_directory(directory) |
| 17 | + local file = io.open(state_file, 'w+') |
| 18 | + |
| 19 | + if not file then return msg.error('could not open', state_file, 'for writing') end |
| 20 | + |
| 21 | + directory = directory or fb.get_directory() |
| 22 | + msg.verbose('writing', directory, 'to', state_file) |
| 23 | + file:write(directory) |
| 24 | + file:close() |
| 25 | +end |
| 26 | + |
| 27 | + |
| 28 | +local addon = { |
| 29 | + version = '1.7.0', |
| 30 | + priority = 0, |
| 31 | +} |
| 32 | + |
| 33 | +function addon:setup() |
| 34 | + local file = io.open(state_file, "r") |
| 35 | + if not file then |
| 36 | + return msg.error('failed to open', state_file, 'for reading') |
| 37 | + end |
| 38 | + |
| 39 | + local dir = file:read("*a") |
| 40 | + msg.verbose('setting default directory to', dir) |
| 41 | + fb.browse_directory(dir, false) |
| 42 | + file:close() |
| 43 | +end |
| 44 | + |
| 45 | +function addon:can_parse(dir, parse_state) |
| 46 | + if parse_state.source == 'browser' then write_directory(dir) end |
| 47 | + return false |
| 48 | +end |
| 49 | + |
| 50 | +function addon:parse() |
| 51 | + return nil |
| 52 | +end |
| 53 | + |
| 54 | +mp.register_event('shutdown', function() write_directory() end) |
| 55 | + |
| 56 | +return addon |
0 commit comments