11import logging
2+ import os
3+ from types import TracebackType
24from typing import Any , Literal
35
46from rich .segment import Segment
7+ from rich .style import Style
8+ from rich .text import Text
59from rich_toolkit import RichToolkit , RichToolkitTheme
6- from rich_toolkit .styles import MinimalStyle , TaggedStyle
10+ from rich_toolkit .styles import BaseStyle , MinimalStyle , TaggedStyle
11+
12+ from fastapi_cloud_cli .utils .version_check import (
13+ DISABLE_VERSION_CHECK_ENV ,
14+ BackgroundVersionCheck ,
15+ )
716
817logger = logging .getLogger (__name__ )
918
@@ -20,10 +29,20 @@ def _get_tag_segments(
2029 animation_status : Literal ["started" , "stopped" , "error" ] | None = None ,
2130 ) -> tuple [list [Segment ], int ]:
2231 if not is_animated :
23- return super ()._get_tag_segments (
32+ tag_segments , left_padding = super ()._get_tag_segments (
2433 metadata , is_animated , done , animation_status = animation_status
2534 )
2635
36+ tag_style = metadata .get ("tag_style" )
37+
38+ if isinstance (tag_style , (str , Style )):
39+ style = self .console .get_style (tag_style )
40+ tag_segments = [
41+ Segment (segment .text , style = style ) for segment in tag_segments
42+ ]
43+
44+ return tag_segments , left_padding
45+
2746 emojis = [
2847 "🥚" ,
2948 "🐣" ,
@@ -47,6 +66,46 @@ def _get_tag_segments(
4766 return [Segment (tag )], left_padding
4867
4968
69+ class FastAPIRichToolkit (RichToolkit ):
70+ def __init__ (
71+ self ,
72+ style : BaseStyle | None = None ,
73+ theme : RichToolkitTheme | None = None ,
74+ ) -> None :
75+ super ().__init__ (style = style , theme = theme )
76+ self ._version_check = self ._get_version_check ()
77+
78+ def __exit__ (
79+ self ,
80+ exc_type : type [BaseException ] | None ,
81+ exc_value : BaseException | None ,
82+ traceback : TracebackType | None ,
83+ ) -> bool | None :
84+ self ._print_update_message ()
85+
86+ return super ().__exit__ (
87+ exc_type ,
88+ exc_value ,
89+ traceback ,
90+ )
91+
92+ def _get_version_check (self ) -> BackgroundVersionCheck | None :
93+ if os .environ .get (DISABLE_VERSION_CHECK_ENV ) == "1" :
94+ return None
95+
96+ version_check = BackgroundVersionCheck ()
97+ version_check .start ()
98+
99+ return version_check
100+
101+ def _print_update_message (self ) -> None :
102+ if self ._version_check is None :
103+ return
104+
105+ if message := self ._version_check .get_update_message ():
106+ self .print (Text .from_markup (message ), tag = "update" , tag_style = "tag.update" )
107+
108+
50109def get_rich_toolkit (minimal : bool = False ) -> RichToolkit :
51110 style = MinimalStyle () if minimal else FastAPIStyle (tag_width = 11 )
52111
@@ -55,6 +114,7 @@ def get_rich_toolkit(minimal: bool = False) -> RichToolkit:
55114 theme = {
56115 "tag.title" : "white on #009485" ,
57116 "tag" : "white on #007166" ,
117+ "tag.update" : "black on yellow" ,
58118 "placeholder" : "grey62" ,
59119 "text" : "white" ,
60120 "selected" : "#007166" ,
@@ -65,4 +125,4 @@ def get_rich_toolkit(minimal: bool = False) -> RichToolkit:
65125 },
66126 )
67127
68- return RichToolkit (theme = theme )
128+ return FastAPIRichToolkit (theme = theme )
0 commit comments