awful: Patch prompt so it can handle different files

Signed-off-by: Damien Leone <damien.leone@gmail.com>
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Damien Leone 2008-08-13 20:13:20 +02:00 committed by Julien Danjou
parent cc367647e7
commit b2398370f9
1 changed files with 63 additions and 56 deletions

View File

@ -39,7 +39,7 @@ hooks = {}
hooks.user = {} hooks.user = {}
prompt = {} prompt = {}
prompt.history = {} prompt.history = {}
prompt.history.data = { max = 50, table = {} } prompt.history.data = {}
completion = {} completion = {}
screen = {} screen = {}
layout = {} layout = {}
@ -775,66 +775,69 @@ function eval(s)
end end
--- Load history file in history table --- Load history file in history table
local function prompt_history_load() -- @param id The prompt history identifier which is the path to the filename
if prompt.history.data.path then -- @param max Optional parameter, the maximum number of entries in file
local f = io.open(prompt.history.data.path, "r") local function prompt_history_check_load(id, max)
if id and id ~= ""
-- Read history file and not prompt.history[id] then
if f then prompt.history[id] = { max = 50, table = {} }
if max then
prompt.history[id].max = max
end
local f = io.open(id, "r")
-- Read history file
if f then
for line in f:lines() do for line in f:lines() do
table.insert(prompt.history.data.table, line) table.insert(prompt.history[id].table, line)
if #prompt.history.data.table >= prompt.history.data.max then if #prompt.history[id].table >= prompt.history[id].max then
break break
end end
end end
end end
end end
end end
--- Set path for history file
-- @param path The history file path, set "on" to enable history in the default file (~/.awesome_history)
-- @param max_cmd Optional parameter: set the max entries number in the history file, 50 by default
function prompt.history.set(path, max_cmd)
if path then
prompt.history.data.path = path
else
prompt.history.data.path = os.getenv("HOME") .. "/.awesome_history"
end
if max_cmd then
prompt.history.data.max = max_cmd
end
-- Load history stuff with current settings
prompt_history_load()
end
--- Save history table in history file --- Save history table in history file
local function prompt_history_save() -- @param id The prompt history identifier
if prompt.history.data.path then local function prompt_history_save(id)
local f = assert(io.open(prompt.history.data.path, "w")) if prompt.history[id] then
for i = 1, math.min(#prompt.history.data.table, prompt.history.data.max) do local f = assert(io.open(id, "w"))
f:write(prompt.history.data.table[i] .. "\n") for i = 1, math.min(#prompt.history[id].table, prompt.history[id].max) do
f:write(prompt.history[id].table[i] .. "\n")
end end
f:close() f:close()
end
end
--- Return the number of items in history table regarding the id
-- @param id The prompt history identifier
-- @return the number of items in history table, -1 if history is disabled
local function prompt_history_items(id)
if prompt.history[id] then
return #prompt.history[id].table
else
return -1
end end
end end
--- Add an entry to the history file --- Add an entry to the history file
-- @param id The prompt history identifier
-- @param command The command to add -- @param command The command to add
local function prompt_history_add(command) local function prompt_history_add(id, command)
if prompt.history.data.path then if prompt.history[id] then
if command ~= "" if command ~= ""
and command ~= prompt.history.data.table[#prompt.history.data.table] then and command ~= prompt.history[id].table[#prompt.history[id].table] then
table.insert(prompt.history.data.table, command) table.insert(prompt.history[id].table, command)
-- Do not exceed our max_cmd -- Do not exceed our max_cmd
if #prompt.history.data.table > prompt.history.data.max then if #prompt.history[id].table > prompt.history[id].max then
table.remove(prompt.history.data.table, 1) table.remove(prompt.history[id].table, 1)
end end
-- Save table content in file, not very optimized atm... prompt_history_save(id)
prompt_history_save()
end end
end end
end end
@ -928,7 +931,9 @@ end
-- @param textbox The textbox to use for the prompt. -- @param textbox The textbox to use for the prompt.
-- @param exe_callback The callback function to call with command as argument when finished. -- @param exe_callback The callback function to call with command as argument when finished.
-- @param completion_callback The callback function to call to get completion. -- @param completion_callback The callback function to call to get completion.
function prompt.run(args, textbox, exe_callback, completion_callback) -- @param history_path Optional parameter: file path where the history should be saved, set nil to disable history
-- @param history_max Optional parameter: set the maximum entries in history file, 50 by default
function prompt.run(args, textbox, exe_callback, completion_callback, history_path, history_max)
if not args then args = {} end if not args then args = {} end
local command = "" local command = ""
local command_before_comp local command_before_comp
@ -936,7 +941,9 @@ function prompt.run(args, textbox, exe_callback, completion_callback)
local prettyprompt = args.prompt or "" local prettyprompt = args.prompt or ""
local inv_col = args.fg_cursor or theme.fg_focus or "black" local inv_col = args.fg_cursor or theme.fg_focus or "black"
local cur_col = args.bg_cursor or theme.bg_focus or "white" local cur_col = args.bg_cursor or theme.bg_focus or "white"
local history_index = #prompt.history.data.table + 1
prompt_history_check_load(history_path, history_max)
local history_index = prompt_history_items(history_path) + 1
-- The cursor position -- The cursor position
local cur_pos = 1 local cur_pos = 1
-- The completion element to use on completion request. -- The completion element to use on completion request.
@ -967,7 +974,7 @@ function prompt.run(args, textbox, exe_callback, completion_callback)
else else
if key == "Return" then if key == "Return" then
textbox.text = "" textbox.text = ""
prompt_history_add(command) prompt_history_add(history_path, command)
exe_callback(command) exe_callback(command)
return false return false
elseif key == "Escape" then elseif key == "Escape" then
@ -1059,20 +1066,20 @@ function prompt.run(args, textbox, exe_callback, completion_callback)
if history_index > 1 then if history_index > 1 then
history_index = history_index - 1 history_index = history_index - 1
command = prompt.history.data.table[history_index] command = prompt.history[history_path].table[history_index]
cur_pos = #command + 2 cur_pos = #command + 2
end end
elseif key == "Down" then elseif key == "Down" then
if history_index < #prompt.history.data.table then if history_index < prompt_history_items(history_path) then
history_index = history_index + 1 history_index = history_index + 1
command = prompt.history.data.table[history_index] command = prompt.history[history_path].table[history_index]
cur_pos = #command + 2 cur_pos = #command + 2
elseif history_index == #prompt.history.data.table then elseif history_index == prompt_history_items(history_path) then
history_index = history_index + 1 history_index = history_index + 1
command = "" command = ""
cur_pos = 1 cur_pos = 1
end end
else else
-- len() is UTF-8 aware but #key is not, -- len() is UTF-8 aware but #key is not,