Skip to content

Commit

Permalink
pp
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Sep 9, 2024
1 parent 517fdfe commit 9b105ac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Command } from 'commander'
import { readFileSync } from 'fs'
import { run } from '~/shell'
import { exit, parseAndRemoveWildcardOptions } from '~/utils'
import { exit, parseAndRemoveWildcardOptions, pp } from '~/utils'
import { StaxConfig } from '~/types'
import Stax from '~/stax'
import tmp from 'tmp'
Expand All @@ -26,8 +26,7 @@ program.command('config')
.description('Show config variables for the container.')
.action((name, options) => {
const container = stax.findContainer(name, options)
const attributes = { ...container.config, labels: container.labels }
console.log(attributes)
pp({ ...container.config, labels: container.labels })
})

program.command('copy')
Expand Down
22 changes: 22 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { existsSync, statSync } from 'fs'
import tmp from 'tmp'
import icons from './icons'
import yaml from 'js-yaml'
import chalk from 'chalk'

// Parses a CSV string into a key/value object (e.g. "a=b,c=d" => { a: "b", c: "d" })
export function csvKeyValuePairs(csv: string = '') {
Expand Down Expand Up @@ -106,3 +108,23 @@ export function getNonNullProperties(obj: Record<string, any>): Record<string, a

return result
}

export function pp(object) {
const yamlString = yaml.dump(object)
const lines = yamlString.split('\n')
let currentIndent = 0

const coloredLines = lines.map(line => {
const match = line.match(/^(\s*)(\S+):(.*)$/)
if (match) {
const [, indent, key, value] = match
currentIndent = indent.length
const bar = chalk.gray('│ ').repeat(currentIndent / 2)
return `${bar}${chalk.cyan(key)}${chalk.gray(':')}${value}`
} else {
const bar = chalk.gray('│ ').repeat((currentIndent + 2) / 2)
return `${bar}${line.trim()}`
}
})
console.log(coloredLines.slice(0, coloredLines.length-1).join('\n'))
}

0 comments on commit 9b105ac

Please sign in to comment.