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

New option for estimating number of pages #110

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
57 changes: 47 additions & 10 deletions src/lua/addons/statusbar_pagecount.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
do
local function cb(event, token, terms)
local settings = DocumentSet.addons.pagecount or {}
local pages
if settings.enabled then
local pages = math.floor((Document.wordcount or 0) / settings.wordsperpage)
if (settings.pagesbylines) then
pages = math.ceil((Document.linecount or 0) / settings.linesperpage)
else
pages = math.ceil((Document.wordcount or 0) / settings.wordsperpage)
end

terms[#terms+1] = {
priority=80,
value=string.format("%d %s", pages,
Expand All @@ -28,7 +34,9 @@ do
local function cb()
DocumentSet.addons.pagecount = DocumentSet.addons.pagecount or {
enabled = false,
pagesbylines = true,
wordsperpage = 250,
linesperpage = 22,
}
end

Expand All @@ -44,40 +52,61 @@ function Cmd.ConfigurePageCount()
local enabled_checkbox =
Form.Checkbox {
x1 = 1, y1 = 1,
x2 = 33, y2 = 1,
x2 = 40, y2 = 1,
label = "Show approximate page count",
value = settings.enabled
}

local mode_checkbox =
Form.Checkbox {
x1 = 1, y1 = 3,
x2 = 40, y2 = 3,
label = "estimate pagecount by lines, not words",
value = settings.pagesbylines
}
local count_textfield =
Form.TextField {
x1 = 33, y1 = 3,
x2 = 43, y2 = 3,
x1 = 40, y1 = 5,
x2 = 50, y2 = 5,
value = tostring(settings.wordsperpage)
}

local count_lpptextfield =
Form.TextField {
x1 = 40, y1 = 7,
x2 = 50, y2 = 7,
value = tostring(settings.linesperpage)
}

local dialogue =
{
title = "Configure Page Count",
width = Form.Large,
height = 5,
height = 11,
stretchy = false,

["KEY_^C"] = "cancel",
["KEY_RETURN"] = "confirm",
["KEY_ENTER"] = "confirm",

enabled_checkbox,
mode_checkbox,

Form.Label {
x1 = 1, y1 = 3,
x2 = 32, y2 = 3,
x1 = 1, y1 = 5,
x2 = 39, y2 = 5,
align = Form.Left,
value = "Number of words per page:"
},
count_textfield,

Form.Label {
x1 = 1, y1 = 7,
x2 = 39, y2 = 7,
align = Form.Left,
value = "Number of lines per page:"
},
count_lpptextfield
}

while true do
local result = Form.Run(dialogue, RedrawScreen,
"SPACE to toggle, RETURN to confirm, CTRL+C to cancel")
Expand All @@ -86,13 +115,21 @@ function Cmd.ConfigurePageCount()
end

local enabled = enabled_checkbox.value
local pagesbylines = mode_checkbox.value
local wordsperpage = tonumber(count_textfield.value)

local linesperpage = tonumber(count_lpptextfield.value)

if not wordsperpage then
ModalMessage("Parameter error", "The number of words per page must be a valid number.")

elseif not linesperpage then
ModalMessage("Parameter error", "The number of lines per page must be a valid number.")

else
settings.enabled = enabled
settings.pagesbylines = pagesbylines
settings.wordsperpage = wordsperpage
settings.linesperpage = linesperpage
DocumentSet:touch()

return true
Expand Down
1 change: 1 addition & 0 deletions src/lua/document.lua
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ DocumentClass =
-- These should no longer exist; this dates from a previous attempt
-- at undo with file version 6. We're not storing the undo buffer
-- in files any more.
-- lol XD this is the content of so many programming memes
self.undostack = nil
self.redostack = nil
end,
Expand Down
14 changes: 14 additions & 0 deletions src/lua/fileio.lua
Original file line number Diff line number Diff line change
Expand Up @@ -726,4 +726,18 @@ function UpgradeDocument(oldversion)
end
DocumentSet.styles = nil
end

-- Upgrade version 7 to 8!

if (oldversion < 8) then
--this version is where the number of lines is a tracked value, and two fields were
--added to the pagecount addon

--not sure of how you're setting FILEVERSION or how you've been doing file version updates
--
--I've added a few more variables to the file, so it seems like
--there should be a new fileversion

Document.linecount = nil
end
end
19 changes: 19 additions & 0 deletions src/lua/redraw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,22 @@ do

AddEventListener(Event.Changed, cb)
end

-----------------------------------------------------------------------------
--Maintains the line count field in the current document.

do
local function cb(event, token)
local lc = 0
local nl = 0

for _, p in ipairs(Document) do
lc = lc + #p:wrap()

end

Document.linecount = lc
end

AddEventListener(Event.Changed, cb)
end