22import re
33from enum import Enum
44from textwrap import dedent
5- from typing import TYPE_CHECKING , Any , Dict , Iterable , List , NamedTuple , Optional , Union
5+ from typing import (
6+ TYPE_CHECKING ,
7+ Any ,
8+ Dict ,
9+ List ,
10+ NamedTuple ,
11+ Optional ,
12+ Sequence ,
13+ Union ,
14+ )
615
716import yaml
817import yaml .parser
@@ -22,7 +31,7 @@ class Buttons(str, Enum):
2231
2332class Config (NamedTuple ):
2433 title : str
25- prompt_literal_start : Iterable [str ]
34+ prompt_literal_start : Sequence [str ]
2635 buttons : Buttons
2736
2837
@@ -47,7 +56,7 @@ def __repr__(self) -> str: # pragma:no cover
4756ParsedBlock = Union [Command , Comment , Output , Progress ]
4857
4958
50- def make_regex_prompts (prompt_literal_start : Iterable [str ]) -> "re.Pattern[str]" :
59+ def make_regex_prompts (prompt_literal_start : Sequence [str ]) -> "re.Pattern[str]" :
5160 prompt_literal_start = [re .escape (p ).strip () for p in prompt_literal_start ]
5261 prompt_to_replace = {
5362 ">" : ">" ,
@@ -88,7 +97,7 @@ def add_spaces(code: str, spaces: str) -> str:
8897 return "\n " .join (result )
8998
9099
91- def parse_config (raw : str ) -> Optional [Config ]:
100+ def parse_config (raw : str , default : Optional [ Config ] ) -> Optional [Config ]:
92101 try :
93102 config = yaml .full_load (raw )
94103 except yaml .parser .ParserError : # pragma:no cover
@@ -97,14 +106,22 @@ def parse_config(raw: str) -> Optional[Config]:
97106 if not isinstance (config , dict ):
98107 return None
99108
100- return parse_config_from_dict (config )
109+ return parse_config_from_dict (config , default )
101110
102111
103- def parse_config_from_dict (config : Dict [str , Any ]) -> Config :
112+ def parse_config_from_dict (
113+ config : Dict [str , Any ],
114+ default : Optional [Config ] = None ,
115+ ) -> Config :
104116 return Config (
105- title = str (config .get ("title" , "bash" )),
106- prompt_literal_start = list (config .get ("prompt_literal_start" , ("$" ,))),
107- buttons = Buttons (config .get ("buttons" , "macos" )),
117+ title = str (config .get ("title" , default .title if default else "bash" )),
118+ prompt_literal_start = list (
119+ config .get (
120+ "prompt_literal_start" ,
121+ default .prompt_literal_start if default else ("$" ,),
122+ ),
123+ ),
124+ buttons = Buttons (config .get ("buttons" , default .buttons if default else "macos" )),
108125 )
109126
110127
@@ -236,7 +253,7 @@ def run(self, lines: List[str]) -> List[str]:
236253 spaces = m .group ("spaces" ) or ""
237254 config_raw = (m .group ("config" ) or "" ).strip ()
238255 if config_raw :
239- config = parse_config (config_raw )
256+ config = parse_config (config_raw , self . config )
240257 if config :
241258 termynal = Termynal (config )
242259
0 commit comments