1- use super :: { NvimClient , NvimConnection } ;
1+ use std:: path:: Path ;
2+
3+ use super :: { NvimClient , NvimConnection , NvimWindow } ;
24use crate :: nvim:: BufferDirection ;
3- use crate :: types:: BuildConfiguration ;
45use anyhow:: Result ;
56use nvim_rs:: { Buffer , Window } ;
6- use tokio_stream:: { Stream , StreamExt } ;
7+ use tokio_stream:: StreamExt ;
78
89pub struct Logger < ' a > {
910 pub nvim : & ' a NvimClient ,
10- pub title : & ' a str ,
11- pub request : & ' a BuildConfiguration ,
11+ pub title : String ,
1212 pub buf : Buffer < NvimConnection > ,
1313 open_cmd : Option < String > ,
1414 current_line_count : Option < i64 > ,
1515}
1616
1717impl < ' a > Logger < ' a > {
18- pub fn new (
19- nvim : & ' a NvimClient ,
20- title : & ' a str ,
21- request : & ' a BuildConfiguration ,
22- direction : Option < BufferDirection > ,
23- ) -> Self {
18+ pub fn new ( nvim : & ' a NvimClient , title : String , direction : Option < BufferDirection > ) -> Self {
2419 let buf = Buffer :: new ( nvim. log_bufnr . into ( ) , nvim. inner ( ) . clone ( ) ) ;
2520 let open_cmd = direction. map ( |v| v. to_nvim_command ( nvim. log_bufnr ) ) ;
2621
2722 Self {
2823 nvim,
2924 title,
30- request,
3125 buf,
3226 open_cmd,
3327 current_line_count : None ,
3428 }
3529 }
3630
37- pub async fn log ( & mut self , msg : String ) -> Result < ( ) > {
38- let c = self . line_count ( ) . await ?;
31+ async fn clear ( & self ) -> Result < ( ) > {
32+ self . buf . set_lines ( 0 , -1 , false , vec ! [ ] ) . await ?;
33+ Ok ( ( ) )
34+ }
3935
40- self . buf . set_lines ( c, c + 1 , false , vec ! [ msg] ) . await ?;
41- self . current_line_count = Some ( c + 1 ) ;
36+ async fn get_line_count ( & ' a self ) -> Result < i64 > {
37+ Ok ( if let Some ( count) = self . current_line_count {
38+ count
39+ } else {
40+ match self . buf . line_count ( ) . await ? {
41+ 1 => 0 ,
42+ count => count,
43+ }
44+ } )
45+ }
4246
43- Ok ( ( ) )
47+ async fn get_open_cmd ( & self , direction : Option < BufferDirection > ) -> String {
48+ let direction =
49+ BufferDirection :: get_window_direction ( self . nvim , direction, self . nvim . log_bufnr ) ;
50+
51+ match direction. await {
52+ Ok ( open_command) => open_command,
53+ Err ( e) => {
54+ tracing:: error!( "Unable to convert value to string {e}" ) ;
55+ BufferDirection :: Horizontal . to_nvim_command ( self . nvim . log_bufnr )
56+ }
57+ }
4458 }
4559
46- async fn clear ( & self ) -> Result < ( ) > {
47- self . buf . set_lines ( 0 , -1 , false , vec ! [ ] ) . await ?;
60+ pub async fn log ( & mut self , msg : String , win : & Option < NvimWindow > ) -> Result < ( ) > {
61+ let mut c = self . get_line_count ( ) . await ?;
62+ let lines = msg
63+ . split ( "\n " )
64+ . map ( ToString :: to_string)
65+ . collect :: < Vec < String > > ( ) ;
66+ let lines_len = lines. len ( ) as i64 ;
67+
68+ self . buf
69+ . set_lines ( c, c + lines_len as i64 , false , lines)
70+ . await ?;
71+ c += lines_len;
72+
73+ self . current_line_count = Some ( c) ;
74+
75+ if let Some ( win) = win {
76+ win. set_cursor ( ( c, 0 ) ) . await ?;
77+ }
78+
4879 Ok ( ( ) )
4980 }
5081
51- pub async fn log_stream < S > ( & mut self , mut stream : S , clear : bool , open : bool ) -> Result < ( ) >
52- where
53- S : Stream < Item = String > + Unpin ,
54- {
55- self . set_running ( ) . await ?;
56-
57- let title = format ! (
58- "[{}] ------------------------------------------------------" ,
59- self . title
60- ) ;
82+ pub async fn log_build_stream < P : AsRef < Path > > (
83+ & mut self ,
84+ root : P ,
85+ args : & Vec < String > ,
86+ clear : bool ,
87+ open : bool ,
88+ ) -> Result < ( bool , Option < Window < NvimConnection > > ) > {
89+ let mut stream = crate :: xcode:: stream_build ( root, args) . await ?;
6190
6291 // TODO(nvim): close log buffer if it is open for new direction
6392 //
@@ -74,22 +103,50 @@ impl<'a> Logger<'a> {
74103 None
75104 } ;
76105
77- self . log ( title) . await ?;
78-
79106 let mut success = false ;
107+
108+ self . set_running ( ) . await ?;
109+
80110 while let Some ( line) = stream. next ( ) . await {
81111 line. contains ( "Succeed" ) . then ( || success = true ) ;
82- self . log ( line) . await ?;
83- if open {
84- win. as_ref ( )
85- . unwrap ( )
86- . set_cursor ( ( self . line_count ( ) . await ?, 0 ) )
87- . await ?;
88- }
112+
113+ self . log ( line, & win) . await ?;
89114 }
90115
91116 self . set_status_end ( success, open) . await ?;
92117
118+ Ok ( ( success, win) )
119+ }
120+
121+ async fn log_title ( & mut self ) -> Result < ( ) > {
122+ self . log ( self . title . clone ( ) , & None ) . await ?;
123+ Ok ( ( ) )
124+ }
125+
126+ async fn open_cmd ( & self ) -> String {
127+ let open_cmd = match self . open_cmd . as_ref ( ) {
128+ Some ( s) => s. clone ( ) ,
129+ None => self . get_open_cmd ( None ) . await ,
130+ } ;
131+ open_cmd
132+ }
133+
134+ pub async fn open_win ( & self ) -> Result < Window < NvimConnection > > {
135+ let open_cmd = self . open_cmd ( ) . await ;
136+
137+ self . nvim . exec ( & open_cmd, false ) . await ?;
138+ let win = self . nvim . get_current_win ( ) . await ?;
139+ self . nvim . exec ( "setl nu nornu so=9" , false ) . await ?;
140+ self . nvim . exec ( "wincmd w" , false ) . await ?;
141+
142+ Ok ( win)
143+ }
144+
145+ pub async fn set_running ( & mut self ) -> Result < ( ) > {
146+ self . log_title ( ) . await ?;
147+ self . nvim
148+ . exec ( "let g:xbase_watch_build_status='running'" , false )
149+ . await ?;
93150 Ok ( ( ) )
94151 }
95152
@@ -107,61 +164,11 @@ impl<'a> Logger<'a> {
107164 self . nvim
108165 . get_current_win ( )
109166 . await ?
110- . set_cursor ( ( self . line_count ( ) . await ?, 0 ) )
167+ . set_cursor ( ( self . get_line_count ( ) . await ?, 0 ) )
111168 . await ?;
112169 self . nvim . exec ( "call feedkeys('zt')" , false ) . await ?;
113170 }
114171 }
115172 Ok ( ( ) )
116173 }
117-
118- pub async fn set_running ( & mut self ) -> Result < ( ) > {
119- self . nvim
120- . exec ( "let g:xbase_watch_build_status='running'" , false )
121- . await ?;
122- Ok ( ( ) )
123- }
124-
125- pub async fn line_count ( & ' a self ) -> Result < i64 > {
126- Ok ( if let Some ( count) = self . current_line_count {
127- count
128- } else {
129- match self . buf . line_count ( ) . await ? {
130- 1 => 0 ,
131- count => count,
132- }
133- } )
134- }
135-
136- async fn get_open_cmd ( & self , direction : Option < BufferDirection > ) -> String {
137- let direction =
138- BufferDirection :: get_window_direction ( self . nvim , direction, self . nvim . log_bufnr ) ;
139-
140- match direction. await {
141- Ok ( open_command) => open_command,
142- Err ( e) => {
143- tracing:: error!( "Unable to convert value to string {e}" ) ;
144- BufferDirection :: Horizontal . to_nvim_command ( self . nvim . log_bufnr )
145- }
146- }
147- }
148-
149- pub async fn open_win ( & self ) -> Result < Window < NvimConnection > > {
150- let open_cmd = self . open_cmd ( ) . await ;
151-
152- self . nvim . exec ( & open_cmd, false ) . await ?;
153- let win = self . nvim . get_current_win ( ) . await ?;
154- self . nvim . exec ( "setl nu nornu so=9" , false ) . await ?;
155- self . nvim . exec ( "wincmd w" , false ) . await ?;
156-
157- Ok ( win)
158- }
159-
160- async fn open_cmd ( & self ) -> String {
161- let open_cmd = match self . open_cmd . as_ref ( ) {
162- Some ( s) => s. clone ( ) ,
163- None => self . get_open_cmd ( None ) . await ,
164- } ;
165- open_cmd
166- }
167174}
0 commit comments