Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import argparse

parser = argparse.ArgumentParser(
prog="check-for-cat",
description="Implement my own version of cat in python",
)

parser.add_argument("paths", nargs="+", help="The file paths to process")
parser.add_argument("-n", action="store_true", help="Number lines")
parser.add_argument("-b", action="store_true", help="Number non-blank lines")

args = parser.parse_args()

paths = args.paths
show_n = args.n
show_b = args.b

line_number = 1

for path in paths:
with open(path, "r", encoding="utf-8") as f:
content = f.read()

ends_with_newline = content.endswith("\n")
lines = content.split("\n")
if ends_with_newline:
lines.pop()

new_lines = []
for line in lines:
if show_b:
if line == "":
new_lines.append(line)
else:
numbered = str(line_number).rjust(6, " ") + "\t" + line
line_number += 1
new_lines.append(numbered)
elif show_n:
numbered = str(line_number).rjust(6, " ") + "\t" + line
line_number += 1
new_lines.append(numbered)
else:
new_lines.append(line)
lines = new_lines

output = "\n".join(lines)
if ends_with_newline:
output += "\n"
print(output, end="")
43 changes: 43 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import argparse
import os
import stat

parser = argparse.ArgumentParser(
prog="check-for-ls",
description="Implement my own version of ls",
)

parser.add_argument("paths", nargs="*", help="The file paths to process")
parser.add_argument("-1", "--one", action="store_true", help="This lists one file per line")
parser.add_argument("-a", action="store_true", help="This shows all files")

args = parser.parse_args()

show_one_line = args.one
show_all_files = args.a

paths = args.paths
file_path = paths
if len(file_path) == 0:
file_path = ["."]

for target in file_path:
info = os.stat(target)

if stat.S_ISDIR(info.st_mode):
show_files = os.listdir(target)

if show_all_files:
show_files = [".", "..", *show_files]
else:
show_files = [name for name in show_files if not name.startswith(".")]

show_files.sort()
else:
show_files = [target]

if show_one_line:
for file in show_files:
print(file)
else:
print(" ".join(show_files))
70 changes: 70 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import argparse
import re

parser = argparse.ArgumentParser(
prog="check-for-wc",
description="Implement my own version of wc",
)

parser.add_argument("paths", nargs="+", help="The file paths to process")
parser.add_argument("-l", action="store_true", help="Counts the total number of lines")
parser.add_argument("-c", action="store_true", help="Counts the total number of characters")
parser.add_argument("-w", action="store_true", help="Counts the total number of words")

args = parser.parse_args()

paths = args.paths
show_lines = args.l
show_words = args.w
show_char = args.c

no_flags_given = not show_lines and not show_words and not show_char

columns = []

for path in paths:
with open(path, "r", encoding="utf-8") as f:
content = f.read()

line_count = content.count("\n")

word_count = len([word for word in re.split(r"\s+", content) if word != ""])

char_count = len(content.encode("utf-8"))

columns.append({
"path": path,
"lines": line_count,
"words": word_count,
"char": char_count,
})

if len(columns) > 1:
total_lines = 0
total_words = 0
total_char = 0

for result in columns:
total_lines += result["lines"]
total_words += result["words"]
total_char += result["char"]

columns.append({
"path": "total",
"lines": total_lines,
"words": total_words,
"char": total_char,
})

for result in columns:
line = ""
if no_flags_given or show_lines:
line += str(result["lines"]).rjust(6, " ")
if no_flags_given or show_words:
line += str(result["words"]).rjust(6, " ")
if no_flags_given or show_char:
line += str(result["char"]).rjust(6, " ")

line += " " + result["path"]

print(line)
Loading