C++Guns – RoboBlog

23.08.2012

Farbige Konsolenausgabe

Filed under: Allgemein — Tags: , , — Thomas @ 09:08

Farbe in der Konsole ist nicht schwer.
Beispiele in C und Fortran:


#include < unistd.h >

void resetFont() {
    if (isatty (fileno (stdout)))
        std::cout << "\e[0m";
}

void setFontRed() {
    if (isatty (fileno (stdout)))
        std::cout << "\e[1;31m";
}

void setFontGreen() {
    if (isatty (fileno (stdout)))
        std::cout << "\e[32m";
}

subroutine resetFont()
  implicit none
  ! this stuff is to print an escape sequenz and omit a newline
  write(*,'(A)', advance='no') achar(27)//"[0m"
end subroutine

subroutine setFontRed()
  implicit none
  write(*,'(A)', advance='no') achar(27)//"[1;31m"
  end subroutine

subroutine setFontGreen()
  implicit none
  write(*,'(A)', advance='no') achar(27)//"[32m";
end subroutine

Da Fortran per default keine Escape Sequenzen interpretiert (man muss das per Compiler Flag einschalten), kann man die achar() Funktion nutzen. Sie wandelt ein Integer auch in nicht-druckbare ASCII Zeichen um.

Auf diese Weise kann man sich auch eine gotoxy() Funktion bauen.

No Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

You must be logged in to post a comment.

Powered by WordPress