From f42b0ba740d6a1ec11404e707c4b047e34b9013c Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sat, 30 Mar 2019 15:36:17 +1300 Subject: Convert: Don't use interactive mode --- Plugin/Convert.pm | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'Plugin') diff --git a/Plugin/Convert.pm b/Plugin/Convert.pm index c830628..75f4ba0 100755 --- a/Plugin/Convert.pm +++ b/Plugin/Convert.pm @@ -19,21 +19,30 @@ sub convert { my $from = (split / to /, $rest)[0]; my $to = (split / to /, $rest)[1]; - return "Syntax: convert to \n" unless ($from and $to); + return "Syntax: convert [to ]\n" unless ($from); my ($out, $in); - my $pid = open2($out, $in, 'units', '-1', '--compact', '--quiet'); + my $pid; + if ($to) { + $pid = open2($out, $in, 'units', '-1', '--compact', '--quiet', $from, $to); + } else { + $pid = open2($out, $in, 'units', '-1', '--compact', '--quiet', $from); + } - print $in "$from\n$to\n"; my $converted = <$out>; chomp $converted; close($in); waitpid($pid, 0); + my $exit_status = $? >> 8; # `units` doesn't actually seem to set this non-zero, but use it anyway - return "Conversion error" if $exit_status; + return "Error: $converted" if $exit_status; - return "Converted: $converted\n"; + if ($to) { + return "Convert $from -> $to: $converted\n"; + } else { + return "Define $from: $converted\n"; + } } 1; -- cgit v1.1