Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: 80x25 ASCII text art in terminal

by cavac (Prior)
on Jun 21, 2023 at 13:22 UTC ( [id://11153068]=note: print w/replies, xml ) Need Help??


in reply to 80x25 ASCII text art in terminal

I've mentioned using the Curses library in another reply. With a bit of reading docs and playing around (and, i admit, asking ChatGPT for some extra help), i have come up with this:

#!/usr/bin/env perl use strict; use warnings; use Curses; my $CENTERIMAGE = 0; # ASCII art to display my $art = <<'ASCII_ART'; _______ / \ | | | | \_______/ ASCII_ART # Initialize Curses initscr(); # Initialize screen / Start curses mode noecho(); # Don't echo (display) characters typed by the user cbreak(); # Line buffering disabled, Pass on keypad(1); # It enables the reading of function keys like F1, F2, ar +row keys etc. curs_set(0); # Hide the active cursor # Get terminal dimensions my $rows; my $cols; getmaxyx(stdscr(), $rows, $cols); # Calculate the center position for displaying the ASCII art my @parts = split/\n/, $art; my $linecount = scalar @parts; my $rowcount = 0; foreach my $part (@parts) { if(length($part) > $rowcount) { $rowcount = length($part); } } my ($xoffs, $yoffs); if($CENTERIMAGE) { # Center $xoffs = int(($cols - $rowcount) / 2); $yoffs = int(($rows - $linecount) / 2); } else { # Lower right $xoffs = int(($cols - $rowcount)); $yoffs = int(($rows - $linecount)); } clear(); # Clear the screen # Display the ASCII art for(my $i = 0; $i < $linecount; $i++) { move($yoffs + $i, $xoffs); addstr($parts[$i]); } refresh(); # Refresh the screen (e.g. actually put what we have drawn +into the terminal) getch(); # Wait for a keypress endwin(); # Exit curses mode

At least the terminals i had quick access too (xfce4-terminal, xterm, bash in screen in xterm, the basic linux shell that starts before loading X11), this seems to work quite well.

If you need something a bit more sophisticated that just putting characters on the screen, Curses::UI comes with all kinds of input fields, textmode-windows, mouse support etc...

Of course, printing ASCII art monochrome so boooring. Thankfully, someone invented colors in the 1990's. Here's the color version:

#!/usr/bin/env perl use strict; use warnings; use Curses; use Carp; my $TRANSPARENCY = 0; # ASCII art to display my $art = <<'ASCII_ART'; _______ / \ | | | | \_______/ ASCII_ART # Initialize Curses initscr(); # Initialize screen / Start curses mode if(!has_colors()) { endwin(); croak("Your boring terminal has no color support!"); } noecho(); # Don't echo (display) characters typed by the user cbreak(); # Line buffering disabled, Pass on keypad(1); # It enables the reading of function keys like F1, F2, +arrow keys etc. curs_set(0); # Hide the active cursor start_color(); # Color mode ON!!!! # Get terminal dimensions my $rows; my $cols; getmaxyx(stdscr(), $rows, $cols); # Calculate the center position for displaying the ASCII art my @parts = split/\n/, $art; my $linecount = scalar @parts; my $rowcount = 0; foreach my $part (@parts) { if(length($part) > $rowcount) { $rowcount = length($part); } } # Generate the color pairs my @colors = (COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, COLOR_ +MAGENTA, COLOR_CYAN, COLOR_WHITE); my $cnum = 1; my @colorpairs; foreach my $color (@colors) { init_pair($cnum, $color, COLOR_BLACK); push @colorpairs, COLOR_PAIR($cnum); $cnum++; } clear(); # Clear the screen # Display the ASCII art my ($xoffs, $yoffs) = (0,0); foreach my $colorpair (@colorpairs) { attron($colorpair); for(my $i = 0; $i < $linecount; $i++) { if(!$TRANSPARENCY) { # No transparency, can print the whole line addstr($yoffs + $i, $xoffs, $parts[$i]); } else { # Only print non-space characters my @chars = split//, $parts[$i]; my $coloffs = 0; foreach my $char (@chars) { if($char ne ' ') { addstr($yoffs + $i, $xoffs + $coloffs, $char); } $coloffs++; } } } attroff($colorpair); $xoffs += 2; $yoffs += 3; } refresh(); # Refresh the screen (e.g. actually put what we have drawn +into the terminal) getch(); # Wait for a keypress endwin(); # Exit curses mode

Edit: With a bit of playing around, it should be possible to display low res color images in the text console as well. If you're interested, i would give it a go.

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

Replies are listed 'Best First'.
Re^2: 80x25 ASCII text art in terminal
by NERDVANA (Priest) on Jun 21, 2023 at 18:07 UTC

    There's already a perl interface to aalib, so that probably takes care of images rendered as ascii (as opposed to more line-oriented ascii art that humans would produce).

    Had you seen TTY Quake before? Sadly it doesn't seem that anyone has uploaded a Youtube video of it, but there is TTY World of Warcraft

    I have a bzcat movie of TTYQuake somewhere lost in my old backups of harddrives I think. I should hunt that down...

Re^2: 80x25 ASCII text art in terminal
by afoken (Chancellor) on Jun 21, 2023 at 19:39 UTC
    Of course, printing ASCII art monochrome so boooring. Thankfully, someone invented colors in the 1990's.

    Nope.

    ANSI.SYS was included in MS-DOS since at least 3.3, i.e. 1988.

    The HP 2627A terminal had 8 colors on a CRT, it was build in 1982. (But it looks like it does not understand ANSI standard color codes.)

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11153068]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2025-05-23 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.