Command-line improvements
This commit is contained in:
parent
133cf044c4
commit
316a6b0d0c
41
bin/grep.py
41
bin/grep.py
|
@ -6,20 +6,37 @@ def get_app_class(template):
|
||||||
class App(template):
|
class App(template):
|
||||||
def F_main(self):
|
def F_main(self):
|
||||||
pattern = self.argv[1].encode("utf-8")
|
pattern = self.argv[1].encode("utf-8")
|
||||||
file = self.argv[2]
|
files = self.argv[2:]
|
||||||
|
|
||||||
|
if files:
|
||||||
|
prefix = len(files) > 1
|
||||||
|
for file in files:
|
||||||
|
try:
|
||||||
|
with open(file, "rb") as f:
|
||||||
|
for line in f.readlines():
|
||||||
|
if re.search(pattern, line):
|
||||||
|
self.console.write((f"{file}:".encode("utf-8") if prefix else b"") + line)
|
||||||
|
except OSError as e:
|
||||||
|
try:
|
||||||
|
os.listdir(file)
|
||||||
|
console.print(self.console, f"grep: {file}: Is a directory")
|
||||||
|
except OSError:
|
||||||
|
console.print(self.console, f"grep: {file}: No such file or directory")
|
||||||
|
self.exit()
|
||||||
|
else:
|
||||||
|
self.store["pattern"] = pattern
|
||||||
|
self.store["buffer"] = []
|
||||||
|
self.jump("loop")
|
||||||
|
|
||||||
|
|
||||||
|
def F_loop(self):
|
||||||
try:
|
try:
|
||||||
with open(file, "rb") as f:
|
line = console.read(self.console, self.store["buffer"])
|
||||||
for line in f.readlines():
|
except EOFError:
|
||||||
if re.search(pattern, line):
|
self.exit()
|
||||||
self.console.write(line)
|
return
|
||||||
except OSError as e:
|
|
||||||
try:
|
|
||||||
os.listdir(file)
|
|
||||||
console.print(self.console, f"grep: {file}: Is a directory")
|
|
||||||
except OSError:
|
|
||||||
console.print(self.console, f"grep: {file}: No such file or directory")
|
|
||||||
|
|
||||||
self.exit()
|
if line and re.search(self.store["pattern"], line):
|
||||||
|
self.console.write(line + b"\n")
|
||||||
|
|
||||||
return App
|
return App
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import shlex
|
import shlex
|
||||||
|
import console
|
||||||
|
|
||||||
def get_app_class(template):
|
def get_app_class(template):
|
||||||
class App(template):
|
class App(template):
|
||||||
|
@ -13,6 +14,11 @@ def get_app_class(template):
|
||||||
self.next_function = 'loop'
|
self.next_function = 'loop'
|
||||||
|
|
||||||
def F_loop(self):
|
def F_loop(self):
|
||||||
|
if len(self.os.apps) == 1:
|
||||||
|
console.print(self.console, "All processes have quit.")
|
||||||
|
self.jump("end")
|
||||||
|
|
||||||
|
def F_end(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return App
|
return App
|
||||||
|
|
|
@ -13,7 +13,12 @@ def get_app_class(template):
|
||||||
self.jump('input')
|
self.jump('input')
|
||||||
|
|
||||||
def F_input(self):
|
def F_input(self):
|
||||||
raw_line = console.read(self.console, self.store['buf'])
|
try:
|
||||||
|
raw_line = console.read(self.console, self.store['buf'])
|
||||||
|
except EOFError:
|
||||||
|
self.exit()
|
||||||
|
return
|
||||||
|
|
||||||
if raw_line is not None:
|
if raw_line is not None:
|
||||||
line = shlex.split(raw_line.decode('ascii'))
|
line = shlex.split(raw_line.decode('ascii'))
|
||||||
if line:
|
if line:
|
||||||
|
|
|
@ -10,6 +10,8 @@ def read(console, buffer):
|
||||||
data = b''.join(buffer)
|
data = b''.join(buffer)
|
||||||
buffer.clear()
|
buffer.clear()
|
||||||
return data
|
return data
|
||||||
|
elif char == b'\x04' and not buffer:
|
||||||
|
raise EOFError()
|
||||||
elif len(char) == 1:
|
elif len(char) == 1:
|
||||||
console.write(char)
|
console.write(char)
|
||||||
buffer.append(char)
|
buffer.append(char)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user