antiright-devel
[Top][All Lists]
Advanced

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

[Antiright-devel] antiright/rootcat rootcat.c rootcat.py


From: Jeffrey Bedard
Subject: [Antiright-devel] antiright/rootcat rootcat.c rootcat.py
Date: Wed, 16 Jan 2008 17:48:14 +0000

CVSROOT:        /sources/antiright
Module name:    antiright
Changes by:     Jeffrey Bedard <jefbed> 08/01/16 17:48:14

Modified files:
        rootcat        : rootcat.c 
Added files:
        rootcat        : rootcat.py 

Log message:
        Added python version of rootcat.  

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/antiright/rootcat/rootcat.c?cvsroot=antiright&r1=1.2&r2=1.3
http://cvs.savannah.gnu.org/viewcvs/antiright/rootcat/rootcat.py?cvsroot=antiright&rev=1.1

Patches:
Index: rootcat.c
===================================================================
RCS file: /sources/antiright/antiright/rootcat/rootcat.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- rootcat.c   15 Jan 2008 18:20:56 -0000      1.2
+++ rootcat.c   16 Jan 2008 17:48:13 -0000      1.3
@@ -32,6 +32,10 @@
 #define SR_DEFAULT_FONT "Mono 12"
 #define SR_DEFAULT_COLOR "white"
 
+#ifndef $
+#define $(class, method, ...) class->method(class, ##__VA_ARGS__)
+#endif /* ! $ */
+
 typedef struct _StringRenderer
 {
        Display * dpy;
@@ -40,7 +44,7 @@
        XftColor color;
        XftFont * font;
        void (*delete)(struct _StringRenderer *);
-       void (*draw)(struct _StringRenderer *, 
+       unsigned int (*draw)(struct _StringRenderer *, 
                const int, const int, const char *);
        void (*clear)(struct _StringRenderer *);
        unsigned int (*get_line_height)(struct _StringRenderer *,       
@@ -70,12 +74,32 @@
        return extents.height;
 }
 
-static void
+/* Returns the number of lines printed.  */
+static unsigned int
 StringRenderer_draw(StringRenderer * rc, 
        const int x, const int y, const char * string)
 {
+       char * iter = (char*)string;
+       const int max_length=strlen(string);
+       int total_length;
+       int length;
+       int lines=0;
+
+       for(total_length=0; total_length<=max_length; total_length+=length)
+       {
+               for(length=0;iter[length] != '\0' 
+                       && iter[length] !='\n';length++);
+               length++;
        XftDrawStringUtf8(rc->__draw, &(rc->color), rc->font,
-               x, y, (FcChar8 *)string, strlen(string));
+                       x, y+lines*1.5*($(rc, get_line_height, iter)), 
+                       (FcChar8 *)iter, length-1); 
+               iter+=length;
+               lines++;
+       }
+       return lines;
+/*
+       XftDrawStringUtf8(rc->__draw, &(rc->color), rc->font,
+               x, y, (FcChar8 *)string, strlen(string)); */
 }
 
 static void
@@ -145,28 +169,58 @@
        return new_StringRenderer(dpy, DefaultRootWindow(dpy), font, NULL);
 }
 
-#ifndef $
-#define $(class, method, ...) class->method(class, ##__VA_ARGS__)
-#endif /* ! $ */
+static inline void
+rootcat_end(StringRenderer * rc, int exit_code)
+{
+       $(rc, delete);
+       exit(exit_code);
+}
 
-int
-main(int argc, char ** argv)
+static void
+parse_command_line(StringRenderer * rc, int argc, char ** argv)
 {
-       StringRenderer * rc;
+       int counter;
 
-       rc=new_StringRenderer_root(NULL);
-       assert(rc);
+       for(counter=1; counter<argc; counter++)
+       {
+               const char * string = argv[counter];
 
-       $(rc, clear);
-       for(;argc>1; argc--)
+               if(string[0]=='-')
+               {
+                       switch(string[1])
+                       {
+                       case 'c':
+                               /* Since the root window is cleared before
+                                  this function is called, simply exiting
+                                  at this point will achieve desired
+                                  operation.  */
+                               rootcat_end(rc, 0);
+                               break;
+                       default:
+                               printf("USAGE %s [-c] [string] ...\n",
+                                       argv[0]);
+                               rootcat_end(rc, 1);
+                       }
+               }
+               else
        {
-               const int index=argc-1;
-               const char * string = argv[index];
                unsigned int height=$(rc, get_line_height, string);
 
-               $(rc, draw, (height/2), ((index)*1.5*height), string);
+                       $(rc, draw, (height/2), 
+                               ((counter)*1.5*height), string);
+               }
        }
+}
 
+int
+main(int argc, char ** argv)
+{
+       StringRenderer * rc;
+
+       rc=new_StringRenderer_root(NULL);
+       assert(rc);
+       $(rc, clear);
+       parse_command_line(rc, argc, argv);
        $(rc, delete);
 
        return 0;

Index: rootcat.py
===================================================================
RCS file: rootcat.py
diff -N rootcat.py
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ rootcat.py  16 Jan 2008 17:48:14 -0000      1.1
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+
+#    (c) 2008 Jeffrey Bedard
+#    address@hidden
+# 
+#    This file is part of AntiRight.
+#
+#     AntiRight is free software; you can redistribute it and/or modify
+#     it under the terms of the GNU General Public License as published by
+#     the Free Software Foundation; either version 2 of the License, or
+#     (at your option) any later version.
+#
+#     AntiRight is distributed in the hope that it will be useful,
+#     but WITHOUT ANY WARRANTY; without even the implied warranty of
+#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#     GNU General Public License for more details.
+#
+#     You should have received a copy of the GNU General Public License
+#     along with AntiRight; if not, write to the Free Software
+#     Foundation, Inc., 51 Franklin Street, Fifth Floor, 
+#     Boston, MA  02110-1301  USA
+
+
+# Based on:
+
+# examples/draw.py -- high-level xlib test application.
+#
+#    Copyright (C) 2000 Peter Liljenberg <address@hidden>
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the Free Software
+#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+import sys
+import os
+from Xlib import X, display, Xutil
+from optparse import OptionParser
+
+DEBUG=False;
+
+class StringRenderer:
+       def __init__(self, display):
+               self.dpy=display;
+               self.screen=self.dpy.screen();
+               self.root=self.screen.root;
+               self.font=self.dpy.open_font("fixed")
+               self.gc=self.root.create_gc(
+                       foreground=self.screen.white_pixel,
+                       background=self.screen.black_pixel,
+                       font=self.font);
+       def clear(self):
+               self.root.clear_area();
+               if DEBUG:
+                       print("clear()");
+       def draw(self, text, x=16, y=32):
+               self.root.draw_text(self.gc, x, y, text);
+       def __del__(self):
+               if DEBUG:
+                       print("Cleaning up StringRenderer...");
+               self.gc.free();
+               self.font.close();
+               self.dpy.close();
+
+class RootCat:
+       def __init__(self):
+               self.renderer=StringRenderer(display.Display());
+               self.renderer.clear();
+       def echo_args(self, args=sys.argv[1:]):
+               line=0
+               for string in args:
+                       line=line+1;
+                       self.renderer.draw(string, y=line*16);
+       def parse_args(self, args=sys.argv):
+               parser=OptionParser();
+               parser.add_option("-c", "--clear", 
+                       dest="clear", action="store_true");
+               (options, args)=parser.parse_args();
+               if options.clear:
+                       sys.exit(0);
+               self.echo_args(args)
+       def __del__(self):
+               if DEBUG:
+                       print("Cleaning up RootCat...");
+
+if __name__ == '__main__':
+       RootCat().parse_args();
+
+




reply via email to

[Prev in Thread] Current Thread [Next in Thread]