feat: more operators
This commit is contained in:
parent
20d0424930
commit
d35d0df021
2 changed files with 94 additions and 16 deletions
|
|
@ -8,6 +8,7 @@ dependencies = [
|
||||||
"argparse>=1.4.0",
|
"argparse>=1.4.0",
|
||||||
"configparser>=7.2.0",
|
"configparser>=7.2.0",
|
||||||
"datetime>=6.0",
|
"datetime>=6.0",
|
||||||
|
"rich>=14.3.3",
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.scripts]
|
[project.scripts]
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,10 @@ import locale
|
||||||
import logging
|
import logging
|
||||||
import argparse
|
import argparse
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
|
from rich.panel import Panel
|
||||||
|
from rich import print
|
||||||
|
|
||||||
#
|
#
|
||||||
# LIBRARY
|
# LIBRARY
|
||||||
|
|
@ -33,6 +36,19 @@ class consume:
|
||||||
def __call__(self, stream):
|
def __call__(self, stream):
|
||||||
return stream.readlines()
|
return stream.readlines()
|
||||||
|
|
||||||
|
class paragraphs(Consume):
|
||||||
|
def __call__(self, stream):
|
||||||
|
pars = []
|
||||||
|
current = ""
|
||||||
|
for item in stream.readlines():
|
||||||
|
# Not counting spaces as legit content.
|
||||||
|
if item.strip():
|
||||||
|
current += item
|
||||||
|
else:
|
||||||
|
pars.append( current )
|
||||||
|
current = ""
|
||||||
|
return pars
|
||||||
|
|
||||||
class format:
|
class format:
|
||||||
class Format:
|
class Format:
|
||||||
def __call__(self, items):
|
def __call__(self, items):
|
||||||
|
|
@ -43,22 +59,23 @@ class format:
|
||||||
return items
|
return items
|
||||||
|
|
||||||
class trim(Format):
|
class trim(Format):
|
||||||
def __init__(self, max = "14"):
|
# Every argument is a string.
|
||||||
|
def __init__(self, max = "140"):
|
||||||
self.max = int(max)
|
self.max = int(max)
|
||||||
|
|
||||||
def trim(self, line):
|
def trim(self, item):
|
||||||
if len(line) > self.max:
|
if len(item) > self.max:
|
||||||
yield line[:self.max]
|
yield item[:self.max]
|
||||||
for subline in self.trim(line[self.max:]):
|
for subitem in self.trim(item[self.max:]):
|
||||||
yield subline
|
yield subitem
|
||||||
else:
|
else:
|
||||||
yield line
|
yield item
|
||||||
|
|
||||||
def __call__(self, items):
|
def __call__(self, items):
|
||||||
trimmed = []
|
trimmed = []
|
||||||
for item in items:
|
for item in items:
|
||||||
for subline in self.trim(item):
|
for separated in self.trim(item):
|
||||||
trimmed.append(subline)
|
trimmed.append(separated)
|
||||||
return trimmed
|
return trimmed
|
||||||
|
|
||||||
class eol(Format):
|
class eol(Format):
|
||||||
|
|
@ -75,6 +92,63 @@ class format:
|
||||||
stripped.append( item.strip() )
|
stripped.append( item.strip() )
|
||||||
return stripped
|
return stripped
|
||||||
|
|
||||||
|
class skip(Format):
|
||||||
|
def __call__(self, items):
|
||||||
|
res = []
|
||||||
|
for item in items:
|
||||||
|
if item.strip():
|
||||||
|
res.append( item )
|
||||||
|
return res
|
||||||
|
|
||||||
|
class paragraph(Format):
|
||||||
|
def __call__(self, items):
|
||||||
|
glued = []
|
||||||
|
current = ""
|
||||||
|
for item in items:
|
||||||
|
if item.strip():
|
||||||
|
current += item
|
||||||
|
else:
|
||||||
|
glued.append( current )
|
||||||
|
current = ""
|
||||||
|
return glued
|
||||||
|
|
||||||
|
class panel(Format):
|
||||||
|
def __call__(self, items):
|
||||||
|
panel = []
|
||||||
|
for item in items:
|
||||||
|
panel.append( Panel.fit(item.strip()) )
|
||||||
|
return panel
|
||||||
|
|
||||||
|
class count(Format):
|
||||||
|
def __init__(self, end = ' ␄', sep = '\n'):
|
||||||
|
self.sep = sep
|
||||||
|
self.end = end
|
||||||
|
|
||||||
|
def __call__(self, items):
|
||||||
|
total = len(items)
|
||||||
|
res = []
|
||||||
|
for i,item in enumerate(items):
|
||||||
|
res.append(f"{item}{self.sep}{i+1}/{total}")
|
||||||
|
if res:
|
||||||
|
res[-1] += self.end
|
||||||
|
return res
|
||||||
|
|
||||||
|
class suffix(Format):
|
||||||
|
def __init__(self, content = "", sep = '\n'):
|
||||||
|
self.content = content
|
||||||
|
self.sep = sep
|
||||||
|
|
||||||
|
def __call__(self, items):
|
||||||
|
return [i+self.sep+self.content for i in items]
|
||||||
|
|
||||||
|
class prefix(Format):
|
||||||
|
def __init__(self, content = "", sep = '\n'):
|
||||||
|
self.content = content
|
||||||
|
self.sep = sep
|
||||||
|
|
||||||
|
def __call__(self, items):
|
||||||
|
return [self.content+self.sep+i for i in items]
|
||||||
|
|
||||||
|
|
||||||
class lift:
|
class lift:
|
||||||
class Lift:
|
class Lift:
|
||||||
|
|
@ -84,10 +158,13 @@ class lift:
|
||||||
class stdout(Lift):
|
class stdout(Lift):
|
||||||
def __call__(self, items):
|
def __call__(self, items):
|
||||||
for item in items:
|
for item in items:
|
||||||
|
if item:
|
||||||
if __debug__:
|
if __debug__:
|
||||||
print(item, end = '', file = sys.stdout, flush = True)
|
print(item, end = '', file = sys.stdout, flush = True)
|
||||||
else:
|
else:
|
||||||
print(item, end = '')
|
print(item, end = '')
|
||||||
|
else:
|
||||||
|
logger.debug("Empty item")
|
||||||
|
|
||||||
|
|
||||||
class Forthlifter:
|
class Forthlifter:
|
||||||
|
|
@ -97,14 +174,14 @@ class Forthlifter:
|
||||||
formatters = [format.as_is()],
|
formatters = [format.as_is()],
|
||||||
lifters = [lift.stdout()],
|
lifters = [lift.stdout()],
|
||||||
):
|
):
|
||||||
self.consumer = consumer
|
|
||||||
logger.debug(f"├ consumer: {type(consumer).__name__}")
|
|
||||||
|
|
||||||
if not isinstance(streamers, list):
|
if not isinstance(streamers, list):
|
||||||
streamers = [streamers]
|
streamers = [streamers]
|
||||||
self.streamers = streamers
|
self.streamers = streamers
|
||||||
logger.debug(f"├ streamers: {', '.join(type(i).__name__ for i in self.streamers)}")
|
logger.debug(f"├ streamers: {', '.join(type(i).__name__ for i in self.streamers)}")
|
||||||
|
|
||||||
|
self.consumer = consumer
|
||||||
|
logger.debug(f"├ consumer: {type(consumer).__name__}")
|
||||||
|
|
||||||
if not isinstance(formatters, list):
|
if not isinstance(formatters, list):
|
||||||
formatters = [formatters]
|
formatters = [formatters]
|
||||||
self.formatters = formatters
|
self.formatters = formatters
|
||||||
|
|
@ -166,7 +243,7 @@ def operator(asked_op):
|
||||||
# because the shell would try to interpret
|
# because the shell would try to interpret
|
||||||
# any unescaped/unquoted parentheses.
|
# any unescaped/unquoted parentheses.
|
||||||
# It is faster to use f:a for the user.
|
# It is faster to use f:a for the user.
|
||||||
m = re.match("(\w+):(.+)", op)
|
m = re.match(r"(\w+):(.+)", op)
|
||||||
if m:
|
if m:
|
||||||
name = m.group(1)
|
name = m.group(1)
|
||||||
args = [a.strip() for a in m.group(2).split(',')]
|
args = [a.strip() for a in m.group(2).split(',')]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue