Skip to content

Commit

Permalink
Fix last page rarely not having footer
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasvana10 committed May 15, 2024
1 parent 079b36a commit 2f4796e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 56 deletions.
27 changes: 8 additions & 19 deletions commits2pdf/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"You cannot set scaling when using the gen1 PDF generator."
)
INVALID_BASENAME_WARNING = (
"If the directory name of the git repository you are accessing has been"
" renamed, there may be incorrect diff links in the output PDF."
"To ensure all hyperlinks in the output PDF are valid, please check that your "
"repository's folder name is correct."
)


Expand All @@ -49,25 +49,14 @@
" ({}) instead."
)
NONEXISTING_OR_INVALID_REPO_ERROR = (
"The repository you specified does not exist or is invalid. Attempting to"
" delete tree..."
"The repository does not exist or is invalid. Attempting to delete."
)
INVALID_GIT_REPO_ERROR = (
"The path to the repo you specified ({}) does not contain a .git file."
" Exiting..."
)
CLONING_REPO_INFO = (
"Cloning the .git file of your specified repository. This may take a"
" while..."
)
NONEXISTING_REPO_ERROR = (
"The repository you specified does not exist. Exiting..."
)
MUST_RECLONE_ERROR = (
"Please delete your repository from your file system and rerun your"
" console command to clone a different branch of your specified"
" repository."
"The path you entered ({}) does not contain a .git file."
)
CLONING_REPO_INFO = "Cloning. This may take a while."
NONEXISTING_REPO_ERROR = "The repository does not exist."
MUST_RECLONE_ERROR = "Please delete your repository and try again."
ZERO_COMMITS_WARNING = (
"Based on your filtering parameters, the total commit count has been reduced "
"to zero."
Expand All @@ -87,7 +76,7 @@


# General PDF messages
WRITING_PDF_INFO = "Writing your PDF to {}"
WRITING_PDF_INFO = "Writing PDF to {}"
INVALID_OUTPUT_DIR_ERROR = "Invalid characters in output directory."
INVALID_FILENAME_ERROR = "Invalid characters in filename."
FILENAME = "{}-commit_report.pdf"
Expand Down
61 changes: 25 additions & 36 deletions commits2pdf/render_fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def wrapper(self, *args, **kwargs):
self.set_fill_color(*FPDF_DARK["background"])
self.rect(h=self.h, w=self.w, x=0, y=0, style="DF")
return result

return wrapper


Expand All @@ -53,7 +52,6 @@ def __init__(
mode: str,
scaling: str,
) -> None:
super().__init__() # portrait; millimetres; a4
FPDF_PDF._set_scaling(scaling)

self.err_flag: bool = False
Expand Down Expand Up @@ -110,7 +108,7 @@ def _configure_fpdf(self):

def _prepare_and_draw(self):
self._p.add_page()
self._draw_page_bg()
self._bg()
self._draw_title_page()
if self._mode == "unstable":
self.do_pre_vis: bool = True
Expand All @@ -123,6 +121,7 @@ def _prepare_and_draw(self):
self._draw_commits()

def footer(self) -> None:
"""Draw the footer of a page."""
self._p.set_y(-1 * (MARGIN_TB / 2))
self._set_font(*MARGIN_FONT)
self._p.set_text_color(*self._ap["text"])
Expand All @@ -131,7 +130,7 @@ def footer(self) -> None:
0, 0, f"Generated by commits2pdf at {self.timestamp}", 0, 0, "C"
)

def _draw_page_bg(self) -> None:
def _bg(self) -> None:
"""Draw the page background as a rectangle."""
self._p.rect(h=self._p.h, w=self._p.w, x=0, y=0, style="DF")

Expand All @@ -144,43 +143,32 @@ def _write(self) -> None:
def _set_font(
self, *args: List[float], obj: Union[str, object] = "main"
) -> None:
"""Set the current font to a RGB value."""
"""Set the current font to a given family, style and size."""
p = self._p if obj == "main" else obj
p.set_font(args[0], args[1], args[2])

def _draw_newpage_commit(self, commit, no_divider=False):
def _multipage_commit(self, commit, no_divider=False):
"""Draw a commit that cannot fit on the existing page."""
self.footer()
# Ensure that a new page is not added when drawing the commit (that is
# a multipage commit) to prevent page 2 being empty.

# Prevent adding a new page on the first commit
if self._p.page_no() == 2 and self.commit_counter == 0:
self._p.set_y(MARGIN_TB)
else:
else: # It is ok to add a new page, since the current one must have commits
self._p.add_page()
self._draw_page_bg()

self._bg()
self._p.set_auto_page_break(auto=True, margin=MARGIN_TB)
self._p.footer = (
self.footer
) # Temporarily override the empty ``footer``
# method inherited from ``FPDF`` to
# automatically generate a footer if this
# new commit spans more than a single page.
page_no_before_draw = self._p.page
self._draw_commit(commit, no_divider=no_divider)
self._commit(commit, no_divider=no_divider)
self._p.set_auto_page_break(auto=False)
self._p.footer = footer # Set the footer method of ``self._p`` back to
# an empty method to prevent a recursion error
# when cloning an instance of ``FPDF``.
if self._p.page > page_no_before_draw:
self.footer()
self.commit_counter += 1

def _draw_commits(self) -> None:
"""Driver function to draw all the commits using either the
pre-visualisation method or the commit height estimation method.
"""
self._p.add_page() # Draw separate to the title page
self._draw_page_bg()
self._bg()
self.commit_counter: int = 0
# gen2b
if self._mode == "unstable":
Expand All @@ -189,18 +177,19 @@ def _draw_commits(self) -> None:
ncols=85,
desc="GENERATING",
):
result = self._draw_commit(commit, pre_vis=True)
result = self._commit(commit, pre_vis=True)
self._p.footer = self.footer
if result == "NEW_PAGE_OK": # Break page
self._draw_newpage_commit(commit)
self._multipage_commit(commit)
elif (
result == "NEW_PAGE_OK_BUT_NO_DIVIDER"
): # The divider just barely doesn't fit
self._draw_newpage_commit(commit, no_divider=True)
self._multipage_commit(commit, no_divider=True)
else: # No need to break the page
self._draw_commit(commit)
self._commit(commit)
self.commit_counter += 1
if self.commit_counter == self.commit_count:
self.footer()
self._p.footer = footer
self.footer()

# gen2a
elif self._mode == "stable":
Expand All @@ -209,13 +198,13 @@ def _draw_commits(self) -> None:
ncols=85,
desc="GENERATING",
):
self._p.footer = self.footer
if self._commit_exceeds_size(commit): # Break page
self._draw_newpage_commit(commit)
self._multipage_commit(commit)
else:
self._draw_commit(commit)
self._commit(commit)
self.commit_counter += 1
if self.commit_counter == self.commit_count:
self.footer()
self.footer()

def _get_pdf_object(self, pre_vis):
if not pre_vis or not self.do_pre_vis:
Expand All @@ -234,7 +223,7 @@ def _get_pdf_object(self, pre_vis):
self.err_flag = True
exit(1)

def _draw_commit(
def _commit(
self,
commit: Dict[str, str],
pre_vis: bool = False,
Expand Down Expand Up @@ -421,6 +410,6 @@ def _draw_title_page(self) -> None:
0,
self._p.font_size * 1.5,
align="C",
txt=f"Commit count: {len(self._commits.filtered_commits)}",
txt=f"Commit count: {self.commit_count}",
)
self._p.ln()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="commits2pdf",
version="1.1.12",
version="1.1.13",
author="Tomas Vana",
url="https://github.com/tomasvana10/commits2pdf",
description="Convert Git commits to a PDF",
Expand Down

0 comments on commit 2f4796e

Please sign in to comment.