Skip to content

Commit

Permalink
Add possibility to add sections to cheatsheets
Browse files Browse the repository at this point in the history
  • Loading branch information
martialblog committed Jan 1, 2018
1 parent c662502 commit 8f29896
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
26 changes: 18 additions & 8 deletions cheat/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ def printcheats(self, template):
:param template: Template to use with the format() function.
"""

for description in self.configparser['cheats']:
value = self.configparser['cheats'][description]
value = self.add_color(value) if self.print_colored else value
output = template.format(description.capitalize(), value)
sections = self.configparser.sections()
sections.remove('main')

print(output)
for section in sections:
print(section.upper())
for description in self.configparser[section]:
value = self.configparser[section][description]
value = self.add_color(value) if self.print_colored else value
output = template.format(description.capitalize(), value)
print(output)


class InlinePrinter(Printer):
Expand All @@ -71,7 +75,13 @@ def width(self):
Width of the longest ConfigParser entry.
"""

width = len(max(self.configparser['cheats'], key=len))
width = 1

# Calculate new width
for section in self.configparser.sections():
longest_width = len(max(self.configparser[section], key=len))
if longest_width > width:
width = longest_width

return str(width)

Expand Down Expand Up @@ -104,8 +114,8 @@ class PrinterFactory:
"""

printer_classes = {
"InlinePrinter": InlinePrinter,
"BreaklinePrinter": BreaklinePrinter
'InlinePrinter': InlinePrinter,
'BreaklinePrinter': BreaklinePrinter
}

@staticmethod
Expand Down
8 changes: 8 additions & 0 deletions cheatsheets/example.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[main]
name=example
desc=An example for start your own
[common]
Simple Example = example foobar
Example with Parameter = example foo <PARAMETER>
[workflow]
Example in another section = example barfoo
16 changes: 8 additions & 8 deletions tests/test_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ def test_InlinePrinter_colored(self):
"""

# Done this way for better readablility.
lines = ["Test cheat a \x1b[94mlorem\x1b[1;m\n",
lines = ["CHEATS\n", "Test cheat a \x1b[94mlorem\x1b[1;m\n",
"Test cheat b \x1b[94mipsum\x1b[1;m\n",
"Test cheat c \x1b[94mdolor\x1b[1;m\n"]

expected_output = lines[0] + lines[1] + lines[2]
expected_output = ''.join(lines)

printer = cp.InlinePrinter(self.cparser, u.Colors, print_colored=True)

Expand All @@ -81,11 +81,11 @@ def Test_InlinePrinter(self):
"""

# Done this way for better readablility.
lines = ["Test cheat a lorem\n",
lines = ["CHEATS\n", "Test cheat a lorem\n",
"Test cheat b ipsum\n",
"Test cheat c dolor\n"]

expected_output = lines[0] + lines[1] + lines[2]
expected_output = ''.join(lines)

printer = cp.InlinePrinter(self.cparser, u.Colors, print_colored=False)

Expand All @@ -109,11 +109,11 @@ def test_BreaklinePrinter(self):
"""

# Done this way for better readablility.
lines = ["Test cheat a \n lorem\n",
lines = ["CHEATS\n", "Test cheat a \n lorem\n",
"Test cheat b \n ipsum\n",
"Test cheat c \n dolor\n"]

expected_output = lines[0] + lines[1] + lines[2]
expected_output = ''.join(lines)

printer = cp.BreaklinePrinter(self.cparser, u.Colors, print_colored=False)

Expand All @@ -127,11 +127,11 @@ def test_Printer_printsheet(self):
"""

# Done this way for better readablility.
lines = ["Test cheat a\n",
lines = ["CHEATS\n", "Test cheat a\n",
"Test cheat b\n",
"Test cheat c\n"]

expected_output = lines[0] + lines[1] + lines[2]
expected_output = ''.join(lines)

printer = cp.Printer(self.cparser, u.Colors)
template = "{0}"
Expand Down

0 comments on commit 8f29896

Please sign in to comment.