Skip to content

Commit

Permalink
Apply formatting to exception text
Browse files Browse the repository at this point in the history
  • Loading branch information
civerachb-cpr committed Sep 11, 2024
1 parent 84dea4d commit 0008c94
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions turtlebot4_setup/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,21 @@ def list_files(self):
return files

def highlight_file(self, filepath):
try:
lexer = lexers.get_lexer_for_filename(filepath, stripnl=False, stripall=False)
except ClassNotFound:
lexer = lexers.get_lexer_by_name('text', stripnl=False, stripall=False)
formatter = formatters.TerminalFormatter(bg='dark') # dark or light

try:
with open(filepath, 'r') as f:
file_content = f.read()
try:
lexer = lexers.get_lexer_for_filename(filepath, stripnl=False, stripall=False)
except ClassNotFound:
lexer = lexers.get_lexer_by_name('text', stripnl=False, stripall=False)
formatter = formatters.TerminalFormatter(bg='dark') # dark or light
highlighted_file_content = highlight(file_content, lexer, formatter)
return highlighted_file_content
except PermissionError:
return 'Permission denied.\nPlease check file permissions'
file_content = 'Permission denied.\nPlease check file permissions'
except FileNotFoundError:
return f'{filepath} was deleted'
file_content = f'{filepath} was deleted'
except Exception as err:
return f'Error reading {filepath}:\n{err}'
file_content = f'Error reading {filepath}:\n{err}'

highlighted_file_content = highlight(file_content, lexer, formatter)
return highlighted_file_content

0 comments on commit 0008c94

Please sign in to comment.