diff --git a/src/pyzet/zettel.py b/src/pyzet/zettel.py index ba24c54..c0cd7fc 100644 --- a/src/pyzet/zettel.py +++ b/src/pyzet/zettel.py @@ -64,27 +64,6 @@ def get_zettel(path: Path) -> Zettel: return Zettel(title=title, id_=id_, tags=tags, path=md_path) -def _get_first_and_last_line(path: Path) -> tuple[str, str]: - """Gets the first and the last line from a given file. - - It uses file.seek() to look from the end of the file. It's fast but - requires the file to be opened in binary mode. - - Reference: - https://stackoverflow.com/a/54278929/14458327 - """ - with open(path, 'rb') as file: - title_line = file.readline().decode('utf-8') - try: - file.seek(-2, os.SEEK_END) - while file.read(1) != b'\n': - file.seek(-2, os.SEEK_CUR) - except OSError: # file has only a single line - file.seek(0) - tags_line = file.readline().decode('utf-8') - return title_line, tags_line - - def get_zettel_repr(zet: Zettel, args: Namespace) -> str: tags = '' if args.tags: @@ -150,3 +129,24 @@ def get_tags_str(zettel: Zettel) -> str: raise ValueError else: return '#' + ' #'.join(zettel.tags) + + +def _get_first_and_last_line(path: Path) -> tuple[str, str]: + """Gets the first and the last line from a given file. + + It uses file.seek() to look from the end of the file. It's fast but + requires the file to be opened in binary mode. + + Reference: + https://stackoverflow.com/a/54278929/14458327 + """ + with open(path, 'rb') as file: + title_line = file.readline().decode('utf-8') + try: + file.seek(-2, os.SEEK_END) + while file.read(1) != b'\n': + file.seek(-2, os.SEEK_CUR) + except OSError: # file has only a single line + file.seek(0) + tags_line = file.readline().decode('utf-8') + return title_line, tags_line