src/commands/logs.js
/**
   * Prompt the user to choose a log from the internally kept history of logs. The list is shown with recent logs
   * first but a number of logs can be excluded of the end by provding a "reverseOffset"
   *
   * @param {integer} reverseOffset The number of logs to skip from the end of the logs list
   * @param {integer} logCount The number of logs to show to the user to choose from
   * @returns {Promise<LogEntry|null>} A promise that resolves to the log entry the user chose or null if cancelled
   */
Prompt the user to choose a log from the internally kept history of logs. The list is shown with recent logs
first but a number of logs can be excluded of the end by provding a "reverseOffset"

@param {integer} reverseOffset The number of logs to skip from the end of the logs list
@param {integer} logCount The number of logs to show to the user to choose from
@returns {Promise} A promise that resolves to the log entry the user chose or null if cancelled
Prompt the user to choose a log from the internally kept history of logs . The list is shown with recent logs first but a number of logs can be excluded of the end by provding a " reverseOffset " @param { integer } reverseOffset The number of logs to skip from the end of the logs list @param { integer } logCount The number of logs to show to the user to choose from @returns { Promise } A promise that resolves to the log entry the user chose or null if cancelled
async promptForLog(reverseOffset = 0, logCount = 10) {
    const {flags: {utc}} = this.parse(LogsCommand)
    const logs = reverseOffset > 0 ? this.logs.slice(0, this.logs.length - reverseOffset) : this.logs
    const logsToPresent = logs.length <= logCount ? logs : logs.slice(this.logs.length - logCount)
    const choices = logsToPresent.map((entry, index) => ({name: entry.formattedSummary(utc), value: index}))
    if (logs.length > logCount) {
      choices.unshift({name: 'View more...', value: -1})
    }
    choices.push({name: 'Cancel', value: false})
    this.spinner(null)
    this.clearScreen()
    const {response} = await inquirer.prompt([{
      type: 'list',
      name: 'response',
      message: 'Choose an entry to view:',
      choices,
      default: choices.length - 2,
      pageSize: choices.length,
    }])
    if (response === false) {
      return null
    }
    if (response < 0) {
      return this.promptForLog(reverseOffset + logCount, logCount)
    }
    return logsToPresent[response]
  }
async promptForLog ( reverseOffset = 0 , logCount = 10 ) { const { flags : { utc } } = this . parse ( LogsCommand ) const logs = reverseOffset > 0 ? this . logs . slice ( 0 , this . logs . length - reverseOffset ) : this . logs const logsToPresent = logs . length <= logCount ? logs : logs . slice ( this . logs . length - logCount ) const choices = logsToPresent . map ( ( entry , index ) => ( { name : entry . formattedSummary ( utc ) , value : index } ) ) if ( logs . length > logCount ) { choices . unshift ( { name : 'View more...' , value : - 1 } ) } choices . push ( { name : 'Cancel' , value : false } ) this . spinner ( null ) this . clearScreen ( ) const { response } = await inquirer . prompt ( [ { type : 'list' , name : 'response' , message : 'Choose an entry to view:' , choices , default : choices . length - 2 , pageSize : choices . length , } ] ) if ( response === false ) { return null } if ( response < 0 ) { return this . promptForLog ( reverseOffset + logCount , logCount ) } return logsToPresent [ response ] }
Prompt the user to choose a log from the internally kept history of logs.
Prompt the user to choose a log from the internally kept history of logs .
// Fetch the UTC preference from command flags // Get the logs, taking off a given number from the end // Slice the specific logs that need to be presented // Map them into "choices" for inquirer // Add a "view more" option if there's more logs in the history // Add a "cancel" option // Stop the spinner and clear the screen // Show the user the list of logs (formatted the same way) // `false` means they chose "Cancel" // Check if the user chose "View more" // Return the log that was chosen