Skip to content

Commit

Permalink
feat: added options to align columns left, center or right #82
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 26, 2021
1 parent 2b83fe7 commit 2467fb1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ WhichKey comes with the following defaults:
height = { min = 4, max = 25 }, -- min and max height of the columns
width = { min = 20, max = 50 }, -- min and max width of the columns
spacing = 3, -- spacing between columns
align = "left", -- align columns left, center or right
},
ignore_missing = false, -- enable this to hide mappings for which you didn't specify a label
hidden = { "<silent>", "<cmd>", "<Cmd>", "<CR>", "call", "lua", "^:", "^ "}, -- hide mapping boilerplate
Expand Down
1 change: 1 addition & 0 deletions lua/which-key/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ local defaults = {
height = { min = 4, max = 25 }, -- min and max height of the columns
width = { min = 20, max = 50 }, -- min and max width of the columns
spacing = 3, -- spacing between columns
align = "left", -- align columns left, center or right
},
ignore_missing = false, -- enable this to hide mappings for which you didn't specify a label
hidden = { "<silent>", "<cmd>", "<Cmd>", "<CR>", "^:", "^ ", "^call ", "^lua " }, -- hide mapping boilerplate
Expand Down
12 changes: 11 additions & 1 deletion lua/which-key/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,18 @@ function Layout:layout(win)
local pad_top = self.options.window.padding[3]
local pad_left = self.options.window.padding[4]

local columns_used = math.min(columns, math.ceil(#self.items / height))
local offset_x = 0
if columns_used < columns then
if self.options.layout.align == "right" then
offset_x = (columns - columns_used) * column_width
elseif self.options.layout.align == "center" then
offset_x = (columns - columns_used) * column_width / 2
end
end

for _, item in pairs(self.items) do
local start = (col - 1) * column_width + self.options.layout.spacing
local start = (col - 1) * column_width + self.options.layout.spacing + offset_x
if col == 1 then
start = start + pad_left
end
Expand Down

3 comments on commit 2467fb1

@lcrockett
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers on the additional functionality. Configuring alignment to left or right works. When it's configured to center it doesn't work and reports E5108: Error executing lua Vim:E805: Using a Float as a Number

Configuration snippet:

require('which-key').setup {
  layout = {
    align = 'center',
    height = { min = 1, max = 25 },
    spacing = 10,
    width = { min = 20, max = 50 }
  },
  plugins = {
    marks = false,
    spelling = {
      enabled = true,
      suggestions = 36
    }
  }
}

@folke
Copy link
Owner Author

@folke folke commented on 2467fb1 May 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lcrockett sorry about that! Just pushed a fix. It seems that my tests gave an even number for offset_x :)

@lcrockett
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@folke both the fix and functionality work well, cheers

Please sign in to comment.