Skip to content

Commit

Permalink
feat: log dividers with terminal width
Browse files Browse the repository at this point in the history
  • Loading branch information
Orzelius committed May 25, 2023
1 parent 23cc536 commit 2ea3732
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 3 additions & 9 deletions core/src/cli/command-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import { findProjectConfig } from "../config/base"
import { toGardenError } from "../exceptions"
import { Garden } from "../garden"
import type { Log } from "../logger/log-entry"
import { getRootLogger } from "../logger/logger"
import { renderDivider } from "../logger/util"
import { getTermWidth, renderDivider } from "../logger/util"
import { getGardenForRequest } from "../server/commands"
import type { GardenInstanceManager } from "../server/instance-manager"
import { TypedEventEmitter } from "../util/events"
Expand Down Expand Up @@ -457,15 +456,10 @@ export class CommandLine extends TypedEventEmitter<CommandLineEvents> {
this.renderCommandLine()
}

getTermWidth() {
// TODO: accept stdout in constructor
return process.stdout?.columns || 100
}

private printWithDividers(text: string, title: string) {
let width = max(text.split("\n").map((l) => stringWidth(l.trimEnd()))) || 0
width += 2
const termWidth = this.getTermWidth()
const termWidth = getTermWidth()
const minWidth = stringWidth(title) + 10

if (width > termWidth) {
Expand Down Expand Up @@ -668,7 +662,7 @@ ${chalk.white.underline("Keys:")}
opts: ParameterValues<any>
}) {
const id = uuidv4()
const width = this.getTermWidth() - 2
const width = getTermWidth() - 2

const prepareParams = {
log: this.log,
Expand Down
10 changes: 9 additions & 1 deletion core/src/logger/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,17 @@ const getNumberOfCharsPerWidth = (char: string, width: number) => width / string

// Adapted from https://github.com/JureSotosek/ink-divider
export function renderDivider({
width = 80,
width = undefined,
char = "─",
titlePadding = 1,
color,
title,
padding = 0,
}: DividerOpts = {}) {
const pad = " "
if (!width) {
width = getTermWidth()
}

if (!color) {
color = chalk.white
Expand All @@ -124,6 +127,11 @@ export function renderDivider({
return paddingString + dividerSideString + titleString + dividerSideString + paddingString
}

export const getTermWidth = () => {
// TODO: accept stdout as param
return process.stdout?.columns || 100
}

export function renderDuration(duration: number): string {
return `(took ${duration} sec)`
}
Expand Down

0 comments on commit 2ea3732

Please sign in to comment.