show-newlines.py 903 B

1234567891011121314151617181920212223242526272829303132
  1. import autocommand
  2. import inflect
  3. from more_itertools import always_iterable
  4. import jaraco.text
  5. def report_newlines(filename):
  6. r"""
  7. Report the newlines in the indicated file.
  8. >>> tmp_path = getfixture('tmp_path')
  9. >>> filename = tmp_path / 'out.txt'
  10. >>> _ = filename.write_text('foo\nbar\n', newline='', encoding='utf-8')
  11. >>> report_newlines(filename)
  12. newline is '\n'
  13. >>> filename = tmp_path / 'out.txt'
  14. >>> _ = filename.write_text('foo\nbar\r\n', newline='', encoding='utf-8')
  15. >>> report_newlines(filename)
  16. newlines are ('\n', '\r\n')
  17. """
  18. newlines = jaraco.text.read_newlines(filename)
  19. count = len(tuple(always_iterable(newlines)))
  20. engine = inflect.engine()
  21. print(
  22. engine.plural_noun("newline", count),
  23. engine.plural_verb("is", count),
  24. repr(newlines),
  25. )
  26. autocommand.autocommand(__name__)(report_newlines)