Conditions | 8 |
Total Lines | 85 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | #!/usr/bin/env python |
||
90 | def pprint(self, short=False): |
||
91 | """Returns a pretty-printed version of the entry. |
||
92 | If short is true, only print the title.""" |
||
93 | # Handle indentation |
||
94 | if self.journal.config["indent_character"]: |
||
95 | indent = self.journal.config["indent_character"].rstrip() + " " |
||
96 | else: |
||
97 | indent = "" |
||
98 | |||
99 | date_str = colorize( |
||
100 | self.date.strftime(self.journal.config["timeformat"]), |
||
101 | self.journal.config["colors"]["date"], |
||
102 | bold=True, |
||
103 | ) |
||
104 | |||
105 | if not short and self.journal.config["linewrap"]: |
||
106 | # Color date / title and bold title |
||
107 | title = ansiwrap.fill( |
||
108 | date_str |
||
109 | + " " |
||
110 | + highlight_tags_with_background_color( |
||
111 | self, |
||
112 | self.title, |
||
113 | self.journal.config["colors"]["title"], |
||
114 | is_title=True, |
||
115 | ), |
||
116 | self.journal.config["linewrap"], |
||
117 | ) |
||
118 | body = highlight_tags_with_background_color( |
||
119 | self, self.body.rstrip(" \n"), self.journal.config["colors"]["body"] |
||
120 | ) |
||
121 | body_text = [ |
||
122 | colorize( |
||
123 | ansiwrap.fill( |
||
124 | line, |
||
125 | self.journal.config["linewrap"], |
||
126 | initial_indent=indent, |
||
127 | subsequent_indent=indent, |
||
128 | drop_whitespace=True, |
||
129 | ), |
||
130 | self.journal.config["colors"]["body"], |
||
131 | ) |
||
132 | or indent |
||
133 | for line in body.rstrip(" \n").splitlines() |
||
134 | ] |
||
135 | |||
136 | # ansiwrap doesn't handle lines with only the "\n" character and some |
||
137 | # ANSI escapes properly, so we have this hack here to make sure the |
||
138 | # beginning of each line has the indent character and it's colored |
||
139 | # properly. textwrap doesn't have this issue, however, it doesn't wrap |
||
140 | # the strings properly as it counts ANSI escapes as literal characters. |
||
141 | # TL;DR: I'm sorry. |
||
142 | body = "\n".join( |
||
143 | [ |
||
144 | colorize(indent, self.journal.config["colors"]["body"]) + line |
||
145 | if not ansiwrap.strip_color(line).startswith(indent) |
||
146 | else line |
||
147 | for line in body_text |
||
148 | ] |
||
149 | ) |
||
150 | else: |
||
151 | title = ( |
||
152 | date_str |
||
153 | + " " |
||
154 | + highlight_tags_with_background_color( |
||
155 | self, |
||
156 | self.title.rstrip("\n"), |
||
157 | self.journal.config["colors"]["title"], |
||
158 | is_title=True, |
||
159 | ) |
||
160 | ) |
||
161 | body = highlight_tags_with_background_color( |
||
162 | self, self.body.rstrip("\n "), self.journal.config["colors"]["body"] |
||
163 | ) |
||
164 | |||
165 | # Suppress bodies that are just blanks and new lines. |
||
166 | has_body = len(self.body) > 20 or not all( |
||
167 | char in (" ", "\n") for char in self.body |
||
168 | ) |
||
169 | |||
170 | if short: |
||
171 | return title |
||
172 | else: |
||
173 | return "{title}{sep}{body}\n".format( |
||
174 | title=title, sep="\n" if has_body else "", body=body if has_body else "" |
||
175 | ) |
||
223 |