#50 Retain linebreaks in comments.
Merged 2 months ago by lruzicka. Opened 2 months ago by lruzicka.

file modified
+28 -10
@@ -26,6 +26,7 @@ 

  import readline

  import sys

  import termios

+ from textwrap import wrap

  

  # third-party python modules

  from bodhi.client.bindings import BodhiClient, BodhiClientException
@@ -33,8 +34,6 @@ 

  import dnf

  import munch

  import requests

- from textwrap import wrap

- 

  

  PROMPT = "Comment? -1/0/1 -> karma, 'i' -> ignore, other -> skip> "

  
@@ -138,7 +137,7 @@ 

  

          if update["notes"]:

              values["notes"] = "%s\n" % FEK_helper.wrap_paragraphs_prefix(

-                 update["notes"].split("\r\n"), first_prefix="      Notes: ",

+                 update["notes"].splitlines(), first_prefix="      Notes: ",

                  width=width

              )

          else:
@@ -158,6 +157,14 @@ 

  

                  text = comment['text']

  

+                 # When text is left as one chunk even if it is multiline,

+                 # the wrap function used later on eats all the whitespaces

+                 # and removes the linebreaks.

+                 # If prevented using the replace_whitespace=False built-in

+                 # settings, one cannot control the indentation any more.

+                 # For this reason, let us split the comment on new lines.

+                 text = text.splitlines()

+ 

                  # the format of the user has changed, add a data member

                  comment["username"] = comment["user"]["name"]

  
@@ -200,11 +207,20 @@ 

                      # when they are not from the Bodhi user.

                      tint = "reset"

  

+ 

+                 # If there is text (if bodhi-comments is none or important

+                 # the text might be empty. In that case we'd skip it.

                  if text:

-                     wrapped = wrap(color(text.strip(), tint, colorize),

+                     # Deal with each line of the comment separately to

+                     # enable correct indentation.

+                     for line in text:

+                         # Skip empty lines to keep the output more compact.

+                         if not line:

+                             continue

+                         wrapped = wrap(color(line.strip(), tint, colorize),

                                     initial_indent=(indent+"  "),

                                     subsequent_indent=indent+"  ", width=width)

-                     comments.append("\n".join(wrapped))

+                         comments.append("\n".join(wrapped))

  

              val += "\n".join(comments).lstrip() + "\n"

              values["comments"] = val
@@ -282,13 +298,15 @@ 

      @staticmethod

      def return_important_comment(comment_text):

          """Takes a Bodhi comment and returns it, if it is important,

-         or returns empty string."""

+         or returns an empty string."""

          important = ["edited this update"]

-         # Check lines in important, whether they can be found

-         # in the comment.

+         # Iterate over the important and test against the comment_text.

          for message in important:

-             if message in comment_text:

-                 return comment_text

+             # Comment_text might have multiple lines, so iterate over

+             # each of them and test if we have a match.

+             for line in comment_text:

+                 if message in line:

+                     return comment_text

          return ""

  

  USAGE = """

Earlier, Fedora Easy Karma would lose comments' line breaks
and it would put comments on one line which was not very
good with multiline comments.

This PR fixes the situation and retains multiline comments.

Fixes: https://pagure.io/fedora-easy-karma/issue/49

rebased onto 11173e7

2 months ago

This would make it more readable for me:

for line in text:
    # Skip empty lines to keep the output more compact
    if not line:
        continue
    wrapped = ...
    comments.append(...)

Is it doing the same thing as your code?

There's a nice builtin method for this:

text = str.splitlines(text)

2 new commits added

  • Make condition more readable.
  • Use str.splitlines instead of str.split
2 months ago

Is it doing the same thing as your code?

Yes, it is.

Is it doing the same thing as your code?

Yes, it is.

In that case, I think it's better :-) It performs two distinct tasks as two pieces of code, instead of merging both into a single one, which makes it opaque. And saves you code indentation.

1 new commit added

  • Move textwrap into standard modules
2 months ago

1 new commit added

  • Make notes keep linebreaks, too
2 months ago

LGTM. Please don't forget to squash before merging, thanks :-)

rebased onto 11173e7

2 months ago

Commit 2d37292 fixes this pull-request

Pull-Request has been merged by lruzicka

2 months ago
Metadata