pub fn document(&self, cmd_to_doc: Option<String>) -> Result<String, String> {
        // Handle metadata about the whole file first with a preamble
        let mut meta = format!(
            "This is the help page for a configuration file. If you'd like help about Bonnie generally, run `bonnie -h` instead.
Version: {}",
            self.version,
        );
        // Environment variable files
        let mut env_files = Vec::new();
        for env_file in &self.env_files {
            env_files.push(format!("    {}", env_file));
        }
        if !env_files.is_empty() {
            meta += &format!("\nEnvironment variable files:\n{}", env_files.join("\n"));
        }

        let msg;
        if let Some(cmd_name) = cmd_to_doc {
            let cmd = self.scripts.get(&cmd_name);
            let cmd = match cmd {
                Some(cmd) => cmd,
                None => return Err(format!("Command '{}' not found. You can see all supported commands by running `bonnie help`.", cmd_name))
            };
            msg = cmd.document(&cmd_name);
        } else {
            // Loop through every command and document it
            let mut msgs = Vec::new();
            // Sort the subcommands alphabetically
            let mut cmds: Vec<(&String, &Command)> = self.scripts.iter().collect();
            cmds.sort_by(|(name, _), (name2, _)| name.cmp(name2));
            for (cmd_name, cmd) in cmds {
                msgs.push(cmd.document(cmd_name));
            }

            msg = msgs.join("\n");
        }
        // Space everything out evenly based on the longest command name (thing on the left)
        // First, we get the longest command name (thing on the left of where tabs will end up)
        // We loop through each line because otherwise subcommands stuff things up
        let mut longest_left: usize = 0;
        for line in msg.lines() {
            // Get the length of the stuff to the left of the tabs placeholder
            let left_len = line.split("{TABS}").collect::<Vec<&str>>()[0].len();
            if left_len > longest_left {
                longest_left = left_len;
            }
        }
        // Now we loop back through each line and add the appropriate amount of space
        let mut spaced_msg_lines = Vec::new();
        for line in msg.lines() {
            let left_len = line.split("{TABS}").collect::<Vec<&str>>()[0].len();
            // We want the longest line to have 4 spaces, then the rest should have (longest - length + 4) spaces
            let spaces = " ".repeat(longest_left - left_len + 4);
            spaced_msg_lines.push(line.replace("{TABS}", &spaces));
        }
        let spaced_msg = spaced_msg_lines.join("\n");

        Ok(format!("{}\n\n{}", meta, spaced_msg))
    }
// Provides a documentation message for this configuration
// If a single command name is given, only it will be documented
Provides a documentation message for this configuration
If a single command name is given, only it will be documented
Provides a documentation message for this configuration If a single command name is given only it will be documented
pub fn document(&self, cmd_to_doc: Option<String>) -> Result<String, String> {
        // Handle metadata about the whole file first with a preamble
        let mut meta = format!(
            "This is the help page for a configuration file. If you'd like help about Bonnie generally, run `bonnie -h` instead.
Version: {}",
            self.version,
        );
        // Environment variable files
        let mut env_files = Vec::new();
        for env_file in &self.env_files {
            env_files.push(format!("    {}", env_file));
        }
        if !env_files.is_empty() {
            meta += &format!("\nEnvironment variable files:\n{}", env_files.join("\n"));
        }
        let msg;
        if let Some(cmd_name) = cmd_to_doc {
            let cmd = self.scripts.get(&cmd_name);
            let cmd = match cmd {
                Some(cmd) => cmd,
                None => return Err(format!("Command '{}' not found. You can see all supported commands by running `bonnie help`.", cmd_name))
            };
            msg = cmd.document(&cmd_name);
        } else {
            // Loop through every command and document it
            let mut msgs = Vec::new();
            // Sort the subcommands alphabetically
            let mut cmds: Vec<(&String, &Command)> = self.scripts.iter().collect();
            cmds.sort_by(|(name, _), (name2, _)| name.cmp(name2));
            for (cmd_name, cmd) in cmds {
                msgs.push(cmd.document(cmd_name));
            }
            msg = msgs.join("\n");
        }
        // Space everything out evenly based on the longest command name (thing on the left)
        // First, we get the longest command name (thing on the left of where tabs will end up)
        // We loop through each line because otherwise subcommands stuff things up
        let mut longest_left: usize = 0;
        for line in msg.lines() {
            // Get the length of the stuff to the left of the tabs placeholder
            let left_len = line.split("{TABS}").collect::<Vec<&str>>()[0].len();
            if left_len > longest_left {
                longest_left = left_len;
            }
        }
        // Now we loop back through each line and add the appropriate amount of space
        let mut spaced_msg_lines = Vec::new();
        for line in msg.lines() {
            let left_len = line.split("{TABS}").collect::<Vec<&str>>()[0].len();
            // We want the longest line to have 4 spaces, then the rest should have (longest - length + 4) spaces
            let spaces = " ".repeat(longest_left - left_len + 4);
            spaced_msg_lines.push(line.replace("{TABS}", &spaces));
        }
        let spaced_msg = spaced_msg_lines.join("\n");
        Ok(format!("{}\n\n{}", meta, spaced_msg))
    }
pub fn document ( & self , cmd_to_doc : Option < String > ) -> Result < String , String > { // Handle metadata about the whole file first with a preamble let mut meta = format ! ( " " self version ) ; // Environment variable files let mut env_files = Vec :: new ( ) ; for env_file in & self . env_files { env_files . push ( format ! ( " " env_file ) ) ; } if ! env_files . is_empty ( ) { meta += & format ! ( " \n \n " env_files join ( " \n " ) ) ; } let msg ; if let Some ( cmd_name ) = cmd_to_doc { let cmd = self . scripts . get ( & cmd_name ) ; let cmd = match cmd { Some ( cmd ) => cmd , None => return Err ( format ! ( " " cmd_name ) ) } ; msg = cmd . document ( & cmd_name ) ; } else { // Loop through every command and document it let mut msgs = Vec :: new ( ) ; // Sort the subcommands alphabetically let mut cmds : Vec < ( & String , & Command ) > = self . scripts . iter ( ) . collect ( ) ; cmds . sort_by ( | ( name , _ ) , ( name2 , _ ) | name . cmp ( name2 ) ) ; for ( cmd_name , cmd ) in cmds { msgs . push ( cmd . document ( cmd_name ) ) ; } msg = msgs . join ( " \n " ) ; } // Space everything out evenly based on the longest command name (thing on the left) // First, we get the longest command name (thing on the left of where tabs will end up) // We loop through each line because otherwise subcommands stuff things up let mut longest_left : usize = 0 ; for line in msg . lines ( ) { // Get the length of the stuff to the left of the tabs placeholder let left_len = line . split ( " " ) . collect :: < Vec < & str > > ( ) [ 0 ] . len ( ) ; if left_len > longest_left { longest_left = left_len ; } } // Now we loop back through each line and add the appropriate amount of space let mut spaced_msg_lines = Vec :: new ( ) ; for line in msg . lines ( ) { let left_len = line . split ( " " ) . collect :: < Vec < & str > > ( ) [ 0 ] . len ( ) ; // We want the longest line to have 4 spaces, then the rest should have (longest - length + 4) spaces let spaces = " " . repeat ( longest_left - left_len + 4 ) ; spaced_msg_lines . push ( line . replace ( " " , & spaces ) ) ; } let spaced_msg = spaced_msg_lines . join ( " \n " ) ; Ok ( format ! ( " \n \n " meta spaced_msg ) ) }
Provides a documentation message for this configuration
If a single command name is given, only it will be documented
Provides a documentation message for this configuration If a single command name is given only it will be documented
&self
cmd_to_doc
Option<String>