Skip to content

Commit

Permalink
✨Terminal file output (#220)
Browse files Browse the repository at this point in the history
* Added file redirect click.option to terminal.py

* Got rid of indentation error

* Added code for output

* Redirects standard output stream to file

* Outputs to both terminal and file

* Modified imported modules

* Added line at end

* changes on windows

* Update pros/cli/terminal.py for terminal file output

Co-authored-by: Will Xu <54247087+WillXuCodes@users.noreply.github.com>

* Removed ident error

Co-authored-by: BennyBot <48661356+BennyBot@users.noreply.github.com>

Co-authored-by: Will Xu <xu1321@purdue.edu>
Co-authored-by: Benjamin Davis <davi1561@purdue.edu>
Co-authored-by: Will Xu <54247087+WillXuCodes@users.noreply.github.com>
Co-authored-by: BennyBot <48661356+BennyBot@users.noreply.github.com>
  • Loading branch information
5 people committed Aug 22, 2022
1 parent f57a010 commit ac6135d
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion pros/cli/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time

import click
import sys

import pros.conductor as c
import pros.serial.devices as devices
Expand All @@ -29,6 +30,8 @@ def terminal_cli():
help='Specify 2 ports for the "share" backend. The default option deterministically selects ports '
'based on the serial port name')
@click.option('--banner/--no-banner', 'request_banner', default=True)
@click.option('--output', nargs = 1, type=str, is_eager = True, help='Redirect terminal output to a file', default=None)

def terminal(port: str, backend: str, **kwargs):
"""
Open a terminal to a serial port
Expand All @@ -39,7 +42,7 @@ def terminal(port: str, backend: str, **kwargs):
may be preferred when "share" doesn't perform adequately.
Note: share backend is not yet implemented.
"""
"""
analytics.send("terminal")
from pros.serial.devices.vex.v5_user_device import V5UserDevice
from pros.serial.terminal import Terminal
Expand Down Expand Up @@ -82,9 +85,34 @@ def terminal(port: str, backend: str, **kwargs):
device = devices.vex.V5UserDevice(ser)
term = Terminal(device, request_banner=kwargs.pop('request_banner', True))

class TerminalOutput(object):
def __init__(self, file):
self.terminal = sys.stdout
self.log = open(file, 'a')
def write(self, data):
self.terminal.write(data)
self.log.write(data)
def flush(self):
pass
def end(self):
self.log.close()

output = None
if kwargs.get('output', None):
output_file = kwargs['output']
output = TerminalOutput(f'{output_file}')
term.console.output = output
sys.stdout = output
logger(__name__).info(f'Redirecting Terminal Output to File: {output_file}')
else:
sys.stdout = sys.__stdout__

signal.signal(signal.SIGINT, term.stop)
term.start()
while not term.alive.is_set():
time.sleep(0.005)
sys.stdout = sys.__stdout__
if output:
output.end()
term.join()
logger(__name__).info('CLI Main Thread Dying')

0 comments on commit ac6135d

Please sign in to comment.