Skip to content

Commit

Permalink
Merge pull request #37 from cs50/develop
Browse files Browse the repository at this point in the history
fixes \n bug, adds whitespace
  • Loading branch information
dmalan authored Oct 12, 2017
2 parents efdc350 + 514559e commit d2e60a4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
"console_scripts": ["style50=style50.__main__:main"],
},
url="https://github.com/cs50/style50",
version="2.1.6"
version="2.2.0"
)
3 changes: 2 additions & 1 deletion style50/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class C(StyleCheck):
"--pad-header", "--pad-oper", "--max-code-length=100",
"--convert-tabs", "--indent=spaces=4",
"--indent-continuation=1", "--indent-switches",
"--min-conditional-indent=1", "--style=allman"
"--lineend=linux", "--min-conditional-indent=1",
"--style=allman"
]

# Match (1) /**/ comments, and (2) // comments.
Expand Down
51 changes: 29 additions & 22 deletions style50/style50.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def __init__(self, paths, output="character"):
elif output == "json":
self.run = self.run_json
else:
self._warn_chars = set()
self.run = self.run_diff
# Set diff function as needed
if output == "character":
Expand All @@ -91,16 +92,8 @@ def run_diff(self):
header, footer = (termcolor.colored("{0}\n{{}}\n{0}\n".format(
":" * 14), "cyan"), "\n") if len(files) > 1 else ("", "")

first = True
for file in files:
# Only print footer after first file has been printed
if first:
first = False
else:
print(footer, end="")

print(header.format(file), end="")

try:
results = self._check(file)
except Error as e:
Expand All @@ -109,13 +102,26 @@ def run_diff(self):

# Display results
if results.diffs:
print()
print(*self.diff(results.original, results.styled), sep="\n")
if results.comment_ratio < results.COMMENT_MIN:
termcolor.cprint("And consider adding more comments!", "yellow")
print()
conjunction = "And"
else:
termcolor.cprint("Looks good!", "green")
if results.comment_ratio < results.COMMENT_MIN:
termcolor.cprint("But consider adding more comments!", "yellow")
conjunction = "But"

if results.diffs:
for type, c in sorted(self._warn_chars):
color, verb = ("on_green", "insert") if type == "+" else ("on_red", "delete")
termcolor.cprint(c, None, color, end="")
termcolor.cprint(" means that you should {} a {}.".format(
verb, "newline" if c == "\\n" else "tab"), "yellow")

if results.comment_ratio < results.COMMENT_MIN:
termcolor.cprint("{} consider adding more comments!".format(conjunction), "yellow")

if (results.comment_ratio < results.COMMENT_MIN or self._warn_chars) and results.diffs:
print()

def run_json(self):
"""
Expand Down Expand Up @@ -166,18 +172,16 @@ def _check(self, file):
Run apropriate check based on `file`'s extension and return it,
otherwise raise an Error
"""

if not os.path.exists(file):
raise Error("file \"{}\" not found".format(file))

_, extension = os.path.splitext(file)
try:
check = self.extension_map[extension[1:]]

with open(file) as f:
code = f.read()

except (OSError, IOError) as e:
if e.errno == errno.ENOENT:
raise Error("file \"{}\" not found".format(file))
else:
raise
except KeyError:
raise Error("unknown file type \"{}\", skipping...".format(file))
else:
Expand Down Expand Up @@ -229,8 +233,7 @@ def color_transition(old_type, new_type):

return self._char_diff(old, new, color_transition)

@staticmethod
def _char_diff(old, new, transition, fmt=lambda c: c):
def _char_diff(self, old, new, transition, fmt=lambda c: c):
"""
Returns a char-based diff between `old` and `new` where each character
is formatted by `fmt` and transitions between blocks are determined by `transition`.
Expand All @@ -255,13 +258,17 @@ def _char_diff(old, new, transition, fmt=lambda c: c):

if d[2] == "\n":
if dtype != " ":
self._warn_chars.add((dtype, "\\n"))
# Show added/removed newlines.
line += [fmt(r"\n"), transition(dtype, " ")]
yield "".join(line)
line = [transition(" ", dtype)]
else:
elif dtype != " " and d[2] == "\t":
# Show added/removed tabs.
line.append(fmt(d[2] if dtype == " " else d[2].replace("\t", r"\t")))
line.append(fmt("\\t"))
self._warn_chars.add((dtype, "\\t"))
else:
line.append(fmt(d[2]))

# Flush buffer before quitting.
last = "".join(line)
Expand Down

0 comments on commit d2e60a4

Please sign in to comment.