@@ -13,7 +13,11 @@ use opentui::input::strip_ansi_sequences;
1313use crate :: prelude:: * ;
1414
1515pub struct Live {
16- part : RefCell < SessionMessageAssistantTool > ,
16+ part : RefCell < Option < SessionMessageAssistantTool > > ,
17+ /// A body and output given whole, such as an automation's, shown in place of the tool call's.
18+ given : Option < Shown > ,
19+ /// What escape and the close label run in place of closing, to go back to the dialog this one opened from.
20+ back : Option < Rc < dyn Fn ( ) > > ,
1721 copied : Cell < Option < Copied > > ,
1822 draw : RefCell < Option < Rc < dyn Fn ( ) > > > ,
1923}
@@ -26,13 +30,21 @@ enum Copied {
2630
2731impl Live {
2832 pub fn set ( & self , part : SessionMessageAssistantTool ) {
29- if * self . part . borrow ( ) == part {
33+ if self . part . borrow ( ) . as_ref ( ) == Some ( & part) {
3034 return ;
3135 }
32- * self . part . borrow_mut ( ) = part;
36+ * self . part . borrow_mut ( ) = Some ( part) ;
3337 self . redraw ( ) ;
3438 }
3539
40+ fn shown ( & self ) -> Shown {
41+ match ( & self . given , self . part . borrow ( ) . as_ref ( ) ) {
42+ ( Some ( given) , _) => given. clone ( ) ,
43+ ( None , Some ( part) ) => Shown :: new ( part) ,
44+ ( None , None ) => unreachable ! ( "a dialog holds a tool call or a given body" ) ,
45+ }
46+ }
47+
3648 fn redraw ( & self ) {
3749 let draw = self . draw . borrow ( ) . clone ( ) ;
3850 if let Some ( draw) = draw {
@@ -43,7 +55,58 @@ impl Live {
4355
4456/// Opens the dialog for `part` in place of any open dialog. The handle lives while the dialog is open.
4557pub fn show ( tui : & Tui , part : SessionMessageAssistantTool ) -> Weak < Live > {
46- let live = Rc :: new ( Live { part : RefCell :: new ( part) , copied : Cell :: new ( None ) , draw : RefCell :: default ( ) } ) ;
58+ open (
59+ tui,
60+ Live {
61+ part : RefCell :: new ( Some ( part) ) ,
62+ given : None ,
63+ back : None ,
64+ copied : Cell :: new ( None ) ,
65+ draw : RefCell :: default ( ) ,
66+ } ,
67+ )
68+ }
69+
70+ /// A body and its output given whole, for [`show_given`].
71+ #[ derive( Clone ) ]
72+ pub struct Given {
73+ pub title : String ,
74+ pub status : String ,
75+ /// The body's heading, such as `Code` or `Prompt`.
76+ pub label : & ' static str ,
77+ /// The body's language; prose, such as a prompt, has none and wraps by word.
78+ pub filetype : Option < & ' static str > ,
79+ pub body : String ,
80+ pub output : Option < String > ,
81+ pub failed : bool ,
82+ }
83+
84+ /// Opens the dialog for `given`, such as an automation's script and last output. Escape runs `back`.
85+ pub fn show_given ( tui : & Tui , given : Given , back : Rc < dyn Fn ( ) > ) {
86+ let text = given. output . filter ( |text| !text. trim ( ) . is_empty ( ) ) ;
87+ let shown = Shown {
88+ highlighted : text. as_deref ( ) . and_then ( highlighted) ,
89+ code : Some ( given. body ) . filter ( |body| !body. is_empty ( ) ) ,
90+ text,
91+ failed : given. failed ,
92+ completed : true ,
93+ status : given. status ,
94+ title : given. title ,
95+ label : given. label ,
96+ filetype : given. filetype ,
97+ } ;
98+ let live = Live {
99+ part : RefCell :: new ( None ) ,
100+ given : Some ( shown) ,
101+ back : Some ( back) ,
102+ copied : Cell :: new ( None ) ,
103+ draw : RefCell :: default ( ) ,
104+ } ;
105+ open ( tui, live) ;
106+ }
107+
108+ fn open ( tui : & Tui , live : Live ) -> Weak < Live > {
109+ let live = Rc :: new ( live) ;
47110 let handle = Rc :: downgrade ( & live) ;
48111 let weak = tui. weak ( ) ;
49112 let keep = live. clone ( ) ;
@@ -65,7 +128,7 @@ fn view(cx: &Rc<DialogCx>, tui: Weak<Tui>, live: &Rc<Live>, page: Theme) -> View
65128 let ( tui, live) = ( tui. clone ( ) , Rc :: downgrade ( live) ) ;
66129 Rc :: new ( move |kind| {
67130 let ( Some ( tui) , Some ( live) ) = ( tui. upgrade ( ) , live. upgrade ( ) ) else { return } ;
68- let shown = Shown :: new ( & live. part . borrow ( ) ) ;
131+ let shown = live. shown ( ) ;
69132 let Some ( text) = ( match kind {
70133 Copied :: Code => shown. code ,
71134 Copied :: Output => shown. text ,
@@ -83,13 +146,19 @@ fn view(cx: &Rc<DialogCx>, tui: Weak<Tui>, live: &Rc<Live>, page: Theme) -> View
83146 } )
84147 } ;
85148
86- let title = Text :: new ( app, "execute" ) . fg ( cx. theme . text . base ) . bold ( ) . grow ( 1.0 ) ;
87- let status = Text :: new ( app, "" ) ;
149+ let ( label, title) = {
150+ let shown = live. shown ( ) ;
151+ ( shown. label , shown. title )
152+ } ;
153+ let title =
154+ Text :: new ( app, title) . fg ( cx. theme . text . base ) . bold ( ) . grow ( 1.0 ) . min_width ( 0 ) . wrap ( WrapMode :: None ) . truncate ( true ) ;
155+ let status = Text :: new ( app, "" ) . shrink ( 0.0 ) ;
88156 let head = header (
89157 cx,
90158 HeaderProps {
91- title_view : Some ( View :: new ( app) . row ( ) . gap ( 2 ) . grow ( 1.0 ) . child ( & title) . child ( & status) . as_any ( ) ) ,
159+ title_view : Some ( View :: new ( app) . row ( ) . gap ( 2 ) . grow ( 1.0 ) . min_width ( 0 ) . child ( & title) . child ( & status) . as_any ( ) ) ,
92160 gap : Some ( 2 ) ,
161+ on_close : live. back . clone ( ) ,
93162 ..HeaderProps :: default ( )
94163 } ,
95164 ) ;
@@ -106,7 +175,7 @@ fn view(cx: &Rc<DialogCx>, tui: Weak<Tui>, live: &Rc<Live>, page: Theme) -> View
106175 let ( status, scroll, foot, copy) = ( status. clone ( ) , scroll. clone ( ) , foot. clone ( ) , copy. clone ( ) ) ;
107176 Rc :: new ( move || {
108177 let ( Some ( cx) , Some ( live) ) = ( cx. upgrade ( ) , live. upgrade ( ) ) else { return } ;
109- let shown = Shown :: new ( & live. part . borrow ( ) ) ;
178+ let shown = live. shown ( ) ;
110179 status. set_text ( shown. status . clone ( ) ) ;
111180 status. fg ( if shown. failed { cx. theme . text . feedback . error . base } else { cx. theme . text . muted } ) ;
112181 let width = cx
@@ -123,21 +192,24 @@ fn view(cx: &Rc<DialogCx>, tui: Weak<Tui>, live: &Rc<Live>, page: Theme) -> View
123192 foot. add ( & footer (
124193 & cx,
125194 FooterProps {
126- primary : [ ( Copied :: Code , "copy code" , "c" ) , ( Copied :: Output , "copy output" , "o" ) ]
127- . into_iter ( )
128- . map ( |( kind, title, key) | {
129- let mut action = if live. copied . get ( ) == Some ( kind) {
130- let mut action = FooterAction :: new ( "✓ copied" ) ;
131- action. color = Some ( cx. theme . text . feedback . success . base ) ;
132- action
133- } else {
134- FooterAction :: new ( title) . key ( key)
135- } ;
136- let copy = copy. clone ( ) ;
137- action. on_trigger = Some ( Rc :: new ( move || copy ( kind) ) ) ;
195+ primary : [
196+ ( Copied :: Code , format ! ( "copy {}" , label. to_lowercase( ) ) , "c" ) ,
197+ ( Copied :: Output , "copy output" . to_owned ( ) , "o" ) ,
198+ ]
199+ . into_iter ( )
200+ . map ( |( kind, title, key) | {
201+ let mut action = if live. copied . get ( ) == Some ( kind) {
202+ let mut action = FooterAction :: new ( "✓ copied" ) ;
203+ action. color = Some ( cx. theme . text . feedback . success . base ) ;
138204 action
139- } )
140- . collect ( ) ,
205+ } else {
206+ FooterAction :: new ( title) . key ( key)
207+ } ;
208+ let copy = copy. clone ( ) ;
209+ action. on_trigger = Some ( Rc :: new ( move || copy ( kind) ) ) ;
210+ action
211+ } )
212+ . collect ( ) ,
141213 ..FooterProps :: default ( )
142214 } ,
143215 ) ) ;
@@ -174,14 +246,26 @@ fn view(cx: &Rc<DialogCx>, tui: Weak<Tui>, live: &Rc<Live>, page: Theme) -> View
174246 } )
175247 . title( "Scroll to output" )
176248 . group( "Execute" ) ,
177- TuiCommand :: inline( "c" , copy_command( Copied :: Code ) ) . title( "Copy code" ) . group( "Execute" ) ,
249+ TuiCommand :: inline( "c" , copy_command( Copied :: Code ) )
250+ . title( format!( "Copy {}" , label. to_lowercase( ) ) )
251+ . group( "Execute" ) ,
178252 TuiCommand :: inline( "o" , copy_command( Copied :: Output ) ) . title( "Copy output" ) . group( "Execute" ) ,
179253 ] ) ) ;
254+ if let Some ( back) = live. back . clone ( ) {
255+ cx. layer ( KeymapLayer :: new ( vec ! [
256+ TuiCommand :: inline( "escape" , move |_, _| back( ) ) . title( "Back" ) . group( "Dialog" ) ,
257+ ] ) ) ;
258+ }
180259
181260 opencode_ui:: dialog:: layout_view ( cx, & head, & body, Some ( & foot) , DIALOG_INSET , 0 )
182261}
183262
263+ #[ derive( Clone ) ]
184264struct Shown {
265+ title : String ,
266+ /// The body's heading and language.
267+ label : & ' static str ,
268+ filetype : Option < & ' static str > ,
185269 code : Option < String > ,
186270 text : Option < String > ,
187271 /// The output split into its JSON result and the warnings and logs after it.
@@ -238,6 +322,9 @@ impl Shown {
238322 _ => format ! ( "Completed{took}" ) ,
239323 } ;
240324 Self {
325+ title : "execute" . to_owned ( ) ,
326+ label : "Code" ,
327+ filetype : Some ( "typescript" ) ,
241328 highlighted : text. as_deref ( ) . and_then ( highlighted) ,
242329 code,
243330 text,
@@ -271,10 +358,11 @@ fn content(app: &App, theme: &Theme, syntax: &Arc<SyntaxTheme>, shown: &Shown, w
271358 let digits = shown. digits ( ) ;
272359 let code_width = width. saturating_sub ( digits + 1 ) . max ( 1 ) ;
273360 let heading = |label : & str | Text :: new ( app, label) . fg ( theme. text . muted ) . bold ( ) ;
274- let code = View :: new ( app) . column ( ) . child ( & heading ( "Code" ) ) ;
275- match & shown. code {
276- Some ( value) => code. add ( & guttered ( app, theme, syntax, value, "typescript" , digits, code_width) ) ,
277- None => code. add ( & Text :: new ( app, "Waiting for code…" ) . fg ( theme. text . muted ) ) ,
361+ let code = View :: new ( app) . column ( ) . child ( & heading ( shown. label ) ) ;
362+ match ( & shown. code , shown. filetype ) {
363+ ( Some ( value) , Some ( filetype) ) => code. add ( & guttered ( app, theme, syntax, value, filetype, digits, code_width) ) ,
364+ ( Some ( value) , None ) => code. add ( & Text :: new ( app, value. clone ( ) ) . fg ( theme. text . base ) . wrap ( WrapMode :: Word ) ) ,
365+ ( None , _) => code. add ( & Text :: new ( app, "Waiting for code…" ) . fg ( theme. text . muted ) ) ,
278366 } ;
279367 let output = View :: new ( app) . column ( ) . child ( & heading ( "Output" ) ) ;
280368 let text_fg = if shown. failed { theme. text . feedback . error . base } else { theme. text . base } ;
@@ -303,7 +391,7 @@ fn content(app: &App, theme: &Theme, syntax: &Arc<SyntaxTheme>, shown: &Shown, w
303391
304392/// `GutteredCode`: highlighted code, wrapped by character, after one left-aligned gutter of line numbers
305393/// that leaves continuation rows unnumbered.
306- fn guttered (
394+ pub ( crate ) fn guttered (
307395 app : & App ,
308396 theme : & Theme ,
309397 syntax : & Arc < SyntaxTheme > ,
0 commit comments