Skip to content

Instantly share code, notes, and snippets.

@Xliff
Created May 26, 2025 05:48
Show Gist options
  • Save Xliff/8e30d56b753dcbd4e88b9f90887a5763 to your computer and use it in GitHub Desktop.
Save Xliff/8e30d56b753dcbd4e88b9f90887a5763 to your computer and use it in GitHub Desktop.
Attempting to Draw using Terminal::Print

Here's code that should draw a progress bar and a label. It's not working. What am I doing wrong?

#!/usr/bin/env raku

#| A command-line program to display a customizable, colorful progress bar
#| Usage: raku progress-bar.raku <percentage>
#| Example: raku progress-bar.raku 75

use v6;

use Terminal::Print <T>;
use Terminal::Print;
use Terminal::QuickCharts;

class ProgressLine {
  has $!row    is built;
  has $!color  is built;
  has $!text   is built;
  has $!value;

  method set-label ($t is copy) {
    $t = $t.substr(0, 29) ~ '' if $t.chars > 30;
    $!text = $t;
    self!draw-label;
    self;
  }

  method !draw-label {
    T.current-grid.set-span-text($!row, 0, ' ' x 30 );
    T.current-grid.set-span-text($!row, 0, $!text);
  }

  method set-value ($v) {
    $!value = $v;
    self!draw-value;
    self;
  }

  method !draw-value {
    $B = (
      my $b = hbar-chart(
        $!value.Array,
        colors    => [ 'default' ],
        min       => 0,
        max       => 100,
        style     => Terminal::QuickCharts::ChartStyle.new(
          max-width => 40
        )
      );
    );

    T.current-grid.set-span( $!row, 32, $b.head, "{ $!color } on_75,75,75" )
  }

}

#| Main entry point - parse arguments and display progress bar
multi sub MAIN ($percentage is copy where * ~~ 0 .. 100) {
    # Default to 0% if no argument provided
    $percentage //= 0;

    T.initialize-screen;

    # Validate input is a number
    try {
        $percentage = $percentage.Numeric;
        CATCH {
            default {
                note "Error: Input must be a number between 0 and 100";
                note "Usage: raku progress-bar.raku <percentage>";
                exit 1;
            }
        }
    }

    #try {
      #CATCH { default { } }

      # Display the progress bar
      ProgressLine.new(
        color => 'blue',
        row   => 10
      ).set-label('My Progress').set-value($percentage);

      sleep 10;
    #}

    T.shutdown-screen;
}

multi sub MAIN {
  USAGE
}

# Print usage information if run with --help
sub USAGE() {
    say "Usage: raku progress-bar.raku <percentage>";
    say "Displays a colorful progress bar with the specified percentage (0-100)";
    say "Example: raku progress-bar.raku 75";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment