UNPKG

@oclif/multi-stage-output

Version:

Terminal output for oclif commands with multiple stages

33 lines (32 loc) 1.86 kB
import { Box, Text } from 'ink'; import React from 'react'; const getSideDividerWidth = (width, titleWidth) => (width - titleWidth) / 2; const getNumberOfCharsPerWidth = (char, width) => width / char.length; const PAD = ' '; export function Divider({ dividerChar = '─', dividerColor = 'dim', padding = 1, terminalWidth = process.stdout.columns ?? 80, textColor, textPadding = 1, title = '', width = 50, }) { const titleString = title ? `${PAD.repeat(textPadding) + title + PAD.repeat(textPadding)}` : ''; const titleWidth = titleString.length; const widthToUse = width === 'full' ? // if the width is `full`, use the terminal width minus the padding and title padding terminalWidth - textPadding - padding : // otherwise, if the provided width is greater than the terminal width, use the terminal width minus the padding and title paddding width > terminalWidth ? terminalWidth - textPadding - padding : // otherwise, use the provided width width; // Don't render if the available width is less than the title width if (widthToUse < titleWidth) { return; } const dividerWidth = getSideDividerWidth(widthToUse, titleWidth); const numberOfCharsPerSide = getNumberOfCharsPerWidth(dividerChar, dividerWidth); const dividerSideString = dividerChar.repeat(numberOfCharsPerSide); const paddingString = PAD.repeat(padding); return (React.createElement(Box, { flexDirection: "row" }, React.createElement(Text, null, paddingString, React.createElement(Text, { color: dividerColor }, dividerSideString), React.createElement(Text, { color: textColor }, titleString), React.createElement(Text, { color: dividerColor }, dividerSideString), paddingString))); }