Browse Source

fix: read from stdout and write to stdout

master
OhYee 7 months ago
parent
commit
fd199ffd41
  1. 2
      addon/base.py
  2. 2
      addon/colorful.py
  3. 6
      addon/json_addon.py
  4. 12
      addon/store.py
  5. 10
      addon/watch.py
  6. 4
      main.py
  7. 2
      utils/__init__.py
  8. 10
      utils/io.py

2
addon/base.py

@ -11,7 +11,7 @@ class Addon: @@ -11,7 +11,7 @@ class Addon:
return "no help"
def run(self, params):
print(params)
stdoutLine(params)
def __repr__(self):
return "Addon[%s] %s" % (self.name(), self.description())

2
addon/colorful.py

@ -113,7 +113,7 @@ class Colorful(Addon): @@ -113,7 +113,7 @@ class Colorful(Addon):
if ctx.help:
# flags.showHelp()
print("\n".join(
stdoutLine("\n".join(
[
self.description(),
"",

6
addon/json_addon.py

@ -56,7 +56,7 @@ class JSON(Addon): @@ -56,7 +56,7 @@ class JSON(Addon):
if ctx.help:
flags.showHelp()
text = input()
text = stdin()
try:
jsonObj = json.loads(text)
jsonText = json.dumps(
@ -80,10 +80,10 @@ class JSON(Addon): @@ -80,10 +80,10 @@ class JSON(Addon):
jsonText,
)
print(jsonText)
stdoutLine(jsonText)
except Exception as e:
stderrLine(e)
print(text)
stdoutLine(text)
def parse(self, repl, text):
return text if re.match(repl, text) != None else None

12
addon/store.py

@ -34,32 +34,32 @@ class Store(Addon): @@ -34,32 +34,32 @@ class Store(Addon):
def run(self, params):
if ctx.help:
print(
stdoutLine(
"o store {write|read} {key} [value]\n " +
self.description()
)
exit(0)
if len(params) < 1:
print("error: missing command, want write or read")
stdoutLine("error: missing command, want write or read")
exit(1)
cmd = params[0]
if len(params) < 2:
print("error: missing key")
stdoutLine("error: missing key")
exit(1)
key = params[1]
if cmd in ["write", "w"]:
if len(params) < 3:
print("error: missing value")
stdoutLine("error: missing value")
exit(1)
value = params[2]
kvStore(key, value)
elif cmd in ["read", "r"]:
print(kvLoad(key, ""))
stdoutLine(kvLoad(key, ""))
else:
print("Unknown command " + cmd)
stdoutLine("Unknown command " + cmd)
addons.append(Store)

10
addon/watch.py

@ -89,12 +89,12 @@ class Watch(Addon): @@ -89,12 +89,12 @@ class Watch(Addon):
if len(slice[idx]) > number:
slice[idx] = slice[idx][-number:]
print("\033[H\033[2J")
stdoutLine("\033[H\033[2J")
for idx in range(l):
print(commands[idx])
stdoutLine(commands[idx])
if not no_history:
print(slice[idx])
print(
stdoutLine(slice[idx])
stdoutLine(
"Min:", float_fmt % min(slice[idx]),
"Max:", float_fmt % max(slice[idx]),
"Sum:", float_fmt % sum(slice[idx]),
@ -102,7 +102,7 @@ class Watch(Addon): @@ -102,7 +102,7 @@ class Watch(Addon):
sum(slice[idx]) / len(slice[idx])
)
)
print()
stdoutLine()
time.sleep(sleep_time)

4
main.py

@ -63,9 +63,9 @@ if __name__ == "__main__": @@ -63,9 +63,9 @@ if __name__ == "__main__":
except KeyboardInterrupt:
pass
exit(0)
print("Unknown command: %s" % command)
stdoutLine("Unknown command: %s" % command)
print("\n".join([
stdoutLine("\n".join([
flags.help(),
"",
"commands:",

2
utils/__init__.py

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
# -*- coding:utf-8 -*-
from .context import ctx
from .output import debug, stdout, stderr, stdoutLine, stderrLine
from .io import debug, stdout, stderr, stdoutLine, stderrLine, stdin, stdinLine
from .flags import Flags, align
from .i18n import I18n
from .store import kvLoad, kvStore

10
utils/output.py → utils/io.py

@ -6,7 +6,7 @@ import sys @@ -6,7 +6,7 @@ import sys
def debug(*args):
if ctx.debug:
print(" ".join(map(str, args)))
stdoutLine(" ".join(map(str, args)))
def stdout(*args):
@ -29,3 +29,11 @@ def stderrLine(*args): @@ -29,3 +29,11 @@ def stderrLine(*args):
stderr(*args)
stderr("\n")
sys.stderr.flush()
def stdin():
return sys.stdin.read()
def stdinLine():
return sys.stdin.readline()
Loading…
Cancel
Save