OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-note / lib / cli / commands / search.js

44 lines · javascript ref:
/**
 * hn search — Search notes
 */

import { HnClient } from '../client.js'
import { table, log, error, jsonOut } from '../output.js'

export async function searchCommand (cmdArgs, flags) {
  const query = cmdArgs.join(' ')
  if (!query) {
    error('Usage: hn search <query> [--notebook <slug>]')
  }

  const client = new HnClient(flags)

  let path = `/api/search?q=${encodeURIComponent(query)}`
  if (flags.notebook) {
    path += `&notebook=${encodeURIComponent(flags.notebook)}`
  }

  const results = await client.get(path)

  if (flags.json) {
    jsonOut(results)
    return
  }

  if (results.length === 0) {
    log('No results found.')
    return
  }

  log(`Found ${results.length} result(s):\n`)

  table(results, [
    { key: 'notebook', label: 'NOTEBOOK', width: 16 },
    { key: 'id', label: 'ID', width: 16 },
    { key: 'title', label: 'TITLE', width: 30 },
    { key: 'snippet', label: 'SNIPPET', width: 50 }
  ])
}

export default searchCommand