Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1081793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:12:49+00:00 2026-05-16T22:12:49+00:00

I discovered this while using ruby printf, but it also applies to C’s printf.

  • 0

I discovered this while using ruby printf, but it also applies to C’s printf.

If you include ANSI colour escape codes in an output string, it messes up the alignment.

Ruby:

ruby-1.9.2-head > printf "%20s\n%20s\n", "\033[32mGreen\033[0m", "Green"
      Green          # 6 spaces to the left of this one
               Green # correctly padded to 20 chars
 => nil

The same line in a C program produces the same output.

Is there anyway to get printf (or something else) to align output and not add spaces for non-printed characters?

Is this is a bug, or is there a good reason for it?

Update: Since printf can’t be relied upon to align data when there’s ANSI codes and wide chars, is there a best practice way of lining up coloured tabular data in the console in ruby?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:12:50+00:00Added an answer on May 16, 2026 at 10:12 pm

    I disagree with your characterization of ‘9 spaces after the green Green’. I use Perl rather than Ruby, but if I use a modification of your statement, printing a pipe symbol after the string, I get:

    perl -e 'printf "%20s|\n%20s|\n", "\033[32mGreen\033[0m", "Green";'
          Green|
                   Green|
    

    This shows to me that the printf() statement counted 14 characters in the string, so it prepended 6 spaces to produce 20 characters right-aligned. However, the terminal swallowed 9 of those characters, interpreting them as colour changes. So, the output appeared 9 characters shorter than you wanted it to. However, the printf() did not print 9 blanks after the first ‘Green’.


    Regarding the best practices for aligned output (with colourization), I think you’ll need to have each sized-and-aligned field surrounded by simple ‘%s’ fields which deal with the colourization:

    printf "%s%20.20s%s|%s%-10d%s|%s%12.12s%s|\n",
           co_green, column_1_data, co_plain,
           co_blue,  column_2_data, co_plain,
           co_red,   column_3_data, co_plain;
    

    Where, obviously, the co_XXXX variables (constants?) contain the escape sequences to switch to the named colour (and co_plain might be better as co_black). If it turns out that you don’t need colourization on some field, you can use the empty string in place of the co_XXXX variables (or call it co_empty).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I discovered this quite by accident while looking for a file with a number
Possible Duplicate: Python or Ruby Interpreter on iOS I just discovered this apps pypad
This question is already answered. I discovered what the problem is. But, I'm posting
While experimenting with this question on collections in Spring.NET , I discovered that Spring
This has no value as far as I'm concerned. I discovered this while researching
I'm using Entity Framework 4.1, and I'm trying to select an entity while also
I just discovered this operator and I was wondering if it is safe to
I just discovered this component and started working with it. I understand that the
I am working with zend just recently. I discovered this ViewScript decorator for form
I've seen a plugin for Vim called AutoClose (discovered from this post) which automatically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.