layout.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. from abc import ABC, abstractmethod
  2. from itertools import islice
  3. from operator import itemgetter
  4. from threading import RLock
  5. from typing import (
  6. TYPE_CHECKING,
  7. Dict,
  8. Iterable,
  9. List,
  10. NamedTuple,
  11. Optional,
  12. Sequence,
  13. Tuple,
  14. Union,
  15. )
  16. from ._ratio import ratio_resolve
  17. from .align import Align
  18. from .console import Console, ConsoleOptions, RenderableType, RenderResult
  19. from .highlighter import ReprHighlighter
  20. from .panel import Panel
  21. from .pretty import Pretty
  22. from .repr import rich_repr, Result
  23. from .region import Region
  24. from .segment import Segment
  25. from .style import StyleType
  26. if TYPE_CHECKING:
  27. from pip._vendor.rich.tree import Tree
  28. class LayoutRender(NamedTuple):
  29. """An individual layout render."""
  30. region: Region
  31. render: List[List[Segment]]
  32. RegionMap = Dict["Layout", Region]
  33. RenderMap = Dict["Layout", LayoutRender]
  34. class LayoutError(Exception):
  35. """Layout related error."""
  36. class NoSplitter(LayoutError):
  37. """Requested splitter does not exist."""
  38. class _Placeholder:
  39. """An internal renderable used as a Layout placeholder."""
  40. highlighter = ReprHighlighter()
  41. def __init__(self, layout: "Layout", style: StyleType = "") -> None:
  42. self.layout = layout
  43. self.style = style
  44. def __rich_console__(
  45. self, console: Console, options: ConsoleOptions
  46. ) -> RenderResult:
  47. width = options.max_width
  48. height = options.height or options.size.height
  49. layout = self.layout
  50. title = (
  51. f"{layout.name!r} ({width} x {height})"
  52. if layout.name
  53. else f"({width} x {height})"
  54. )
  55. yield Panel(
  56. Align.center(Pretty(layout), vertical="middle"),
  57. style=self.style,
  58. title=self.highlighter(title),
  59. border_style="blue",
  60. )
  61. class Splitter(ABC):
  62. """Base class for a splitter."""
  63. name: str = ""
  64. @abstractmethod
  65. def get_tree_icon(self) -> str:
  66. """Get the icon (emoji) used in layout.tree"""
  67. @abstractmethod
  68. def divide(
  69. self, children: Sequence["Layout"], region: Region
  70. ) -> Iterable[Tuple["Layout", Region]]:
  71. """Divide a region amongst several child layouts.
  72. Args:
  73. children (Sequence(Layout)): A number of child layouts.
  74. region (Region): A rectangular region to divide.
  75. """
  76. class RowSplitter(Splitter):
  77. """Split a layout region in to rows."""
  78. name = "row"
  79. def get_tree_icon(self) -> str:
  80. return "[layout.tree.row]⬌"
  81. def divide(
  82. self, children: Sequence["Layout"], region: Region
  83. ) -> Iterable[Tuple["Layout", Region]]:
  84. x, y, width, height = region
  85. render_widths = ratio_resolve(width, children)
  86. offset = 0
  87. _Region = Region
  88. for child, child_width in zip(children, render_widths):
  89. yield child, _Region(x + offset, y, child_width, height)
  90. offset += child_width
  91. class ColumnSplitter(Splitter):
  92. """Split a layout region in to columns."""
  93. name = "column"
  94. def get_tree_icon(self) -> str:
  95. return "[layout.tree.column]⬍"
  96. def divide(
  97. self, children: Sequence["Layout"], region: Region
  98. ) -> Iterable[Tuple["Layout", Region]]:
  99. x, y, width, height = region
  100. render_heights = ratio_resolve(height, children)
  101. offset = 0
  102. _Region = Region
  103. for child, child_height in zip(children, render_heights):
  104. yield child, _Region(x, y + offset, width, child_height)
  105. offset += child_height
  106. @rich_repr
  107. class Layout:
  108. """A renderable to divide a fixed height in to rows or columns.
  109. Args:
  110. renderable (RenderableType, optional): Renderable content, or None for placeholder. Defaults to None.
  111. name (str, optional): Optional identifier for Layout. Defaults to None.
  112. size (int, optional): Optional fixed size of layout. Defaults to None.
  113. minimum_size (int, optional): Minimum size of layout. Defaults to 1.
  114. ratio (int, optional): Optional ratio for flexible layout. Defaults to 1.
  115. visible (bool, optional): Visibility of layout. Defaults to True.
  116. """
  117. splitters = {"row": RowSplitter, "column": ColumnSplitter}
  118. def __init__(
  119. self,
  120. renderable: Optional[RenderableType] = None,
  121. *,
  122. name: Optional[str] = None,
  123. size: Optional[int] = None,
  124. minimum_size: int = 1,
  125. ratio: int = 1,
  126. visible: bool = True,
  127. height: Optional[int] = None,
  128. ) -> None:
  129. self._renderable = renderable or _Placeholder(self)
  130. self.size = size
  131. self.minimum_size = minimum_size
  132. self.ratio = ratio
  133. self.name = name
  134. self.visible = visible
  135. self.height = height
  136. self.splitter: Splitter = self.splitters["column"]()
  137. self._children: List[Layout] = []
  138. self._render_map: RenderMap = {}
  139. self._lock = RLock()
  140. def __rich_repr__(self) -> Result:
  141. yield "name", self.name, None
  142. yield "size", self.size, None
  143. yield "minimum_size", self.minimum_size, 1
  144. yield "ratio", self.ratio, 1
  145. @property
  146. def renderable(self) -> RenderableType:
  147. """Layout renderable."""
  148. return self if self._children else self._renderable
  149. @property
  150. def children(self) -> List["Layout"]:
  151. """Gets (visible) layout children."""
  152. return [child for child in self._children if child.visible]
  153. @property
  154. def map(self) -> RenderMap:
  155. """Get a map of the last render."""
  156. return self._render_map
  157. def get(self, name: str) -> Optional["Layout"]:
  158. """Get a named layout, or None if it doesn't exist.
  159. Args:
  160. name (str): Name of layout.
  161. Returns:
  162. Optional[Layout]: Layout instance or None if no layout was found.
  163. """
  164. if self.name == name:
  165. return self
  166. else:
  167. for child in self._children:
  168. named_layout = child.get(name)
  169. if named_layout is not None:
  170. return named_layout
  171. return None
  172. def __getitem__(self, name: str) -> "Layout":
  173. layout = self.get(name)
  174. if layout is None:
  175. raise KeyError(f"No layout with name {name!r}")
  176. return layout
  177. @property
  178. def tree(self) -> "Tree":
  179. """Get a tree renderable to show layout structure."""
  180. from pip._vendor.rich.styled import Styled
  181. from pip._vendor.rich.table import Table
  182. from pip._vendor.rich.tree import Tree
  183. def summary(layout: "Layout") -> Table:
  184. icon = layout.splitter.get_tree_icon()
  185. table = Table.grid(padding=(0, 1, 0, 0))
  186. text: RenderableType = (
  187. Pretty(layout) if layout.visible else Styled(Pretty(layout), "dim")
  188. )
  189. table.add_row(icon, text)
  190. _summary = table
  191. return _summary
  192. layout = self
  193. tree = Tree(
  194. summary(layout),
  195. guide_style=f"layout.tree.{layout.splitter.name}",
  196. highlight=True,
  197. )
  198. def recurse(tree: "Tree", layout: "Layout") -> None:
  199. for child in layout._children:
  200. recurse(
  201. tree.add(
  202. summary(child),
  203. guide_style=f"layout.tree.{child.splitter.name}",
  204. ),
  205. child,
  206. )
  207. recurse(tree, self)
  208. return tree
  209. def split(
  210. self,
  211. *layouts: Union["Layout", RenderableType],
  212. splitter: Union[Splitter, str] = "column",
  213. ) -> None:
  214. """Split the layout in to multiple sub-layouts.
  215. Args:
  216. *layouts (Layout): Positional arguments should be (sub) Layout instances.
  217. splitter (Union[Splitter, str]): Splitter instance or name of splitter.
  218. """
  219. _layouts = [
  220. layout if isinstance(layout, Layout) else Layout(layout)
  221. for layout in layouts
  222. ]
  223. try:
  224. self.splitter = (
  225. splitter
  226. if isinstance(splitter, Splitter)
  227. else self.splitters[splitter]()
  228. )
  229. except KeyError:
  230. raise NoSplitter(f"No splitter called {splitter!r}")
  231. self._children[:] = _layouts
  232. def add_split(self, *layouts: Union["Layout", RenderableType]) -> None:
  233. """Add a new layout(s) to existing split.
  234. Args:
  235. *layouts (Union[Layout, RenderableType]): Positional arguments should be renderables or (sub) Layout instances.
  236. """
  237. _layouts = (
  238. layout if isinstance(layout, Layout) else Layout(layout)
  239. for layout in layouts
  240. )
  241. self._children.extend(_layouts)
  242. def split_row(self, *layouts: Union["Layout", RenderableType]) -> None:
  243. """Split the layout in tow a row (Layouts side by side).
  244. Args:
  245. *layouts (Layout): Positional arguments should be (sub) Layout instances.
  246. """
  247. self.split(*layouts, splitter="row")
  248. def split_column(self, *layouts: Union["Layout", RenderableType]) -> None:
  249. """Split the layout in to a column (layouts stacked on top of each other).
  250. Args:
  251. *layouts (Layout): Positional arguments should be (sub) Layout instances.
  252. """
  253. self.split(*layouts, splitter="column")
  254. def unsplit(self) -> None:
  255. """Reset splits to initial state."""
  256. del self._children[:]
  257. def update(self, renderable: RenderableType) -> None:
  258. """Update renderable.
  259. Args:
  260. renderable (RenderableType): New renderable object.
  261. """
  262. with self._lock:
  263. self._renderable = renderable
  264. def refresh_screen(self, console: "Console", layout_name: str) -> None:
  265. """Refresh a sub-layout.
  266. Args:
  267. console (Console): Console instance where Layout is to be rendered.
  268. layout_name (str): Name of layout.
  269. """
  270. with self._lock:
  271. layout = self[layout_name]
  272. region, _lines = self._render_map[layout]
  273. (x, y, width, height) = region
  274. lines = console.render_lines(
  275. layout, console.options.update_dimensions(width, height)
  276. )
  277. self._render_map[layout] = LayoutRender(region, lines)
  278. console.update_screen_lines(lines, x, y)
  279. def _make_region_map(self, width: int, height: int) -> RegionMap:
  280. """Create a dict that maps layout on to Region."""
  281. stack: List[Tuple[Layout, Region]] = [(self, Region(0, 0, width, height))]
  282. push = stack.append
  283. pop = stack.pop
  284. layout_regions: List[Tuple[Layout, Region]] = []
  285. append_layout_region = layout_regions.append
  286. while stack:
  287. append_layout_region(pop())
  288. layout, region = layout_regions[-1]
  289. children = layout.children
  290. if children:
  291. for child_and_region in layout.splitter.divide(children, region):
  292. push(child_and_region)
  293. region_map = {
  294. layout: region
  295. for layout, region in sorted(layout_regions, key=itemgetter(1))
  296. }
  297. return region_map
  298. def render(self, console: Console, options: ConsoleOptions) -> RenderMap:
  299. """Render the sub_layouts.
  300. Args:
  301. console (Console): Console instance.
  302. options (ConsoleOptions): Console options.
  303. Returns:
  304. RenderMap: A dict that maps Layout on to a tuple of Region, lines
  305. """
  306. render_width = options.max_width
  307. render_height = options.height or console.height
  308. region_map = self._make_region_map(render_width, render_height)
  309. layout_regions = [
  310. (layout, region)
  311. for layout, region in region_map.items()
  312. if not layout.children
  313. ]
  314. render_map: Dict["Layout", "LayoutRender"] = {}
  315. render_lines = console.render_lines
  316. update_dimensions = options.update_dimensions
  317. for layout, region in layout_regions:
  318. lines = render_lines(
  319. layout.renderable, update_dimensions(region.width, region.height)
  320. )
  321. render_map[layout] = LayoutRender(region, lines)
  322. return render_map
  323. def __rich_console__(
  324. self, console: Console, options: ConsoleOptions
  325. ) -> RenderResult:
  326. with self._lock:
  327. width = options.max_width or console.width
  328. height = options.height or console.height
  329. render_map = self.render(console, options.update_dimensions(width, height))
  330. self._render_map = render_map
  331. layout_lines: List[List[Segment]] = [[] for _ in range(height)]
  332. _islice = islice
  333. for (region, lines) in render_map.values():
  334. _x, y, _layout_width, layout_height = region
  335. for row, line in zip(
  336. _islice(layout_lines, y, y + layout_height), lines
  337. ):
  338. row.extend(line)
  339. new_line = Segment.line()
  340. for layout_row in layout_lines:
  341. yield from layout_row
  342. yield new_line
  343. if __name__ == "__main__":
  344. from pip._vendor.rich.console import Console
  345. console = Console()
  346. layout = Layout()
  347. layout.split_column(
  348. Layout(name="header", size=3),
  349. Layout(ratio=1, name="main"),
  350. Layout(size=10, name="footer"),
  351. )
  352. layout["main"].split_row(Layout(name="side"), Layout(name="body", ratio=2))
  353. layout["body"].split_row(Layout(name="content", ratio=2), Layout(name="s2"))
  354. layout["s2"].split_column(
  355. Layout(name="top"), Layout(name="middle"), Layout(name="bottom")
  356. )
  357. layout["side"].split_column(Layout(layout.tree, name="left1"), Layout(name="left2"))
  358. layout["content"].update("foo")
  359. console.print(layout)