From be72f4f6932afe481aef3a460a33f4c694c127a1 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Thu, 15 Dec 2022 22:25:44 +0500 Subject: [PATCH] Add "merge" command (#1) --- histd.py | 35 +++++++++++++++++++++++++++++++++++ readme.md | 6 +++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/histd.py b/histd.py index 9218b00..ca29c4c 100755 --- a/histd.py +++ b/histd.py @@ -13,6 +13,8 @@ def main(): edit_note(base_dir, today) elif sys.argv[1] == "backup": backup(base_dir, today) + elif sys.argv[1] == "merge": + merge(base_dir) else: print('Command not found') @@ -69,5 +71,38 @@ def backup(base_dir: str, current_date): print("Archiver returned non-zero exit code") +def merge(base_dir: str): + """ + This function concatenates all files and prefixes each with the filename. + The result will be printed to stdout. + """ + + def read_files(path: str) -> str: + """ + Recursive function to read all files in a directory + """ + strings = [] + contents = os.listdir(path) + + for entry in contents: + entry_path = os.path.join(path, entry) + + # It's a directory + if os.path.isdir(entry_path): + # Read all files in this directory + res = read_files(entry_path) + strings.append(res) + # It's a file + else: + with open(entry_path, 'r') as note: + strings.append(f'## {entry_path}') + strings.append(note.read()) + + return '\n\n'.join(strings) + + res = read_files(base_dir) + print(res) + + if __name__ == '__main__': main() diff --git a/readme.md b/readme.md index d8dc2d6..2b600b1 100644 --- a/readme.md +++ b/readme.md @@ -40,7 +40,7 @@ histd backup ``` ## Merge all notes -This command concatenates all files and prefixes each with the creation date. -```sh -for f in ~/.local/share/histd/**/*.md; do echo $f | sed 's/^.*histd\//# /'; cat "${f}"; echo; done +This command concatenates all files and prefixes each with the filename. +```bash +histd merge ```