@@ -3,7 +3,7 @@ use std::process::{Command, Stdio};
33
44use crate :: cmd:: prelude:: * ;
55use crate :: paper:: PaperList ;
6- use crate :: utils:: { confirm, expand_tilde_string } ;
6+ use crate :: utils:: confirm;
77
88pub static MAN : & ' static str = "Usage:
991) alone: open [filter]
@@ -61,9 +61,10 @@ pub fn execute(
6161 // Some reports.
6262 let num_open = files. len ( ) ;
6363 println ! (
64- "Skipping papers without filepaths ({} out of {})." ,
64+ "{} {} selected. Skipping {} without file paths." ,
65+ num_papers,
66+ if num_papers > 1 { "papers" } else { "paper" } ,
6567 num_papers - num_open,
66- num_papers
6768 ) ;
6869
6970 // Ask for confirmation.
@@ -72,37 +73,54 @@ pub fn execute(
7273 }
7374
7475 // Open papers.
75- let mut selected = Vec :: new ( ) ;
7676 if config. output . viewer_batch {
77- // let command = build_viewer_command(files, config);
78- // spawn(command, // TODO: handle batched.
77+ let ( selected, files) : ( Vec < usize > , Vec < PathBuf > ) = files. into_iter ( ) . unzip ( ) ;
78+ if spawn ( build_viewer_command ( files. as_ref ( ) , config) ) {
79+ Ok ( CommandOutput :: Papers ( PaperList ( selected) ) )
80+ } else {
81+ Ok ( CommandOutput :: Papers ( PaperList ( Vec :: new ( ) ) ) )
82+ }
7983 } else {
84+ let mut selected = Vec :: new ( ) ;
8085 for ( i, file) in files. into_iter ( ) {
81- let command = build_viewer_command ( & [ file] , config) ;
82- spawn ( command, i, & mut selected) ;
86+ if spawn ( build_viewer_command ( & [ file] , config) ) {
87+ selected. push ( i) ;
88+ }
8389 }
90+ Ok ( CommandOutput :: Papers ( PaperList ( selected) ) )
8491 }
85-
86- Ok ( CommandOutput :: Papers ( PaperList ( selected) ) )
8792}
8893
89- fn spawn ( mut command : Command , i : usize , selected : & mut Vec < usize > ) {
94+ fn spawn ( mut command : Command ) -> bool {
9095 match command. spawn ( ) {
91- Ok ( _) => selected . push ( i ) ,
96+ Ok ( _) => true ,
9297 Err ( e) => {
9398 if matches ! ( e. kind( ) , std:: io:: ErrorKind :: NotFound ) {
9499 println ! ( "Invalid editor command: '{:?}'" , e) ;
95100 } else {
96101 println ! ( "Failed to spawn subprocess: '{:?}'" , e) ;
97102 }
103+ false
98104 }
99105 }
100106}
101107
102108fn build_viewer_command ( files : & [ PathBuf ] , config : & Config ) -> Command {
103- let command = & config. output . viewer_command ;
104- let mut ret = Command :: new ( & command[ 0 ] ) ;
105- ret. args ( & command[ 1 ..] ) . args ( files) ;
106- ret. stdin ( Stdio :: null ( ) ) . stdout ( Stdio :: null ( ) ) . stderr ( Stdio :: null ( ) ) ;
109+ let mut ret = Command :: new ( & config. output . viewer_command [ 0 ] ) ;
110+ let mut curly = false ;
111+ for command in & config. output . viewer_command [ 1 ..] {
112+ if command == "{}" {
113+ ret. args ( files) ;
114+ curly = true ;
115+ } else {
116+ ret. arg ( command) ;
117+ }
118+ }
119+ if !curly {
120+ ret. args ( files) ;
121+ }
122+ ret. stdin ( Stdio :: null ( ) )
123+ . stdout ( Stdio :: null ( ) )
124+ . stderr ( Stdio :: null ( ) ) ;
107125 ret
108126}
0 commit comments