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
41 changes: 41 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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:
lines = f.readlines()

for line in lines:
line = line.rstrip("\n")

if show_b:
if line == "":
print()
else:
numbered = str(line_number).rjust(6) + "\t" + line
print(numbered)
line_number += 1

elif show_n:
numbered = str(line_number).rjust(6) + "\t" + line
print(numbered)
line_number += 1

else:
print(line)
57 changes: 57 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import argparse
import os

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="List one file per line"
)

parser.add_argument(
"-a",
action="store_true",
help="Show all files"
)

args = parser.parse_args()

paths = args.paths

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

for target in paths:

if os.path.isdir(target):
show_files = os.listdir(target)

if args.a:
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 args.one:
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