Print tabulation added

This commit is contained in:
Gino D
2022-12-31 16:17:29 +01:00
parent 56bb67a5a2
commit 9eb7c15ed6

View File

@@ -37,7 +37,9 @@ from typing import Dict, List, Optional, Tuple, Union
BUFFER_SIZE: int = 126 BUFFER_SIZE: int = 126
PRINT_STRPAD = '\t\t =>'
def strpad(text: str, length: int = 30) -> str:
return text + ' ' * (length - len(text))
class Telnet: class Telnet:
@@ -187,7 +189,7 @@ class Pyboard(object):
self.loading() self.loading()
else: else:
timeout += 1 timeout += 1
time.sleep(0.001) time.sleep(0.0001)
def __enter__(self): def __enter__(self):
self.send_ctrl_c() self.send_ctrl_c()
@@ -230,7 +232,7 @@ class Pyboard(object):
self.loading() self.loading()
self.serial.write(command[i: min(i + BUFFER_SIZE, len(command))]) self.serial.write(command[i: min(i + BUFFER_SIZE, len(command))])
time.sleep(0.001) time.sleep(0.0001)
if not self.read_until(self.send_ok()): if not self.read_until(self.send_ok()):
raise Exception('Terminal: could not execute command') raise Exception('Terminal: could not execute command')
@@ -526,17 +528,21 @@ class Picowatch(object):
status, output, exception = self._fs.ls(remote) status, output, exception = self._fs.ls(remote)
if status: if status:
print('-' * 60)
print('[ ]', strpad('filename'), 'size|exception')
print('-' * 60)
for name, size in output: for name, size in output:
if size == -1: if size == -1:
print('[*]', name[1:]) print('[*]', strpad(name[1:]), '-')
else: else:
print('[.]', name[1:], f'({size}b)') print('[*]', strpad(name[1:]), f'{size}b')
else: else:
print('[?]', remote, PRINT_STRPAD, exception) print('[?]', strpad(remote), exception)
def contents(self, remote: str): def contents(self, remote: str):
try: try:
content, checksum = self._fs.get(remote) content, _ = self._fs.get(remote)
for ln in content.decode('utf-8').split('\n'): for ln in content.decode('utf-8').split('\n'):
print(ln) print(ln)
@@ -563,11 +569,15 @@ class Picowatch(object):
elif os.path.exists(local): elif os.path.exists(local):
queue.append((local, filepath)) queue.append((local, filepath))
print('-' * 60)
print('[ ]', strpad('filename'), 'checksum|exception')
print('-' * 60)
for filename, remote in queue: for filename, remote in queue:
try: try:
print('[↑]', filename, PRINT_STRPAD, self._fs.upload(filename, remote)) print('[↑]', strpad(filename), self._fs.upload(filename, remote))
except Exception as e: except Exception as e:
print('[?]', filename, PRINT_STRPAD, str(e)) print('[?]', strpad(filename), str(e))
def download(self, filepath: str): def download(self, filepath: str):
if filepath.startswith('.'): if filepath.startswith('.'):
@@ -576,6 +586,10 @@ class Picowatch(object):
status, output, exception = self._fs.ls(filepath) status, output, exception = self._fs.ls(filepath)
if status: if status:
print('-' * 60)
print('[ ]', strpad('filename'), 'checksum|exception')
print('-' * 60)
for remote, size in output: for remote, size in output:
if size == -1: if size == -1:
os.makedirs(f'.{remote}', 777, exist_ok=True) os.makedirs(f'.{remote}', 777, exist_ok=True)
@@ -585,25 +599,29 @@ class Picowatch(object):
if not size == -1: if not size == -1:
try: try:
print('[↓]', local, PRINT_STRPAD, self._fs.download(remote, local)) print('[↓]', strpad(local), self._fs.download(remote, local))
except Exception as e: except Exception as e:
print('[?]', local, PRINT_STRPAD, str(e)) print('[?]', strpad(local), str(e))
else: else:
print('[?]', filepath, PRINT_STRPAD, exception) print('[?]', strpad(filepath), exception)
def delete(self, filepath: str): def delete(self, filepath: str):
status, output, exception = self._fs.rm(filepath) status, output, exception = self._fs.rm(filepath)
if status: if status:
print('-' * 60)
print('[ ]', strpad('filename'), 'exception')
print('-' * 60)
for remote, checked, message in output: for remote, checked, message in output:
local = f'.{remote}' local = f'.{remote}'
if checked: if checked:
print('[-]', local) print('[-]', strpad(local))
else: else:
print('[?]', local, PRINT_STRPAD, message) print('[?]', strpad(local), message)
else: else:
print('[?]', filepath, PRINT_STRPAD, exception) print('[?]', strpad(filepath), exception)
def launch(self, filepath: str): def launch(self, filepath: str):
self._fs.launch(filepath) self._fs.launch(filepath)
@@ -686,46 +704,51 @@ picowatch.interupt()
# try: # try:
while True: while True:
try: try:
message = input('>>> ').strip() unmessage = input('>>> ').strip()
match message.split(' '): for message in unmessage.split('&'):
case ['0' | 'exit']: match message.strip().split(' '):
sys.exit() case ['0' | 'exit']:
case ['reboot' | 'reset']: sys.exit()
picowatch.terminal('help()') case ['reboot' | 'reset']:
case ['ls' | 'stat', *source]: picowatch.terminal('help()')
picowatch.listing(source[0] if source else '/') case ['ls' | 'stat', *source]:
case ['cat' | 'open' | 'contents', source]: picowatch.listing(source[0] if source else '/')
picowatch.contents(source) case ['cat' | 'open' | 'contents', source]:
case ['del' | 'rm' | 'delete' | 'remove', source]: picowatch.contents(source)
picowatch.delete(source) case ['del' | 'rm' | 'delete' | 'remove', source]:
case ['format']: picowatch.delete(source)
picowatch.delete('/') case ['format']:
case ['upl' | 'upload' | 'update', source]: picowatch.delete('/')
picowatch.upload(source) case ['upl' | 'upload' | 'update', source]:
case ['restore']: picowatch.upload(source)
picowatch.upload('/') case ['restore']:
case ['download' | 'transfer', source]: picowatch.upload('/')
picowatch.download(source) case ['download' | 'transfer', source]:
case ['backup']: picowatch.download(source)
picowatch.download('/') case ['backup']:
# case ['' | 'save' | 'commit']: picowatch.download('/')
# watchdog_callback() # case ['' | 'save' | 'commit']:
# case ['status' | 'staged']: # watchdog_callback()
# for filename in sessions['deleted']: # case ['status' | 'staged']:
# print('-', filename) # for filename in sessions['deleted']:
# for filename in sessions['modified']: # print('-', filename)
# print('+', filename) # for filename in sessions['modified']:
# case ['cancel' | 'unstaged']: # print('+', filename)
# sessions['deleted'] = set() # case ['cancel' | 'unstaged']:
# sessions['modified'] = set() # sessions['deleted'] = set()
case ['watch' | 'test', filename]: # sessions['modified'] = set()
picowatch.watch(filename) case ['!', 'watch' | 'test', filename]:
case _: picowatch.watch(filename)
if message.startswith('./'): case ['!']:
picowatch.launch(message[2:]) picowatch.watch('main.py')
elif message: case ['!!']:
print(f'"{message}" is not recognized.') picowatch.launch('main.py')
case _:
if message.startswith('./'):
picowatch.launch(message[2:])
elif message:
print(f'"{message}" is not recognized.')
except Exception as e: except Exception as e:
print(str(e)) print(str(e))
# except KeyboardInterrupt: # except KeyboardInterrupt: