Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

persist rich mode across updates #1757

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 20 additions & 12 deletions subiquitycore/tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import asyncio
import inspect
import json
import logging
import os
import signal
Expand Down Expand Up @@ -77,9 +78,7 @@ def __init__(self, opts):
if not opts.dry_run:
open("/run/casper-no-prompt", "w").close()

# Set rich_mode to the opposite of what we want, so we can
# call toggle_rich to get the right things set up.
self.rich_mode = opts.run_on_serial
self.rich_mode = None
self.urwid_loop = None
self.cur_screen = None
self.fg_proc = None
Expand Down Expand Up @@ -256,21 +255,23 @@ def select_initial_screen(self):
def set_rich(self, rich):
if rich == self.rich_mode:
return
self.toggle_rich()

def toggle_rich(self):
if self.rich_mode:
urwid.util.set_encoding("ascii")
new_palette = PALETTE_MONO
self.rich_mode = False
else:
if rich:
urwid.util.set_encoding("utf-8")
new_palette = PALETTE_COLOR
self.rich_mode = True
else:
urwid.util.set_encoding("ascii")
new_palette = PALETTE_MONO
self.rich_mode = False
with open(self.state_path("rich-mode"), "w") as fp:
json.dump(self.rich_mode, fp)
urwid.CanvasCache.clear()
self.urwid_loop.screen.register_palette(new_palette)
self.urwid_loop.screen.clear()

def toggle_rich(self):
self.set_rich(not self.rich_mode)

def unhandled_input(self, key):
if self.opts.dry_run and key == "ctrl x":
self.exit()
Expand Down Expand Up @@ -301,7 +302,14 @@ def start_urwid(self, input=None, output=None):
**self.extra_urwid_loop_args(),
)
extend_dec_special_charmap()
self.toggle_rich()
try:
fp = open(self.state_path("rich-mode"))
except FileNotFoundError:
initial_rich_mode = not self.opts.run_on_serial
else:
with fp:
initial_rich_mode = json.load(fp)
self.set_rich(initial_rich_mode)
self.urwid_loop.start()
self.select_initial_screen()

Expand Down