Merge pull request #3001 from ArenaL5/abbreviate

Abbreviate cheatsheet
This commit is contained in:
mergify[bot] 2020-02-22 23:47:12 +00:00 committed by GitHub
commit 923c3e21f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 103 additions and 13 deletions

View File

@ -55,7 +55,11 @@ local widget = {
widget.hide_without_description = true
--- Merge hotkey records into one if they have the same modifiers and
-- description.
-- description. Records with five or more keys will abbreviate them.
--
-- This property only affects hotkey records added via `awful.key` keybindings.
-- Cheatsheets for external programs are static and will present merged records
-- regardless of the value of this property.
-- @tfield boolean widget.merge_duplicates
-- @param boolean
widget.merge_duplicates = true
@ -110,7 +114,8 @@ widget.merge_duplicates = true
-- @tparam[opt] table args Configuration options for the widget.
-- @tparam[opt] boolean args.hide_without_description Don't show hotkeys without descriptions.
-- @tparam[opt] boolean args.merge_duplicates Merge hotkey records into one if
-- they have the same modifiers and description.
-- they have the same modifiers and description. Records with five keys or more
-- will abbreviate them.
-- @tparam[opt] int args.width Widget width.
-- @tparam[opt] int args.height Widget height.
-- @tparam[opt] color args.bg Widget background color.
@ -187,7 +192,14 @@ function widget.new(args)
_colors_counter = {},
_group_list = {},
_widget_settings_loaded = false,
_keygroups = {},
}
for k, v in pairs(awful.key.keygroups) do
widget_instance._keygroups[k] = {}
for k2, v2 in pairs(v) do
widget_instance._keygroups[k][k2] = widget_instance.labels[v2[1]] or v2[1]
end
end
function widget_instance:_load_widget_settings()
@ -247,6 +259,7 @@ function widget.new(args)
if not target[group] then target[group] = {} end
local new_key = {
key = (self.labels[key] or key),
keylist = {(self.labels[key] or key)},
mod = joined_mods,
description = data.description
}
@ -256,6 +269,7 @@ function widget.new(args)
else
if self.merge_duplicates and joined_mods == target[group][index].mod then
target[group][index].key = target[group][index].key .. "/" .. new_key.key
table.insert(target[group][index].keylist, new_key.key)
else
while target[group][index] do
index = index .. " "
@ -285,6 +299,53 @@ function widget.new(args)
end
function widget_instance:_abbreviate_awful_keys()
-- This method is intended to abbreviate the keys of a merged entry (not
-- the modifiers) if and only if the entry consists of five or more
-- correlative keys from the same keygroup.
--
-- For simplicity, it checks only the first keygroup which contains the
-- first key. If any of the keys in the merged entry is not in this
-- keygroup, or there are any gaps between the keys (e.g. the entry
-- contains the 2nd, 3rd, 5th, 6th, and 7th key in
-- awful.key.keygroups.numrow, but not the 4th) this method does not try
-- to abbreviate the entry.
--
-- Cheatsheets for external programs are abbreviated by hand where
-- applicable: they do not need this method.
for _, keys in pairs(self._cached_awful_keys) do
for _, params in pairs(keys) do
if #params.keylist > 4 then
-- assuming here keygroups will never overlap;
-- if they ever do, another for loop will be necessary:
local keygroup = gtable.find_first_key(self._keygroups, function(_, v)
return not not gtable.hasitem(v, params.keylist[1])
end)
local first, last, count, tally = nil, nil, 0, {}
for _, k in ipairs(params.keylist) do
local i = gtable.hasitem(self._keygroups[keygroup], k)
if i and not tally[i] then
tally[i] = k
if (not first) or (i < first) then first = i end
if (not last) or (i > last) then last = i end
count = count + 1
elseif not i then
count = 0
break
end
end
-- this conditional can only be true if there are more than
-- four actual keys (discounting duplicates) and ALL of
-- these keys can be found one after another in a keygroup:
if count > 4 and last - first + 1 == count then
params.key = tally[first] .. "" .. tally[last]
end
end
end
end
end
function widget_instance:_import_awful_keys()
if next(self._cached_awful_keys) then
return
@ -295,6 +356,9 @@ function widget.new(args)
end
end
self:_sort_hotkeys(self._cached_awful_keys)
if self.merge_duplicates then
self:_abbreviate_awful_keys()
end
end

View File

@ -60,9 +60,9 @@ local gobject = require("gears.object")
-- @property modifiers
-- @tparam table modifiers
--- The key description.
--- The description of the function run from a key binding.
--
-- This is used, for example, by the `awful.hotkey_popup`.
-- This is used, for example, by `awful.hotkeys_popup`.
--
-- @property description
-- @param string
@ -74,9 +74,9 @@ local gobject = require("gears.object")
-- @property name
-- @param string
--- The key group.
--- The key group bound to a function in a key binding.
--
-- This is used, for example, by the `awful.hotkey_popup`.
-- This is used, for example, by `awful.hotkeys_popup`.
--
-- @property group
-- @param string
@ -298,22 +298,48 @@ local function new_common(mod, _keys, press, release, data)
return setmetatable(ret, obj_mt)
end
local keygroups = {
-- Left: the keycode in a format which regular awful.key understands.
-- Right: the argument of the function ran upon executing the key binding.
--- The default definitions of keygroups.
--
-- A definition for a keygroup (say, **arrows**) can be accessed by indexing
-- this table (e.g. `awful.key.keygroups.arrows`).
--
-- Every definition is given as an array, where every element is another array
-- with the following structure:
--
-- * The first element is a string representing a key, in any format the
-- property `key` will allow.
-- * The second element is a value. Key bindings created by `awful.key` and a
-- `keygroup` are bound to a 1-parameter function, whose parameter is this
-- second element.
--
-- As an example, **arrows** is currently defined thus:
--
-- arrows = {
-- {"Left" , "Left" },
-- {"Right" , "Right" },
-- {"Up" , "Up" },
-- {"Down" , "Down" },
-- }
--
-- This table is acessed internally by Awesome. Users will usually use key
-- bindings with the property `keygroup` instead of accessing this table
-- directly.
-- @name awful.key.keygroups
-- @class table
key.keygroups = {
numrow = {},
arrows = {
{"Left" , "Left" },
{"Right" , "Right" },
{"Up" , "Up" },
{"Down", "Down" },
{"Down" , "Down" },
}
}
-- Technically, this isn't very popular, but we have been doing this for 12
-- years and nobody complained too loudly.
for i = 1, 10 do
table.insert(keygroups.numrow, {"#" .. i + 9, i == 10 and 0 or i})
table.insert(key.keygroups.numrow, {"#" .. i + 9, i == 10 and 0 or i})
end
-- Allow key objects to provide more than 1 key.
@ -328,8 +354,8 @@ local function get_keys(args)
"Please provide either the `key` or `keygroup` property, not both"
)
assert(keygroups[args.keygroup], "Please provide a valid keygroup")
return keygroups[args.keygroup]
assert(key.keygroups[args.keygroup], "Please provide a valid keygroup")
return key.keygroups[args.keygroup]
end
function key.new(args, _key, press, release, data)