Skip to content

Add option to align title in box logs #389

@gperdomor

Description

@gperdomor

Describe the feature

box it's a great tool to add borders to logs, if you pass a title, this is centered.

Image

A new parameter to align title to left or right should be great to get something like this:

Image

For now I was able to do this using this portion of code:

const isEven = (num) => num % 2 === 0;

function printWarningBox(message) {
  // Calculate the padding needed to push the title to the left
  // The box width is determined by the message length or a minimum width
  const title = ' Warning ';
  const messageLength = message.length;
  const boxWidth = Math.max(messageLength, 10); // minimum box width of 10

  // Create a title with spaces to push it left
  // Add a few spaces after "Warning" to separate it from the border
  const finalTitle = title + '─'.repeat(boxWidth - (title.length + (isEven(boxWidth) ? 1 : 2)));

  consola.box({
    title: finalTitle,
    message: message,
    style: {
      borderColor: 'yellow',
    },
  });
}

printWarningBox('This is a warning message.');

Additional information

  • Would you be willing to help implement this feature?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions