Term::Title


Term-Title documentation Contained in the Term-Title distribution.

Index


Code Index:


Term-Title documentation Contained in the Term-Title distribution.

# lib/Term/Title.pm
# Copyright (c) 2008 by David Golden. All rights reserved.
# Licensed under Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License was distributed with this file or you may obtain a 
# copy of the License from http://www.apache.org/licenses/LICENSE-2.0

package Term::Title;
use strict;
use warnings;

our $VERSION = '0.03';
$VERSION = eval $VERSION; ## no critic

use Exporter;
our @ISA = 'Exporter';
our @EXPORT_OK = qw/set_titlebar/;

# encodings by terminal type -- except for mswin32 get matched as regex
# against $ENV{TERM}
# code ref gets title and text to print
my %terminal = (
    'xterm|rxvt' => {
        pre => "\033]2;",
        post => "\007",
    },
    'screen' => {
        pre => "\ek",
        post => "\e\\",
    },
    'mswin32' => sub {
        my ($title, @optional) = @_;
        my $c = Win32::Console->new();
        $c->Title($title);
        print STDOUT @optional, "\n";
    },
);

sub set_titlebar {
    my ($title, @optional) = @_;
    $title = q{ } unless defined $title;
    my $type = _is_supported();

    if ( $type ) {
        if ( ref $terminal{$type} eq 'CODE' ) {
            $terminal{$type}->( $title, @optional );
        }
        elsif (ref $terminal{$type} eq 'HASH' ) {
            print STDOUT $terminal{$type}{pre},  $title, 
                         $terminal{$type}{post}, @optional, "\n";
        }
    }
    elsif ( @optional ) {
        print STDOUT @optional, "\n";
    }
    return;
}

sub _is_supported {
    if ( $^O eq 'MSWin32' ) {
        return 'mswin32' if eval { require Win32::Console };
    }
    else {
        return unless $ENV{TERM};
        for my $k ( keys %terminal ) {
            return $k if $ENV{TERM} =~ /^(?:$k)/;
        }
    }
    return;
}

1;

__END__