On Wed, 2013-02-20 at 13:08 +0800, Paul Wise wrote:

> Pass a fake terminal to subcommands when mr is run in a terminal

This one was broken when IO::Pty::Easy wasn't installed, attached an
updated version.

-- 
bye,
pabs

http://wiki.debian.org/PaulWise
From f6cc44a570307577f0a3f79e60e57dd614e87869 Mon Sep 17 00:00:00 2001
From: Paul Wise <p...@debian.org>
Date: Wed, 20 Feb 2013 12:03:38 +0800
Subject: [PATCH 2/5] Pass a fake terminal to subcommands when mr is run in a
 terminal

---
 debian/changelog |  6 ++++++
 debian/control   |  2 +-
 mr               | 47 ++++++++++++++++++++++++++++++++---------------
 3 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 6f318e2..accd7f1 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+mr (1.15) unstable; urgency=low
+
+  * Pass a fake terminal to subcommands when mr is run in a terminal
+
+ -- Paul Wise <p...@debian.org>  Thu, 14 Feb 2013 14:54:00 +0800
+
 mr (1.14) unstable; urgency=low
 
   * Added a fetch command. Closes: #480580
diff --git a/debian/control b/debian/control
index 7189dc4..2700380 100644
--- a/debian/control
+++ b/debian/control
@@ -12,7 +12,7 @@ Architecture: all
 Section: vcs
 Depends: ${misc:Depends}
 Suggests: subversion, git-core | git (>= 1:1.7), cvs, bzr, mercurial, darcs, fossil, vcsh, liburi-perl, curl
-Recommends: libwww-perl, libhtml-parser-perl, perl
+Recommends: libwww-perl, libio-pty-easy-perl, libhtml-parser-perl, perl
 Description: Multiple Repository management tool
  The mr(1) command can checkout, update, or perform other actions on
  a set of repositories as if they were one combined respository. It
diff --git a/mr b/mr
index 691bc52..9d99959 100755
--- a/mr
+++ b/mr
@@ -549,6 +549,7 @@ my $no_chdir=0;
 my $jobs=1;
 my $trust_all=0;
 my $directory=getcwd();
+my $terminal=-t STDOUT && eval{require IO::Pty::Easy;IO::Pty::Easy->import();1;} eq 1;
 
 my $HOME_MR_CONFIG = "$ENV{HOME}/.mrconfig";
 $ENV{MR_CONFIG}=find_mrconfig();
@@ -692,6 +693,33 @@ sub fulldir {
 	return $subdir =~ /^\// ? $subdir : $topdir.$subdir;
 }
 
+sub terminal_friendly_spawn {
+	my $actionmsg = shift;
+	my $sh = shift;
+	my $quiet = shift;
+	my $output = "";
+	if ($terminal) {
+		my $pty = IO::Pty::Easy->new;
+		$pty->spawn($sh);
+		while ($pty->is_active) {
+			my $data = $pty->read();
+			$output .= $data if defined $data;
+		}
+		$pty->close;
+	} else {
+		$output = qx/$sh 2>&1/;
+	}
+	my $ret = $?;
+	if ($ret != 0) {
+		print "$actionmsg\n" if $actionmsg;
+		print STDERR $output;
+	} elsif (!$quiet && $output) {
+		print "$actionmsg\n" if $actionmsg;
+		print $output;
+	}
+	return $ret;
+}
+
 sub action {
 	my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
 	my $fulldir=fulldir($topdir, $subdir);
@@ -793,14 +821,8 @@ sub action {
 		my $ret=runsh $action, $topdir, $subdir,
 			$command, \@ARGV, sub {
 				my $sh=shift;
-				if ($quiet) {
-					my $output = qx/$sh 2>&1/;
-					my $ret = $?;
-					if ($ret != 0) {
-						print "$actionmsg\n";
-						print STDERR $output;
-					}
-					return $ret;
+				if (!$jobs || $jobs > 1 || $quiet) {
+					return terminal_friendly_spawn($actionmsg, $sh, $quiet);
 				}
 				else {
 					system($sh);
@@ -864,13 +886,8 @@ sub hook {
 	return OK unless defined $command;
 	my $ret=runsh $hook, $topdir, $subdir, $command, [], sub {
 			my $sh=shift;
-			if ($quiet) {
-				my $output = qx/$sh 2>&1/;
-				my $ret = $?;
-				if ($ret != 0) {
-					print STDERR $output;
-				}
-				return $ret;
+			if (!$jobs || $jobs > 1 || $quiet) {
+				return terminal_friendly_spawn(undef, $sh, $quiet);
 			}
 			else {
 				system($sh);
-- 
1.8.2.1

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to