@@ -14,7 +14,7 @@ use crossterm::{
1414 execute,
1515 terminal:: { disable_raw_mode, enable_raw_mode, EnterAlternateScreen , LeaveAlternateScreen } ,
1616} ;
17- use std:: { error:: Error , io} ;
17+ use std:: { env , error:: Error , io, process :: Command } ;
1818use tui:: {
1919 backend:: { Backend , CrosstermBackend } ,
2020 layout:: { Constraint , Direction , Layout , Rect } ,
@@ -114,13 +114,14 @@ fn main() -> Result<(), Box<dyn Error>> {
114114 if !informations. is_empty ( ) {
115115 app. messages = informations
116116 . iter ( )
117- . map ( |amessage| amessage. urls . clone ( ) )
117+ . map ( |amessage| spider :: remove_quotation ( amessage. ps . clone ( ) ) )
118118 . collect ( ) ;
119119 app. stateoflist = true ;
120120 app. state . select ( Some ( 0 ) ) ;
121121 app. index = Some ( 0 ) ;
122122 app. informations = informations;
123123 }
124+ app. v2ray_input = utils:: start_v2core ( ) ;
124125 let res = run_app ( & mut terminal, app) ;
125126
126127 // restore terminal
@@ -160,6 +161,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
160161 app. show_popup = true ;
161162 app. input_mode = InputMode :: Popup ;
162163 }
164+
163165 _ => { }
164166 } ,
165167 InputMode :: Editing => match key. code {
@@ -171,7 +173,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
171173 storge. push ( '[' ) ;
172174 storge. push ( '\n' ) ;
173175 if !list[ 0 ] . is_empty ( ) {
174- app. messages = list[ 0 ] . clone ( ) ;
176+ // app.messages = list[0].clone();
175177 app. stateoflist = true ;
176178 app. state . select ( Some ( 0 ) ) ;
177179 app. index = Some ( 0 ) ;
@@ -180,15 +182,20 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
180182 app. informations . push ( information. clone ( ) ) ;
181183 storge. push_str ( information. get_the_json_node ( ) . as_str ( ) ) ;
182184 }
185+ app. messages = app
186+ . informations
187+ . iter ( )
188+ . map ( |ainformation| {
189+ spider:: remove_quotation ( ainformation. ps . clone ( ) )
190+ } )
191+ . collect ( ) ;
183192 }
184193 storge. pop ( ) ;
185194 storge. pop ( ) ;
186195 storge. push ( '\n' ) ;
187196 storge. push ( ']' ) ;
188- if let Err ( err) = utils:: create_json_file ( utils:: Save :: Storage , storge)
189- {
190- panic ! ( "err {}" , err) ;
191- } ;
197+ utils:: create_json_file ( utils:: Save :: Storage , storge)
198+ . unwrap_or_else ( |err| panic ! ( "err {}" , err) ) ;
192199 }
193200
194201 //app.messages.push(app.input.drain(..).collect());
@@ -213,7 +220,29 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
213220 KeyCode :: Esc => {
214221 app. input_mode = InputMode :: Normal ;
215222 }
216-
223+ KeyCode :: F ( 5 ) => {
224+ if let Some ( index) = app. index {
225+ let home = env:: var ( "HOME" ) . unwrap ( ) ;
226+ utils:: create_json_file (
227+ utils:: Save :: Running ,
228+ app. informations [ index] . clone ( ) . running_json ( ) ,
229+ )
230+ . unwrap_or_else ( |err| panic ! ( "err {}" , err) ) ;
231+ Command :: new ( "pkill" ) . arg ( "v2ray" ) . output ( ) . unwrap_or_else (
232+ |e| panic ! ( "failed to execute process: {}" , e) ,
233+ ) ;
234+ Command :: new ( "nohup" )
235+ . arg ( app. v2ray_input . clone ( ) )
236+ . arg ( "-config" )
237+ . arg ( home. clone ( ) + "/.config/tv2ray/running.json" )
238+ . arg ( ">" )
239+ . arg ( home + "/.config/tv2ray/test.log" )
240+ . arg ( "2>&1" )
241+ . arg ( "&" )
242+ . spawn ( )
243+ . expect ( "failed" ) ;
244+ }
245+ }
217246 _ => { }
218247 }
219248 } else {
@@ -228,6 +257,19 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
228257 KeyCode :: Char ( 'e' ) => {
229258 app. input_mode = InputMode :: PopupEdit ;
230259 }
260+ KeyCode :: Char ( 's' ) => {
261+ if let Err ( err) = utils:: create_json_file (
262+ utils:: Save :: V2ray ,
263+ format ! (
264+ "{{
265+ \" v2core\" :\" {}\"
266+ }}" ,
267+ app. v2ray_input
268+ ) ,
269+ ) {
270+ panic ! ( "{}" , err) ;
271+ }
272+ }
231273 _ => { }
232274 } ,
233275 InputMode :: PopupEdit => {
@@ -372,18 +414,49 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
372414 InputMode :: PopupEdit => Style :: default ( ) . fg ( Color :: Yellow ) ,
373415 _ => Style :: default ( ) ,
374416 } )
375- . block ( Block :: default ( ) . borders ( Borders :: ALL ) . title ( "Settings " ) ) ;
417+ . block ( Block :: default ( ) . borders ( Borders :: ALL ) . title ( "v2ray-core " ) ) ;
376418 //f.render_widget(input, chunks[1]);
377419 let area = centered_rect ( 60 , 20 , f. size ( ) ) ;
420+ //let settings :Vec<Paragraph> = app
421+ // .settings
422+ // .iter()
423+ // .map(|asetting| Paragraph::new(app.v2ray_input.as_ref())
424+ // .style(Style::default())
425+ // .block(Block::default().borders(Borders::ALL).title(asetting.clone())))
426+
427+ // .collect();
428+
429+ let chunk = Layout :: default ( )
430+ . direction ( Direction :: Vertical )
431+ . margin ( 1 )
432+ . constraints (
433+ [
434+ Constraint :: Length ( 1 ) ,
435+ Constraint :: Length ( 3 ) ,
436+ Constraint :: Min ( 1 ) ,
437+ ]
438+ . as_ref ( ) ,
439+ )
440+ . split ( area) ;
441+
378442 f. render_widget ( Clear , area) ; //this clears out the background
379- f. render_widget ( inputpop, area) ;
443+ let ( msg, style) = (
444+ vec ! [ Span :: raw( "Settings, e to edit, s to save" ) ] ,
445+ Style :: default ( ) ,
446+ ) ;
447+ let mut text = Text :: from ( Spans :: from ( msg) ) ;
448+ text. patch_style ( style) ;
449+ let title = Paragraph :: new ( text) ;
450+ f. render_widget ( title, chunk[ 0 ] ) ;
451+ f. render_widget ( inputpop, chunk[ 1 ] ) ;
452+
380453 if let InputMode :: PopupEdit = app. input_mode {
381454 // Make the cursor visible and ask tui-rs to put it at the specified coordinates after rendering
382455 f. set_cursor (
383456 // Put cursor past the end of the input text
384- area . x + app. v2ray_input . width ( ) as u16 + 1 ,
457+ chunk [ 1 ] . x + app. v2ray_input . width ( ) as u16 + 1 ,
385458 // Move one line down, from the border to the input line
386- area . y + 1 ,
459+ chunk [ 1 ] . y + 1 ,
387460 )
388461 //InputMode::Normal | InputMode::Select | InputMode::Popup =>
389462 // Hide the cursor. `Frame` does this by default, so we don't need to do anything here
0 commit comments