Thread: Basic Cross-Platform Console Formatting Function

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    19

    Basic Cross-Platform Console Formatting Function

    Hi,

    I'm trying to write a function that prints text to the console in a custom format. I'm trying to make it cross-platform, and I am developing on Windows. Right now, all I really care about is that the text is underlined. So far, I have:
    Code:
    void print_underlined(char* str) {
    	#ifdef _WIN32
    	static HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleTextAttribute(handle,COMMON_LVB_UNDERSCORE|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
    	printf("%s",str);
    	SetConsoleTextAttribute(handle,FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
    
    	#else
    	printf("\033[4;37;40m%s\033[4;37;40m",str);
    
    	#endif
    }
    By playing with some of the arguments in SetConsoleTextAttribute in the first block, I can get different colored font, but for some reason it just is never underlined.

    Also, the #else code doesn't work for me (it oughtn't to; as before, I'm on Windows). I was hoping perhaps someone could let me know if it does.

    Thanks!
    Ian
    Last edited by Geometrian; 08-28-2011 at 09:19 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Wrong forum/section methinks. In the future, post C in the C section, Windows (C or C++) in the Windows section.
    This is Windows (and C).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Windows doesn't support ANSI escape sequences (your else code) as you discovered. You also can't underline text in the console without puttting a line of hyphens or a similar character on the line underneath. That is your only option.

    COMMON_LVB_UNDERSCORE is for CJK characters.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    For any cross-platform console programming you need an external library. The only standard things are input/output streams. These can be plugged into the console, but not necessarily.

    Quote Originally Posted by Elysia View Post
    Wrong forum/section methinks. In the future, post C in the C section, Windows (C or C++) in the Windows section.
    This is Windows (and C).
    Hm? Given this piece of code, you cannot determine whether this is pure C, or C++. He is also asking about cross-platform solutions.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    Quote Originally Posted by kmdv View Post
    For any cross-platform console programming you need an external library.
    Ahhh. Can you suggest some? Ideally, I'd like something simple and lightweight.
    you cannot determine whether this is pure C, or C++. He is also asking about cross-platform solutions.
    Yes, this is C++. I posted in C++ because I didn't want to exclude any potential C++ solutions.
    Quote Originally Posted by adeyblue View Post
    You also can't underline text in the console without puttting a line of hyphens or a similar character on the line underneath. That is your only option.
    I had considered this. In my case this is extremely inconvenient, and I would much rather do underlining if possible.
    Quote Originally Posted by adeyblue View Post
    COMMON_LVB_UNDERSCORE is for CJK characters.
    I see. That's good to know. Thanks.

    Ian

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Ahhh. Can you suggest some? Ideally, I'd like something simple and lightweight.
    Maybe ncurses ..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy cross platform dev
    By Witis in forum Tech Board
    Replies: 6
    Last Post: 01-30-2011, 09:56 AM
  2. Looking for info: Cross-Platform
    By AvidGamer in forum C++ Programming
    Replies: 7
    Last Post: 09-13-2010, 10:19 AM
  3. cross-platform issues
    By Aisthesis in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2009, 08:11 AM
  4. Cross-Platform GUI
    By @nthony in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2007, 02:51 PM
  5. Cross-platform code...
    By Sul in forum C++ Programming
    Replies: 10
    Last Post: 06-18-2004, 04:44 PM

Tags for this Thread