[folks/paging: 1/3] inspect: page output for personas command



commit 7fa42d07f53a7e0c280aed0ab493ad46f2de6102
Author: Raul Gutierrez Segales <rgs collabora co uk>
Date:   Tue Sep 27 21:11:04 2011 +0100

    inspect: page output for personas command
    
    Fixes: https://bugzilla.gnome.org/show_bug.cgi?id=660236

 NEWS                       |    1 +
 tools/inspect/Makefile.am  |    1 +
 tools/inspect/inspect.vala |   22 ++++++++++-----
 tools/inspect/utils.vala   |   62 ++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 76 insertions(+), 10 deletions(-)
---
diff --git a/NEWS b/NEWS
index 79462fb..59b6a38 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,7 @@ Dependencies:
 Bugs fixed:
 â Bug 673918 â Port to newer libgee
 â Bug 629537 â Support anti-linking
+â Bug 660236 â Paging for long text in folks-inspect
 
 API changes:
 â Add AntiLinkable interface and implement it on Kf.Persona and Edsf.Persona
diff --git a/tools/inspect/Makefile.am b/tools/inspect/Makefile.am
index f341e4a..65a1b79 100644
--- a/tools/inspect/Makefile.am
+++ b/tools/inspect/Makefile.am
@@ -6,6 +6,7 @@ VALAFLAGS = \
 	--pkg=gobject-2.0 \
 	--pkg=gio-2.0 \
 	--pkg=gee-0.8 \
+	--pkg=posix \
 	--pkg=folks \
 	--pkg=build-conf \
 	$(NULL)
diff --git a/tools/inspect/inspect.vala b/tools/inspect/inspect.vala
index 9d66234..623ff5c 100644
--- a/tools/inspect/inspect.vala
+++ b/tools/inspect/inspect.vala
@@ -24,6 +24,7 @@ using Folks;
 using Readline;
 using Gee;
 using GLib;
+using Posix;
 
 /* We have to have a static global instance so that the readline callbacks can
  * access its data, since they don't pass closures around. */
@@ -52,7 +53,7 @@ public class Folks.Inspect.Client : Object
         }
       catch (OptionError e1)
         {
-          stderr.printf ("Couldnât parse command line options: %s\n",
+          GLib.stderr.printf ("Couldnât parse command line options: %s\n",
               e1.message);
           return 1;
         }
@@ -76,7 +77,7 @@ public class Folks.Inspect.Client : Object
         }
       else
         {
-          assert (args.length > 1);
+          GLib.assert (args.length > 1);
 
           /* Drop the first argument and parse the rest as a command line. If
            * the first argument is â--â then the command was passed after some
@@ -153,7 +154,7 @@ public class Folks.Inspect.Client : Object
       finally
         {
           this.aggregator.disconnect (signal_id);
-          assert (this.aggregator.is_quiescent == true);
+          GLib.assert (this.aggregator.is_quiescent == true);
         }
     }
 
@@ -171,7 +172,7 @@ public class Folks.Inspect.Client : Object
 
       if (command == null)
         {
-          stdout.printf ("Unrecognised command â%sâ.\n", command_name);
+          GLib.stdout.printf ("Unrecognised command â%sâ.\n", command_name);
           return;
         }
 
@@ -185,7 +186,7 @@ public class Folks.Inspect.Client : Object
             }
           catch (GLib.Error e1)
             {
-              stderr.printf ("Error preparing aggregator: %s\n", e1.message);
+              GLib.stderr.printf ("Error preparing aggregator: %s\n", e1.message);
               Process.exit (1);
             }
 
@@ -205,7 +206,7 @@ public class Folks.Inspect.Client : Object
        * stdin in our main loop, and passing character notifications to
        * readline. The main loop also processes all the folks events, thus
        * preventing us having to run a second thread. */
-      var stdin_channel = new IOChannel.unix_new (stdin.fileno ());
+      var stdin_channel = new IOChannel.unix_new (GLib.stdin.fileno ());
       stdin_channel.add_watch (IOCondition.IN, (source, cond) =>
         {
           /* At least a single character is available on stdin, so let readline
@@ -228,7 +229,7 @@ public class Folks.Inspect.Client : Object
           Readline.reset_after_signal ();
 
           /* Display a fresh prompt. */
-          stdout.printf ("^C");
+          GLib.stdout.printf ("^C");
           Readline.crlf ();
           Readline.reset_line_state ();
           Readline.replace_line ("", 0);
@@ -282,11 +283,16 @@ public class Folks.Inspect.Client : Object
           /* Run the command */
           if (command != null)
             {
+              Utils.start_paged_output ();
               command.run (subcommand);
+              Utils.stop_paged_output ();
+              
+              /* Let the $PAGER finish & die */
+              //wait (out status);
             }
           else
             {
-              stdout.printf ("Unrecognised command â%sâ.\n", command_name);
+              GLib.stdout.printf ("Unrecognised command â%sâ.\n", command_name);
             }
 
           /* Store the command in the history, even if it failed */
diff --git a/tools/inspect/utils.vala b/tools/inspect/utils.vala
index 3774e63..084f8b8 100644
--- a/tools/inspect/utils.vala
+++ b/tools/inspect/utils.vala
@@ -21,6 +21,7 @@
 using Folks;
 using Gee;
 using GLib;
+using Posix;
 
 private class Folks.Inspect.Utils
 {
@@ -28,6 +29,11 @@ private class Folks.Inspect.Utils
   private static uint indentation = 0;
   private static string indentation_string = "";
 
+  /* To page or not to page? */
+  private static bool _page_output = false;
+  private static IOChannel _io_channel;
+  private static FILE? _pager;
+
   public static void init ()
     {
       Utils.indentation_string = "";
@@ -43,6 +49,46 @@ private class Folks.Inspect.Utils
           Utils.transform_date_time_to_string);
     }
 
+  public static void start_paged_output ()
+    {
+      /* If the output is not a TTY (cause its a pipe or a file or a
+       * toaster) we don't page. */
+      if (!isatty (1))
+        return;
+
+      var pager = Environment.get_variable ("PAGER");
+      if (pager != null && pager == "")
+        return;
+
+      if (pager == null)
+        pager = "less -FRSX";
+
+      /* In case the previous clean up wasn't finish */
+      Utils.stop_paged_output ();
+
+      Utils._pager = FILE.popen (pager, "w");
+      Utils._io_channel = new IOChannel.unix_new (Utils._pager.fileno ());
+      Utils._page_output = true;
+    }
+
+  public static void stop_paged_output ()
+    {
+      if (!Utils._page_output)
+        return;
+
+      try
+        {
+          Utils._io_channel.shutdown (true);
+        }
+      catch (GLib.Error e) {}
+      finally
+        {
+          Utils._io_channel = null;
+          Utils._pager = null;
+          Utils._page_output = false;
+        }
+    }
+
   private static void transform_object_to_string (Value src,
       out Value dest)
     {
@@ -107,7 +153,19 @@ private class Folks.Inspect.Utils
       /* FIXME: store the va_list temporarily to work around bgo#638308 */
       var valist = va_list ();
       string output = format.vprintf (valist);
-      stdout.printf ("%s%s\n", Utils.indentation_string, output);
+      var str = "%s%s\n".printf (Utils.indentation_string, output);
+      if (Utils._page_output)
+        {
+          try
+            {
+              size_t written;
+              Utils._io_channel.write_chars ((char [])str, out written);
+            }
+          catch (GLib.ConvertError e1) {}
+          catch (GLib.IOChannelError e2) {}
+        }
+      else
+        GLib.stdout.printf (str);
     }
 
   public static void print_individual (Individual individual,
@@ -526,7 +584,7 @@ private class Folks.Inspect.Utils
 
           if (Utils.persona_uid_iter == null)
             {
-              assert (individual != null);
+              GLib.assert (individual != null);
               Utils.persona_uid_iter = individual.personas.iterator ();
             }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]