in src/base-commands/api.ts [24:69]
protected async printImages(images: any[], field: string[] | undefined, thumbnail: boolean, outputFile?: string | undefined) {
const outputStream = outputFile ? createWriteStream(outputFile) : undefined
const writeOutput = (o: string): void => {
if (outputStream) {
outputStream.write(o + '\n')
} else {
process.stdout.write(o + '\n')
}
}
const output = Promise.all(images.map(async image => {
const out = field === undefined ? JSON.stringify(image, null, 2) :
field.map(f => {
const v = get(image, f) ?? get(image.data, f)
if (v) return v
const links = ((image.links || []) as unknown as { rel: string, href: string }[])
const link = links.find(({ rel }) => rel === f)
if (link) {
return terminalLink(link.rel, link.href)
}
throw new Error(`No field named ${f} for image ${image}`)
}).join('\t')
if (!thumbnail) {
return [out]
}
const url = image?.data?.thumbnail?.secureUrl ?? ''
try {
const resp = await this.http!.get(new URL(url))
const buffer = await resp.buffer()
const thumb = await terminalImage.buffer(buffer, { height: '20%' })
return [out, thumb]
} catch {
return [out]
}
}))
for (const [fields, thumb] of await output) {
writeOutput(fields)
if (thumb) {
writeOutput(thumb)
}
}
}