Skip to content

Commit 4e6ef86

Browse files
committed
seek-to: Refactor toggle_seek_mode()
Simplify seek state handling by introducing a global `seek_mode` variable instead of juggling three separate booleans. This ensures that only one mode is active at a time and reduces redundant checks. To be more detailed, `seek_mode` acts as the single source of truth. Only one state can be assigned to it at a time, or none. The prefix code logic is also changed to use inline table lookup, which looks cleaner and more readable.
1 parent 5c55f2d commit 4e6ef86

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

scripts/seek-to.lua

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ local platform = mp.get_property_native("platform")
1414
local active = false
1515
local cursor_position = 1
1616
local time_scale = {60*60*10, 60*60, 60*10, 60, 10, 1, 0.1, 0.01, 0.001}
17+
local seek_mode = nil
1718

1819
local ass_begin = mp.get_property("osd-ass-cc/0")
1920
local ass_end = mp.get_property("osd-ass-cc/1")
@@ -65,7 +66,7 @@ function show_seeker()
6566
str = str .. history[history_position][i]
6667
end
6768
end
68-
local prefix = seek_end and "<" or (seek_bwd and "-" or (seek_fwd and "+" or ""))
69+
local prefix = ({seek_end = "<", seek_bwd = "-", seek_fwd = "+"})[seek_mode] or ""
6970
mp.osd_message("Seek to: " .. ass_begin .. prefix .. str .. ass_end, timer_duration)
7071
end
7172

@@ -134,7 +135,7 @@ function seek_to()
134135
message_displayed = true
135136
return
136137
end
137-
local prefix = seek_end and "-" or ""
138+
local prefix = (seek_mode == "seek_end") and "-" or ""
138139
mp.commandv("osd-bar", "seek", prefix .. seek_time, "absolute")
139140
--deduplicate consecutive timestamps
140141
if #history == 1 or not time_equal(history[history_position], history[#history - 1]) then
@@ -163,15 +164,10 @@ function history_move(up)
163164
end
164165

165166
function toggle_seek_mode(mode)
166-
if mode == "seek_end" then
167-
seek_end = not seek_end
168-
seek_fwd, seek_bwd = false, false
169-
elseif mode == "seek_fwd" then
170-
seek_fwd = not seek_fwd
171-
seek_end, seek_bwd = false, false
172-
elseif mode == "seek_bwd" then
173-
seek_bwd = not seek_bwd
174-
seek_end, seek_fwd = false, false
167+
if seek_mode == mode then
168+
seek_mode = nil
169+
else
170+
seek_mode = mode
175171
end
176172
show_seeker()
177173
end

0 commit comments

Comments
 (0)