WIP command wrapper. Kind of works....but also janky. Not sure how to make it better :/
This commit is contained in:
parent
865f66b79d
commit
82b4d8de7c
5 changed files with 68 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
for i in range(0, 5):
|
for i in range(0, 10000):
|
||||||
print("\033[91mHello World!")
|
print(f"\033[{(i % 7) + 30}mHello World!")
|
||||||
|
print(f"\033[{(i % 7) + 90}mHello World!")
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
# You should have received a copy of the GNU General Public License along with
|
# You should have received a copy of the GNU General Public License along with
|
||||||
# SSHare. If not, see <https://www.gnu.org/licenses/>.
|
# SSHare. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
|
||||||
class NoDefault: pass
|
class NoDefault: pass
|
||||||
|
|
||||||
def Flag(name=None, help=None):
|
def Flag(name=None, help=None):
|
||||||
|
@ -33,6 +35,8 @@ class Argument:
|
||||||
self._short = None
|
self._short = None
|
||||||
self._long = name
|
self._long = name
|
||||||
|
|
||||||
|
self._is_remainder = kwargs.get("nargs", None) == argparse.REMAINDER
|
||||||
|
|
||||||
if not "default" in kwargs:
|
if not "default" in kwargs:
|
||||||
kwargs["default"] = NoDefault
|
kwargs["default"] = NoDefault
|
||||||
kwargs["default"] = _None(kwargs["default"])
|
kwargs["default"] = _None(kwargs["default"])
|
||||||
|
@ -43,6 +47,8 @@ class Argument:
|
||||||
if self._long == None:
|
if self._long == None:
|
||||||
self._long = argument
|
self._long = argument
|
||||||
self._kwargs["metavar"] = argument
|
self._kwargs["metavar"] = argument
|
||||||
|
if self._is_remainder:
|
||||||
|
self._long = self.dest()
|
||||||
|
|
||||||
def set_flags(self, short, long):
|
def set_flags(self, short, long):
|
||||||
if short != None:
|
if short != None:
|
||||||
|
@ -75,11 +81,13 @@ class Argument:
|
||||||
|
|
||||||
def add(self, arg_parser):
|
def add(self, arg_parser):
|
||||||
flags = []
|
flags = []
|
||||||
|
kwargs = self._kwargs
|
||||||
|
if self._is_remainder:
|
||||||
|
flags.append(self._long)
|
||||||
|
else:
|
||||||
if self._short != None: flags.append(f"-{self._short}")
|
if self._short != None: flags.append(f"-{self._short}")
|
||||||
if self._long != None: flags.append(f"--{self._long}")
|
if self._long != None: flags.append(f"--{self._long}")
|
||||||
kwargs = self._kwargs | {
|
kwargs["dest"] = self.dest()
|
||||||
"dest": self.dest()
|
|
||||||
}
|
|
||||||
arg_parser.add_argument(
|
arg_parser.add_argument(
|
||||||
*flags,
|
*flags,
|
||||||
**kwargs
|
**kwargs
|
||||||
|
|
|
@ -35,6 +35,7 @@ class PluginLoader:
|
||||||
in ([ "command_line" ] if command_line else []) + [
|
in ([ "command_line" ] if command_line else []) + [
|
||||||
"file",
|
"file",
|
||||||
"stdin",
|
"stdin",
|
||||||
|
"wrap_command",
|
||||||
"current_time",
|
"current_time",
|
||||||
"append_type",
|
"append_type",
|
||||||
"ssh",
|
"ssh",
|
||||||
|
|
|
@ -21,7 +21,7 @@ plugin_type = "source"
|
||||||
|
|
||||||
activate = { "stdin" }
|
activate = { "stdin" }
|
||||||
config = {
|
config = {
|
||||||
"suffix": "txt"
|
"suffix": "txt",
|
||||||
}
|
}
|
||||||
args = {
|
args = {
|
||||||
"stdin": Flag(help="Upload from stdin")
|
"stdin": Flag(help="Upload from stdin")
|
||||||
|
|
50
src/sshare/plugins/wrap_command.py
Normal file
50
src/sshare/plugins/wrap_command.py
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
# This file is part of SSHare.
|
||||||
|
#
|
||||||
|
# SSHare is free software: you can redistribute it and/or modify it under the terms of
|
||||||
|
# the GNU General Public License as published by the Free Software Foundation,
|
||||||
|
# either version 3 of the License, or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# SSHare is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
# more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with
|
||||||
|
# SSHare. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import locale
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from sshare.plugin.config import Argument
|
||||||
|
from sshare.plugin.source import Raw
|
||||||
|
|
||||||
|
plugin_type = "source"
|
||||||
|
|
||||||
|
activate = { "command" }
|
||||||
|
config = {
|
||||||
|
"suffix": "txt",
|
||||||
|
}
|
||||||
|
args = {
|
||||||
|
"command": Argument(
|
||||||
|
nargs=argparse.REMAINDER,
|
||||||
|
help="Upload the contents of the wrapped command",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
def init():
|
||||||
|
config.command = config.command[1:]
|
||||||
|
|
||||||
|
def get_source():
|
||||||
|
output = b""
|
||||||
|
with subprocess.Popen(
|
||||||
|
config.command,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.STDOUT,
|
||||||
|
bufsize=0,
|
||||||
|
) as process:
|
||||||
|
for line in process.stdout:
|
||||||
|
print(line.decode(locale.getpreferredencoding()), end="")
|
||||||
|
output += line
|
||||||
|
|
||||||
|
return Raw(config.suffix, output)
|
Loading…
Reference in a new issue