Skip to content

Commit c8f35b1

Browse files
Merge pull request #127 from CogentRedTester/API/v1.8.0
Update API to v1.8.0 and improve favourites.lua keybind handling
2 parents fde00db + cce9fec commit c8f35b1

File tree

5 files changed

+35
-21
lines changed

5 files changed

+35
-21
lines changed

addons/favourites.lua

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ local favourites_loaded = false
2222

2323
---@type ParserConfig
2424
local favs = {
25-
api_version = "1.4.0",
25+
api_version = "1.8.0",
2626
priority = 30,
2727
cursor = 1
2828
}
@@ -123,16 +123,14 @@ local function get_favourite(path)
123123
end
124124

125125
--update the browser with new contents of the file
126+
---@async
126127
local function update_browser()
127-
if fb.get_directory():find("[fF]avourites/") then
128-
if fb.get_directory():find("[fF]avourites/$") then
129-
local cursor = fb.get_selected_index()
130-
fb.rescan()
131-
fb.set_selected_index(cursor)
132-
fb.redraw()
133-
else
134-
fb.clear_cache()
135-
end
128+
if favs.get_directory():find("^[fF]avourites/$") then
129+
local cursor = favs.get_selected_index()
130+
fb.rescan_await()
131+
fb.set_selected_index(cursor)
132+
else
133+
fb.clear_cache({'favourites/', 'Favourites/'})
136134
end
137135
end
138136

@@ -171,6 +169,7 @@ local function move_favourite(path, direction)
171169
write_to_file()
172170
end
173171

172+
---@async
174173
local function toggle_favourite(cmd, state, co)
175174
local path = fb.get_full_path(state.list[state.selected], state.directory)
176175

@@ -179,6 +178,7 @@ local function toggle_favourite(cmd, state, co)
179178
update_browser()
180179
end
181180

181+
---@async
182182
local function move_key(cmd, state, co)
183183
if not state.directory:find("[fF]avourites/") then return false end
184184
local path = fb.get_full_path(state.list[state.selected], state.directory)
@@ -195,10 +195,6 @@ local function move_key(cmd, state, co)
195195
end
196196

197197
update_favourites()
198-
mp.register_script_message("favourites/add_favourite", add_favourite)
199-
mp.register_script_message("favourites/remove_favourite", remove_favourite)
200-
mp.register_script_message("favourites/move_up", function(path) move_favourite(path, -1) end)
201-
mp.register_script_message("favourites/move_down", function(path) move_favourite(path, 1) end)
202198

203199
favs.keybinds = {
204200
{ "F", "toggle_favourite", toggle_favourite, {}, },

docs/addons.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# How to Write an Addon - API v1.7.1
1+
# How to Write an Addon - API v1.8.0
22

33
Addons provide ways for file-browser to parse non-native directory structures. This document describes how one can create their own custom addon.
44

@@ -550,10 +550,12 @@ that were removed.
550550

551551
### Advanced Functions
552552

553-
#### `fb.clear_cache(): void`
553+
#### `fb.clear_cache(directories?: string[]): void`
554554

555555
Clears the directory cache. Use this if you are modifying the contents of directories other
556556
than the current one to ensure that their contents will be rescanned when next opened.
557+
An an array of directory strings is passed to the function only those directories
558+
will be cleared from the cache.
557559

558560
#### `fb.coroutine.assert(err?: string): coroutine`
559561

@@ -646,10 +648,15 @@ any additional arguments. The (not yet started) coroutine is returned by the fun
646648

647649
#### `fb.rescan(): coroutine`
648650

649-
Rescans the current directory. Equivalent to Ctrl+r without the cache refresh for higher level directories.
651+
Rescans the current directory. Equivalent to Ctrl+r without the cache refresh for any other directory.
650652
Returns the coroutine of the upcoming parse operation. The parse is queued and run when the script thread next goes idle,
651653
allowing one to store this value and use it to identify the triggered parse operation.
652654

655+
#### `fb.rescan_await(): coroutine`
656+
657+
Same as `fb.rescan()`, but if called from within a coroutine then the function will not
658+
return until the scan operation is complete.
659+
653660
#### `fb.redraw(): void`
654661

655662
Forces a redraw of the browser UI.

modules/apis/fb.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,17 @@ function fb.rescan()
2626
return scanning.rescan()
2727
end
2828

29-
function fb.clear_cache()
30-
cache:clear()
29+
---@async
30+
---@return thread
31+
function fb.rescan_await()
32+
cache:clear({g.state.directory})
33+
local co = scanning.rescan(nil, fb_utils.coroutine.callback())
34+
coroutine.yield()
35+
return co
36+
end
37+
38+
function fb.clear_cache(directories)
39+
cache:clear(directories)
3140
end
3241

3342
---A wrapper around scan_directory for addon API.

modules/globals.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ local o = require 'modules.options'
1010
local globals = {}
1111

1212
--sets the version for the file-browser API
13-
globals.API_VERSION = "1.7.1"
13+
globals.API_VERSION = "1.8.0"
1414

1515
---gets the current platform (only works in mpv v0.36+)
1616
---@type string?

modules/navigation/scanning.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ end
169169

170170
---rescans the folder and updates the list.
171171
---@param moving_adjacent? number|boolean
172+
---@param cb? function
172173
---@return thread # The coroutine for the triggered parse operation. May be aborted early if directory is in the cache.
173-
local function rescan(moving_adjacent)
174+
local function rescan(moving_adjacent, cb)
174175
if moving_adjacent == nil then moving_adjacent = 0 end
175176

176177
--we can only make assumptions about the directory label when moving from adjacent directories
@@ -194,6 +195,7 @@ local function rescan(moving_adjacent)
194195
if not cache.traversal_stack[1] then cache:push() end
195196

196197
ass.update_ass()
198+
if cb then fb_utils.coroutine.run(cb) end
197199
end)
198200

199201
g.state.co = co

0 commit comments

Comments
 (0)