Skip to content

Commit

Permalink
Fix parsing of asciicast files
Browse files Browse the repository at this point in the history
  • Loading branch information
ESultanik committed Jan 26, 2023
1 parent 8edb1f8 commit 1ee164c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
23 changes: 12 additions & 11 deletions cast2gif/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ def main(argv=None):
print(__version_name__)
sys.exit(0)

if args.auto_size:
term_size = AutoTerminalSize()
elif args.width is not None and args.height is not None:
term_size = FixedTerminalSize(width=args.width, height=args.height)
else:
term_size = InheritedTerminalSize()
if args.width is not None:
term_size.width = args.width
elif args.height is not None:
term_size.height = args.height

if args.ASCIICAST is None and not args.exec:
parser.print_usage(sys.stderr)
sys.stderr.write("\nerror: you must provide either an AsciiCast v2 input file or use the `--exec` argument\n")
Expand All @@ -174,16 +185,6 @@ def main(argv=None):
ps1 = args.ps1
else:
ps1 = os.getenv("PS1", "\u001b[32m$\u001b[0m ")
if args.auto_size:
term_size = AutoTerminalSize()
elif args.width is not None and args.height is not None:
term_size = FixedTerminalSize(width=args.width, height=args.height)
else:
term_size = InheritedTerminalSize()
if args.width is not None:
term_size.width = args.width
elif args.height is not None:
term_size.height = args.height
recording: TerminalRecording = TerminalRecording.record(args.exec, ps1=ps1, terminal_size=term_size)
if recording.return_value != 0 and sys.stderr.isatty() and not args.quiet:
sys.stderr.write(f"\n\nWarning: `{' '.join(args.exec)}` exited with code {recording.return_value}\n\n")
Expand All @@ -194,7 +195,7 @@ def main(argv=None):
input_stream = open(args.ASCIICAST, "rb")
try:
input_isatty = input_stream.isatty()
recording = AsciiCast.load(input_stream.read(), width=args.width, height=args.height)
recording = AsciiCast.load(input_stream.read(), terminal_size=term_size)
finally:
if input_stream != sys.stdin:
input_stream.close()
Expand Down
25 changes: 11 additions & 14 deletions cast2gif/asciicast.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from typing import Any, Dict, Iterable, Optional, Type, Union
from typing import Any, Dict, Iterable, Type, Union

from .recording import AutoTerminalSize, C, TerminalOutput, TerminalRecording, TerminalSize
from .recording import AutoTerminalSize, C, InheritedTerminalSize, TerminalOutput, TerminalRecording, TerminalSize


class AsciiCast(TerminalRecording):
Expand All @@ -16,29 +16,26 @@ def metadata(self) -> Dict[str, Any]:

@metadata.setter
def metadata(self, new_metadata: Dict[str, Any]):
if "width" in new_metadata:
self.width = new_metadata["width"]
if "height" in new_metadata:
self.height = new_metadata["height"]
if "width" in new_metadata and isinstance(self._size, InheritedTerminalSize) and self._size.width_was_inherited:
self._size.width = new_metadata["width"]
self._size.width_was_inherited = False
if "height" in new_metadata and isinstance(self._size, InheritedTerminalSize) \
and self._size.height_was_inherited:
self._size.height = new_metadata["height"]
self._size.height_was_inherited = False
self._metadata = new_metadata

@classmethod
def load(cls: Type[C], cast: Union[bytes, str, Iterable[str]],
width: Optional[int] = None, height: Optional[int] = None) -> C:
terminal_size: TerminalSize = InheritedTerminalSize()) -> C:
if isinstance(cast, str) or isinstance(cast, bytes):
cast = cast.splitlines()

ascii_cast = cls(width=width, height=height)
ascii_cast = cls(terminal_size=terminal_size)

for i, line in enumerate(cast):
if i == 0:
ascii_cast.metadata = json.loads(line)
if width is not None:
# override the original metadata
ascii_cast.width = width
if height is not None:
# override the original metadata
ascii_cast.height = height
else:
event_time, event_type, data = json.loads(line)
if event_type == "o":
Expand Down
2 changes: 2 additions & 0 deletions cast2gif/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def __init__(self, width: int = 80, height: int = 24):

class InheritedTerminalSize(FixedTerminalSize):
def __init__(self, width: Optional[int] = None, height: Optional[int] = None):
self.width_was_inherited: bool = width is None
self.height_was_inherited: bool = height is None
if width is None or height is None:
try:
term_size = os.get_terminal_size()
Expand Down
6 changes: 3 additions & 3 deletions cast2gif/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ def _write_escbkt(self, char: str):
# we don't need to handle bracketed paste mode
pass
else:
raise Exception("ESC[%sh escape is currently unsupported!" % self._esc)
raise NotImplementedError("ESC[%sh escape is currently unsupported!" % self._esc)
elif char == 'l':
if self._esc == '?2004':
# we don't need to handle bracketed paste mode
pass
else:
raise Exception("ESC[%sl escape is currently unsupported!" % self._esc)
raise NotImplementedError("ESC[%sl escape is currently unsupported!" % self._esc)
elif char == 'm':
self._write_esc_m()
elif char == 's':
Expand All @@ -124,7 +124,7 @@ def _write_escbkt(self, char: str):
if self._stored_pos is not None:
self.move_to(*self._stored_pos)
elif char in 'STfinhl':
raise Exception(f"ESC[{self._esc}{char} escape is currently unsupported!")
raise NotImplementedError(f"ESC[{self._esc}{char} escape is currently unsupported!")
else:
matched = False
if matched:
Expand Down

0 comments on commit 1ee164c

Please sign in to comment.