Skip to content

Commit 8b4c81f

Browse files
Fix non-ascii chars alignment in agenda view and fix setting tags on headlines with non ascii chars
1 parent 10b102f commit 8b4c81f

File tree

6 files changed

+25
-10
lines changed

6 files changed

+25
-10
lines changed

lua/orgmode/agenda/views/agenda.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function AgendaView:build()
186186
table.insert(content, { line_content = self:_format_day(day) })
187187

188188
local longest_items = utils.reduce(agenda_items, function(acc, agenda_item)
189-
acc.category = math.max(acc.category, agenda_item.headline:get_category():len())
189+
acc.category = math.max(acc.category, vim.api.nvim_strwidth(agenda_item.headline:get_category()))
190190
acc.label = math.max(acc.label, vim.api.nvim_strwidth(agenda_item.label))
191191
return acc
192192
end, {
@@ -264,10 +264,10 @@ end
264264
---@return table
265265
function AgendaView.build_agenda_item_content(agenda_item, longest_category, longest_date, line_nr)
266266
local headline = agenda_item.headline
267-
local category = string.format(' %-' .. longest_category .. 's', headline:get_category() .. ':')
267+
local category = ' ' .. utils.pad_right(string.format('%s:', headline:get_category()), longest_category)
268268
local date = agenda_item.label
269269
if date ~= '' then
270-
date = string.format(' %-' .. longest_date .. 's', agenda_item.label)
270+
date = ' ' .. utils.pad_right(agenda_item.label, longest_date)
271271
end
272272
local todo_keyword = agenda_item.headline.todo_keyword.value
273273
local todo_padding = ''

lua/orgmode/agenda/views/todos.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function AgendaTodosView.generate_view(items, content, filters)
6161
items = sort_todos(items)
6262
local offset = #content
6363
local longest_category = utils.reduce(items, function(acc, todo)
64-
return math.max(acc, todo:get_category():len())
64+
return math.max(acc, vim.api.nvim_strwidth(todo:get_category()))
6565
end, 0)
6666

6767
for i, headline in ipairs(items) do
@@ -74,7 +74,7 @@ function AgendaTodosView.generate_view(items, content, filters)
7474
end
7575

7676
function AgendaTodosView.generate_todo_item(headline, longest_category, line_nr)
77-
local category = string.format(' %-' .. (longest_category + 1) .. 's', headline:get_category() .. ':')
77+
local category = ' ' .. utils.pad_right(string.format('%s:', headline:get_category()), longest_category + 1)
7878
local todo_keyword = headline.todo_keyword.value
7979
local todo_keyword_padding = todo_keyword ~= '' and ' ' or ''
8080
local line = string.format(' %s%s%s %s', category, todo_keyword_padding, todo_keyword, headline.title)

lua/orgmode/parser/table/cell.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local utils = require('orgmode.utils')
2+
13
---@class TableCell
24
---@field row TableRow
35
---@field value string
@@ -32,7 +34,7 @@ function TableCell:compile()
3234
if self.row.is_separator then
3335
val = string.format('-%s-', string.rep('-', width))
3436
else
35-
val = string.format(' %-' .. width .. 's ', self.value)
37+
val = string.format(' %s ', utils.pad_right(self.value, width))
3638
end
3739
local start_col = self.row.table.start_col + 2
3840
if self.col > 1 then

lua/orgmode/treesitter/headline.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,24 @@ function Headline:set_tags(tags)
4545
end
4646
end
4747

48+
local txt = query.get_node_text(predecessor, 0)
4849
local pred_end_row, pred_end_col, _ = predecessor:end_()
49-
local end_col = vim.api.nvim_strwidth(vim.fn.getline(pred_end_row + 1))
50+
local line = vim.fn.getline(pred_end_row + 1)
51+
local stars = line:match('^%*+%s*')
52+
local end_col = line:len()
5053

5154
local text = ''
5255
tags = vim.trim(tags):gsub('^:', ''):gsub(':$', '')
5356
if tags ~= '' then
5457
tags = ':' .. tags .. ':'
5558

5659
local to_col = config.org_tags_column
60+
local tags_width = vim.api.nvim_strwidth(tags)
5761
if to_col < 0 then
58-
local tags_width = vim.api.nvim_strwidth(tags)
5962
to_col = math.abs(to_col) - tags_width
6063
end
6164

62-
local spaces = math.max(to_col - pred_end_col, 1)
65+
local spaces = math.max(to_col - (vim.api.nvim_strwidth(txt) + stars:len()) - tags_width, 1)
6366
text = string.rep(' ', spaces) .. tags
6467
end
6568

lua/orgmode/utils/init.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,4 +578,14 @@ function utils.open_float(name, scale)
578578
})
579579
end
580580

581+
---@param str string
582+
---@param amount number
583+
function utils.pad_right(str, amount)
584+
local spaces = math.max(0, amount - vim.api.nvim_strwidth(str))
585+
if spaces == 0 then
586+
return str
587+
end
588+
return string.format('%s%s', str, string.rep(' ', spaces))
589+
end
590+
581591
return utils

tests/plenary/ui/mappings_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ describe('Mappings', function()
284284
vim.fn.cursor(3, 1)
285285
vim.cmd([[norm ,oA]])
286286
assert.are.same(
287-
'* TODO Test orgmode :ARCHIVE:',
287+
'* TODO Test orgmode :ARCHIVE:',
288288
vim.fn.getline(3)
289289
)
290290
end)

0 commit comments

Comments
 (0)