diff --git a/.gitignore b/.gitignore index 3c3629e64..3e5cc695e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.venv/ \ No newline at end of file diff --git a/implement-shell-tools/cat/custom_cat.py b/implement-shell-tools/cat/custom_cat.py new file mode 100644 index 000000000..29220665c --- /dev/null +++ b/implement-shell-tools/cat/custom_cat.py @@ -0,0 +1,30 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="custom cat in python", + description="trying to match behaviour in actual cat" +) + +parser.add_argument("-b", "--non_blank", action="store_true", help="add number at the beginning of non blank line") +parser.add_argument("-n", "--number", action="store_true", help="add number at the beginning of each line") +parser.add_argument("file_path", nargs="+", help="file path to process") + +args = parser.parse_args() + +count = 1 + +for path in args.file_path: + with open(path, "r") as file: + context = file.read().rstrip().split("\n") + for line in context: + if (args.non_blank): + if (len(line) != 0): + print(f'{str(count).rjust(6, " ")} {line}') + count += 1 + else: + print(line) + elif (args.number): + print(f'{str(count).rjust(6, " ")} {line}') + count += 1 + else: + print(line) \ No newline at end of file diff --git a/implement-shell-tools/ls/custom_ls.py b/implement-shell-tools/ls/custom_ls.py new file mode 100644 index 000000000..8d0d77a1a --- /dev/null +++ b/implement-shell-tools/ls/custom_ls.py @@ -0,0 +1,32 @@ +import argparse +import os +import re + +def sort_list(file_list): + return sorted(file_list, key=lambda file_name: re.sub(r"^\.", "", file_name).lower()) + +def format_list(file_list): + return [f"\033[1;34m{file}\033[0m" if os.path.isdir(os.path.join(args.path, file)) else file for file in file_list] + +parser = argparse.ArgumentParser( + prog="custom ls in python", + description="trying to match behaviour as the actual ls" +) + +parser.add_argument("-a", "--show_hidden", action="store_true", help="show hidden files") +parser.add_argument("-1", "--one_item", action="store_true", help="show one item per one line") +parser.add_argument("path", help="directory path to process", nargs="?", default=".") + +args = parser.parse_args() + +files_array = os.listdir(args.path) + +sorted_files_array = sort_list(files_array) + +hidden_switched_list = [".", "..", *sorted_files_array] if args.show_hidden else [file for file in sorted_files_array if re.match(r"^(?!\.)", file)] + +formatted_list = format_list(hidden_switched_list) + +separator = "\n" if args.one_item else " " + +print(separator.join(formatted_list)) diff --git a/implement-shell-tools/wc/custom_wc.py b/implement-shell-tools/wc/custom_wc.py new file mode 100644 index 000000000..a826e7925 --- /dev/null +++ b/implement-shell-tools/wc/custom_wc.py @@ -0,0 +1,66 @@ +import argparse +import re + +def format_number(number_array): + space_array = [3, 4, 4] + result = map(lambda number, space: str(number).rjust(space), number_array, space_array) + return "".join(list(result)) + +parser = argparse.ArgumentParser( + prog="custom wc in python", + description="trying to match behaviour in the actual wc" +) + +parser.add_argument("-l", "--lines", action="store_true", help="count how many lines") +parser.add_argument("-w", "--words", action="store_true", help="count how many words") +parser.add_argument("-c", "--characters", action="store_true", help="count how many characters") +parser.add_argument("path", nargs="+", help="path to process") + +args = parser.parse_args() + +files_array = args.path + +total_lines = 0 +total_words = 0 +total_characters = 0 + +total_numbers_row_array = [] + +for file_path in files_array: + with open(file_path) as file: + context = file.read() + lines = len(context.split("\n")) - 1 + words = len(re.findall(r"\S+", context)) + characters = len(context) + + numbers_row_array = [] + if (args.lines): + numbers_row_array.append(lines) + if (args.words): + numbers_row_array.append(words) + if (args.characters): + numbers_row_array.append(characters) + + if (len(numbers_row_array) == 1 and len(files_array) == 1): + print(f'{numbers_row_array[0]} {file_path}') + + elif (len(numbers_row_array) > 0): + print(f'{format_number(numbers_row_array)} {file_path}') + else: + print(format_number([lines, words, characters]), file_path) + + total_lines += lines + total_words += words + total_characters += characters + +if (len(files_array) > 1): + if (args.lines): + total_numbers_row_array.append(total_lines) + if (args.words): + total_numbers_row_array.append(total_words) + if (args.characters): + total_numbers_row_array.append(total_characters) + if (len(total_numbers_row_array) > 0): + print(f'{format_number(total_numbers_row_array)} total') + else: + print(format_number([total_lines, total_words, total_characters]), 'total') \ No newline at end of file