Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.venv/
33 changes: 33 additions & 0 deletions implement-shell-tools/cat/custom_cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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:

should_number_line = False;

if (args.non_blank):
if (len(line) != 0):
should_number_line = True
elif (args.number):
should_number_line = True

if (should_number_line):
print(f'{str(count).rjust(6, " ")} {line}')
count += 1
else:
print(line)
32 changes: 32 additions & 0 deletions implement-shell-tools/ls/custom_ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import argparse
import os
import re

def sort_list(file_list: list[str]) -> list[str]:
return sorted(file_list, key=lambda file_name: re.sub(r"^\.", "", file_name).lower())

def format_list(file_list: list[str]) -> list[str]:
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))
66 changes: 66 additions & 0 deletions implement-shell-tools/wc/custom_wc.py
Original file line number Diff line number Diff line change
@@ -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')