Skip to content

Commit

Permalink
console.lua: allow persisting the command history
Browse files Browse the repository at this point in the history
  • Loading branch information
guidocella committed Jan 7, 2025
1 parent f7e2a8c commit 1dd24d6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
10 changes: 10 additions & 0 deletions DOCS/man/console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ Configurable Options

Remove duplicate entries in history as to only keep the latest one.

``persist_history``
Default: no

Whether to save the command history to a file and load it at startup.

``history_path``
Default: ``~~state/command_history.txt``

The file path for ``persist_history`` (see `PATHS`_).

``font_hw_ratio``
Default: auto

Expand Down
67 changes: 67 additions & 0 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ local opts = {
scale_with_window = "auto",
case_sensitive = platform ~= 'windows' and true or false,
history_dedup = true,
persist_history = false,
history_path = '~~state/command_history.txt',
font_hw_ratio = 'auto',
}

Expand Down Expand Up @@ -82,6 +84,7 @@ local histories = {[id] = {}}
local history = histories[id]
local history_pos = 1
local searching_history = false
local history_is_dirty = false
local log_buffers = {[id] = {}}
local key_bindings = {}
local dont_bind_up_down = false
Expand Down Expand Up @@ -655,6 +658,10 @@ local function history_add(text)
end

history[#history + 1] = text

if id == default_id then
history_is_dirty = true
end
end

local function handle_cursor_move()
Expand Down Expand Up @@ -1722,6 +1729,24 @@ local function show_and_type(text, cursor_pos)
end
end

local function read_history()
if opts.persist_history == false then
return
end

local history_file = io.open(mp.command_native({'expand-path', opts.history_path}))

if history_file == nil then
return
end

for command in history_file:lines() do
history[#history + 1] = command
end

history_file:close()
end

-- Add a global binding for enabling the REPL. While it's enabled, its bindings
-- will take over and it can be closed with ESC.
mp.add_key_binding(nil, 'enable', function()
Expand Down Expand Up @@ -1883,6 +1908,48 @@ mp.register_event('log-message', function(e)
terminal_styles[e.level])
end)

mp.register_event('shutdown', function ()
if opts.persist_history == false or history_is_dirty == false then
return
end

history = histories[default_id]
local history_to_save = table.concat(history, '\n') .. '\n'
local history_path = mp.command_native({'expand-path', opts.history_path})

-- Reread the history file to get lines that other mpv instances may have
-- added.
local history_file = io.open(history_path)
if history_file then
local history_map = {}
for _, command in pairs(history) do
history_map[command] = true
end

for command in history_file:lines() do
if not history_map[command] then
history_to_save = history_to_save .. command .. '\n'
end
end

history_file:close()
end

local error_message
history_file, error_message = io.open(history_path, 'w')

if history_file == nil then
mp.msg.error('Failed to write console history: ' .. error_message)

return
end

history_file:write(history_to_save)
history_file:close()
end)

require 'mp.options'.read_options(opts, nil, update)

read_history()

collectgarbage()

0 comments on commit 1dd24d6

Please sign in to comment.