Skip to content
Open
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
8 changes: 5 additions & 3 deletions nanocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@


def read(args):
lines = open(args["path"]).readlines()
with open(args["path"], encoding="utf-8", errors="replace") as f:
lines = f.readlines()
offset = args.get("offset", 0)
limit = args.get("limit", len(lines))
selected = lines[offset : offset + limit]
Expand All @@ -36,7 +37,8 @@ def write(args):


def edit(args):
text = open(args["path"]).read()
with open(args["path"], encoding="utf-8") as f:
text = f.read()
old, new = args["old"], args["new"]
if old not in text:
return "error: old_string not found"
Expand Down Expand Up @@ -67,7 +69,7 @@ def grep(args):
hits = []
for filepath in globlib.glob(args.get("path", ".") + "/**", recursive=True):
try:
for line_num, line in enumerate(open(filepath), 1):
for line_num, line in enumerate(open(filepath, encoding="utf-8", errors="replace"), 1):
if pattern.search(line):
hits.append(f"{filepath}:{line_num}:{line.rstrip()}")
except Exception:
Expand Down