Added prompt to comments
authorAaron Bentley <aaron.bentley@utoronto.ca>
Sun, 16 Apr 2006 21:14:27 +0000 (17:14 -0400)
committerAaron Bentley <aaron.bentley@utoronto.ca>
Sun, 16 Apr 2006 21:14:27 +0000 (17:14 -0400)
becommands/comment.py
libbe/utility.py

index 4f0bf3b8ba67ecec1cc18e088001e9f6989dd365..5f7128d2f60555bc99276cc42225b691b652b729 100644 (file)
@@ -49,7 +49,7 @@ def execute(args):
     bug, parent_comment = cmdutil.get_bug_and_comment(args[0])
     if len(args) == 1:
         try:
-            body = utility.editor_string()
+            body = utility.editor_string("Please enter your comment above")
         except utility.CantFindEditor:
             raise cmdutil.UserError(
                 "No comment supplied, and EDITOR not specified.")
index e62f2cd04420747e97ebe06a45adc9a7859f3e0c..a8c3e248ef025f866dcc25809df761093c7e6b9e 100644 (file)
@@ -98,7 +98,7 @@ class CantFindEditor(Exception):
     def __init__(self):
         Exception.__init__(self, "Can't find editor to get string from")
 
-def editor_string():
+def editor_string(comment=None):
 
     """Invokes the editor, and returns the user_produced text as a string
 
@@ -117,15 +117,37 @@ def editor_string():
         raise CantFindEditor()
     fhandle, fname = tempfile.mkstemp()
     try:
+        if comment is not None:
+            os.write(fhandle, '\n'+comment_string(comment))
         os.close(fhandle)
         oldmtime = os.path.getmtime(fname)
         os.system("%s %s" % (editor, fname))
-        if oldmtime == os.path.getmtime(fname) and\
-            file(fname, "rb").read() == "":
+        output = trimmed_string(file(fname, "rb").read())
+        if output.rstrip('\n') == "":
             output = None
-        else:
-            output = file(fname, "rb").read()
     finally:
         os.unlink(fname)
     return output
 
+
+def comment_string(comment):
+    """
+    >>> comment_string('hello')
+    '== Anything below this line will be ignored ==\\nhello'
+    """
+    return '== Anything below this line will be ignored ==\n' + comment
+
+
+def trimmed_string(instring):
+    """
+    >>> trimmed_string("hello\\n== Anything below this line will be ignored")
+    'hello\\n'
+    >>> trimmed_string("hi!\\n" + comment_string('Booga'))
+    'hi!\\n'
+    """
+    out = []
+    for line in instring.splitlines(True):
+        if line.startswith('== Anything below this line will be ignored'):
+            break
+        out.append(line)
+    return ''.join(out)