Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add title to footer #11

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion lua/presenting/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ Presenting.start = function(separator)
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
Presenting._state.slides = H.parse_slides(lines, separator)
Presenting._state.n_slides = #Presenting._state.slides
-- extract presentation title from first non-empty line of first slide
Presenting._state.title = H.extract_title(Presenting._state.slides[1])

H.create_slide_view(Presenting._state)
end
Expand Down Expand Up @@ -346,7 +348,21 @@ H.set_slide_content = function(state, slide)
)
vim.api.nvim_buf_set_option(state.slide_buf, "modifiable", orig_modifiable)

local footer_text = "presenting.nvim | " .. state.slide .. "/" .. state.n_slides
-- create slide numbers for footer
local footer_nrs = state.slide .. "/" .. state.n_slides

local presentation_title = state.title
-- get presentation title with unicode in mind
local title_len = vim.str_utfindex(presentation_title)

-- if title is too long shorten with elipsis
local width = Presenting.config.options.width
if title_len > width - #footer_nrs - 3 then
presentation_title = presentation_title:sub(1, width - #footer_nrs - 3) .. "..."
end
-- add white spaces between title and slide number
local white_space_fill = string.rep(" ", width - title_len - #footer_nrs)
local footer_text = presentation_title .. white_space_fill .. footer_nrs
Copy link
Owner

Choose a reason for hiding this comment

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

I always wanted to have this, but I was too lazy. Nice!

vim.api.nvim_buf_set_lines(state.footer_buf, 0, -1, false, { footer_text })
end

Expand All @@ -368,4 +384,21 @@ end
---@return boolean
H.in_presenting_mode = function() return Presenting._state ~= nil end

H.extract_title = function(slide)
local title = ""
for _, line in pairs(vim.split(slide, "\n")) do
-- if line is nonempty set it as title
if line:match("%S") then
title = line
break
end
end
-- strip any trailing whitespace
title = title:gsub("%s+$", "")
-- strip any starting * or # for org and md
title = title:gsub("^%*+%s*", "")
title = title:gsub("^#+%s*", "")
return title
end

Comment on lines +387 to +403
Copy link
Owner

Choose a reason for hiding this comment

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

The logic here is not quite apt to the task.

I opened the adoc file and got this title in the footer:
image
It works for md and org files. Let's make it work for adoc files as well.

Also I wonder if we can reuse the separator definitions from the config instead of having two very similar definitions.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah sure go right ahead! I'm not really using adoc.

Ultimately we should add a proper frontmatter parser like in lookatme - #12

btw I held 6 classes last week with this and it was awesome - thanks! 🙏

return Presenting