awful: add prompt history

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Damien Leone 2008-08-11 19:47:23 +02:00 committed by Julien Danjou
parent ea930d8972
commit df7d60faf3
2 changed files with 96 additions and 7 deletions

View File

@ -67,6 +67,9 @@ beautiful.init(theme_path)
-- to inform it about colors we want it to draw. -- to inform it about colors we want it to draw.
awful.beautiful.register(beautiful) awful.beautiful.register(beautiful)
-- Enable prompt history
awful.prompt.history.set()
-- Uncomment this to activate autotabbing -- Uncomment this to activate autotabbing
-- tabulous.autotab_start() -- tabulous.autotab_start()
-- }}} -- }}}
@ -237,10 +240,10 @@ keybinding({ modkey, "Shift" }, "space", function () awful.layout.inc(layouts, -
-- Prompt -- Prompt
keybinding({ modkey }, "F1", function () keybinding({ modkey }, "F1", function ()
awful.prompt({ prompt = "Run: " }, mypromptbox, awful.spawn, awful.completion.bash) awful.prompt.run({ prompt = "Run: " }, mypromptbox, awful.spawn, awful.completion.bash)
end):add() end):add()
keybinding({ modkey }, "F4", function () keybinding({ modkey }, "F4", function ()
awful.prompt({ prompt = "Run Lua code: " }, mypromptbox, awful.eval) awful.prompt.run({ prompt = "Run Lua code: " }, mypromptbox, awful.eval)
end):add() end):add()
--- Tabulous, tab manipulation --- Tabulous, tab manipulation

View File

@ -38,6 +38,8 @@ beautiful = {}
hooks = {} hooks = {}
hooks.user = {} hooks.user = {}
prompt = {} prompt = {}
prompt.history = {}
prompt.history.data = { max = 50, index = 0, table = {} }
completion = {} completion = {}
screen = {} screen = {}
layout = {} layout = {}
@ -758,11 +760,77 @@ function eval(s)
return assert(loadstring("return " .. s))() return assert(loadstring("return " .. s))()
end end
--- Load history file in history table
local function prompt_history_load()
if prompt.history.data.path then
local f = io.open(prompt.history.data.path, "r")
-- Read history file
if f then
for line in f:lines() do
table.insert(prompt.history.data.table, line)
if #prompt.history.data.table >= prompt.history.data.max then
break
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_size 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
local function prompt_history_save()
if prompt.history.data.path then
local f = assert(io.open(prompt.history.data.path, "w"))
for i = 1, math.min(#prompt.history.data.table, prompt.history.data.max) do
f:write(prompt.history.data.table[i] .. "\n")
end
f:close()
end
end
--- Add an entry to the history file
-- @param command The command to add
local function prompt_history_add(command)
if prompt.history.data.path
and command
and command ~= prompt.history.data.table[#prompt.history.data.table] then
table.insert(prompt.history.data.table, command)
-- Do not exceed our max_cmd
if #prompt.history.data.table > prompt.history.data.max then
table.remove(prompt.history.data.table, 1)
end
-- Save table content in file, not very optimized atm...
prompt_history_save()
prompt.history.data.index = #prompt.history.data.table + 1
end
end
--- Use bash completion system to complete command and filename. --- Use bash completion system to complete command and filename.
-- @param command The command line. -- @param command The command line.
-- @param cur_pos The cursor position. -- @param cur_pos The cursor position.
-- @paran ncomp The element number to complete. -- @paran ncomp The element number to complete.
-- @return The new commande and the new cursor position. -- @return The new command and the new cursor position.
function completion.bash(command, cur_pos, ncomp) function completion.bash(command, cur_pos, ncomp)
local wstart = 1 local wstart = 1
local wend = 1 local wend = 1
@ -847,12 +915,12 @@ 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(args, textbox, exe_callback, completion_callback) function prompt.run(args, textbox, exe_callback, completion_callback)
if not args then args = {} end if not args then args = {} end
local command = "" local command = ""
local command_before_comp local command_before_comp
local cur_pos_before_comp local cur_pos_before_comp
local prompt = 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"
-- The cursor position -- The cursor position
@ -862,7 +930,7 @@ function prompt(args, textbox, exe_callback, completion_callback)
if not textbox or not exe_callback then if not textbox or not exe_callback then
return return
end end
textbox.text = prompt .. prompt_text_with_cursor(text, inv_col, cur_col, cur_pos) textbox.text = prettyprompt .. prompt_text_with_cursor(text, inv_col, cur_col, cur_pos)
capi.keygrabber.run( capi.keygrabber.run(
function (mod, key) function (mod, key)
local has_ctrl = false local has_ctrl = false
@ -885,6 +953,7 @@ function prompt(args, textbox, exe_callback, completion_callback)
else else
if key == "Return" then if key == "Return" then
textbox.text = "" textbox.text = ""
prompt_history_add(command)
exe_callback(command) exe_callback(command)
return false return false
elseif key == "Escape" then elseif key == "Escape" then
@ -972,6 +1041,23 @@ function prompt(args, textbox, exe_callback, completion_callback)
cur_pos = cur_pos - 1 cur_pos = cur_pos - 1
elseif key == "Right" then elseif key == "Right" then
cur_pos = cur_pos + 1 cur_pos = cur_pos + 1
elseif key == "Up" then
if prompt.history.data.index > 1 then
prompt.history.data.index = prompt.history.data.index - 1
command = prompt.history.data.table[prompt.history.data.index]
cur_pos = #command + 2
end
elseif key == "Down" then
if prompt.history.data.index < #prompt.history.data.table then
prompt.history.data.index = prompt.history.data.index + 1
command = prompt.history.data.table[prompt.history.data.index]
cur_pos = #command + 2
elseif prompt.history.data.index == #prompt.history.data.table then
command = ""
cur_pos = 1
end
else else
-- len() is UTF-8 aware but #key is not, -- len() is UTF-8 aware but #key is not,
-- so check that we have one UTF-8 char but advance the cursor of # position -- so check that we have one UTF-8 char but advance the cursor of # position
@ -988,7 +1074,7 @@ function prompt(args, textbox, exe_callback, completion_callback)
end end
-- Update textbox -- Update textbox
textbox.text = prompt .. prompt_text_with_cursor(command, inv_col, cur_col, cur_pos) textbox.text = prettyprompt .. prompt_text_with_cursor(command, inv_col, cur_col, cur_pos)
return true return true
end) end)