aboutsummaryrefslogtreecommitdiff
path: root/wide.pl
blob: dc93be77f2fcefc6b77109520510a7d4bcc69900 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/perl

=pod

=head1 NAME

wide.pl - print double-width characters

=head1 SYNOPSIS

=head2 From the shell:

wide.pl <[args ...]>

=head2 From within irssi:

/script load wide.pl

/wide [args]

=head1 DESCRIPTION

wide.pl converts printable ASCII characters to their double-width
equivalent from the Unicode 0xff block. See:

https://en.wikipedia.org/wiki/Halfwidth_and_Fullwidth_Forms_(Unicode_block)

Characters that don't have double-wide equivalents are simply printed
with a trailing space.

This script can be run standalone or as an irssi /script. For things to work
properly in irssi, you'll need UTF-8 and Unicode support in irssi and in the
terminal you're using to run irssi.

When run standalone with no arguments, reads from stdin and writes to stdout.
If arguments are given, they're treated as input (not filenames). Use shell
redirection to read from a file: wide.pl < message.txt

=head1 AUTHOR

Urchlay <yalhcru@gmail.com>

=head1 LICENSE

wide.pl is released under the WTFPL: Do WTF you want with this.

=cut

use warnings;
use strict;

sub wide {
	my $text = shift;
	my $res = "";

	for (split "", $text) {
		my $c = ord($_);
		if($c >= 0x21 && $c <= 0x7e) {
			$res .= chr($c + 0xfee0);
		} else {
			$res .= "$_$_";
		}
	}

	return $res;
}

# main() if we're not running under irssi
if(__PACKAGE__ eq 'main') {
	no warnings 'utf8';

	exec "perldoc $0" if @ARGV && $ARGV[0] =~ /--?h(elp)?/;

	if(@ARGV) {
		print wide($_) for @ARGV;
		print "\n";
	} else {
		while(<>) {
			chomp;
			print wide($_);
			print "\n";
		}
	}

	exit 0;
}

# irssi stuff here
require Irssi;
Irssi->import(qw/command command_bind/);

our $VERSION = "0.1";
our %IRSSI = (
		authors     => 'Urchlay',
		contact     => 'Urchlay on FreeNode',
		name        => 'wide',
		description => 'print double-width characters',
		license     => 'WTFPL',
		url         => 'none',
		);

sub cmd_wide {
	my ($text, $srv, $chan) = @_;

	return unless $text;
	return unless $srv;
	return unless $chan;

	$chan->command('MSG ' . $chan->{name} . ' ' . wide($text));
}

command_bind("wide", \&cmd_wide);