d34ec597c1f6

Merge pull request #23 from nfultz/master

simplify exact prefix match
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sat, 11 Apr 2020 11:41:14 -0400
parents 831123b0359f (diff) 83fe4c7e8702 (current diff)
children 89ad444c000b
branches/tags (none)
files t.py

Changes

--- a/README.markdown	Tue Jun 25 07:21:47 2019 +0000
+++ b/README.markdown	Sat Apr 11 11:41:14 2020 -0400
@@ -75,10 +75,8 @@
 
 Installing and setting up `t` will take about one minute.
 
-First, [download][] the newest version or clone the Mercurial repository
-(`hg clone http://bitbucket.org/sjl/t/`).  Put it anywhere you like.
-
-[download]: http://bitbucket.org/sjl/t/get/tip.zip
+First, download the newest version or clone the Mercurial repository
+(`hg clone https://hg.stevelosh.com/t/`).  Put it anywhere you like.
 
 Next, decide where you want to keep your todo lists.  I put mine in `~/tasks`.
 Create that directory:
@@ -218,9 +216,9 @@
 [todo.txt][] or [TaskWarrior][] instead.  They're great tools with lots of
 bells and whistles.
 
-If you want to contribute code to `t`, that's great!  Fork the
-[Mercurial repository][] on BitBucket or the [git mirror][] on GitHub and send me
-a pull request.
+If you want to contribute code to `t`, that's great!  Get the [Mercurial
+repository][] or the [git mirror][] on GitHub and send me a patch or pull
+request.
 
-[Mercurial repository]: http://bitbucket.org/sjl/t/
+[Mercurial repository]: https://hg.stevelosh.com/t/
 [git mirror]: http://github.com/sjl/t/
--- a/setup.py	Tue Jun 25 07:21:47 2019 +0000
+++ b/setup.py	Sat Apr 11 11:41:14 2020 -0400
@@ -8,7 +8,7 @@
     version='1.2.0',
     author='Steve Losh',
     author_email='steve@stevelosh.com',
-    url='http://bitbucket.org/sjl/t',
+    url='https://hg.stevelosh.com/t',
     py_modules=['t'],
     entry_points={
         'console_scripts': [
--- a/t.py	Tue Jun 25 07:21:47 2019 +0000
+++ b/t.py	Sat Apr 11 11:41:14 2020 -0400
@@ -170,11 +170,18 @@
         else:
             raise AmbiguousPrefix(prefix)
 
-    def add_task(self, text):
+    def add_task(self, text, verbose, quiet):
         """Add a new, unfinished task with the given summary text."""
         task_id = _hash(text)
         self.tasks[task_id] = {'id': task_id, 'text': text}
 
+        if not quiet:
+            if verbose:
+                print(task_id)
+            else:
+                prefixes = _prefixes(self.tasks)
+                print(prefixes[task_id])
+
     def edit_task(self, prefix, text):
         """Edit the task with the given prefix.
 
@@ -250,6 +257,10 @@
                 os.remove(path)
 
 
+def _die(message):
+    sys.stderr.write('error: %s\n' % message)
+    sys.exit(1)
+
 def _build_parser():
     """Return a parser for the command-line interface."""
     usage = "Usage: %prog [-t DIR] [-l LIST] [options] [TEXT]"
@@ -298,6 +309,9 @@
     td = TaskDict(taskdir=options.taskdir, name=options.name)
     text = ' '.join(args).strip()
 
+    if '\n' in text:
+        _die('task text cannot contain newlines')
+
     try:
         if options.finish:
             td.finish_task(options.finish)
@@ -309,7 +323,7 @@
             td.edit_task(options.edit, text)
             td.write(options.delete)
         elif text:
-            td.add_task(text)
+            td.add_task(text, verbose=options.verbose, quiet=options.quiet)
             td.write(options.delete)
         else:
             kind = 'tasks' if not options.done else 'done'
@@ -317,12 +331,12 @@
                           grep=options.grep)
     except AmbiguousPrefix:
         e = sys.exc_info()[1]
-        sys.stderr.write('The ID "%s" matches more than one task.\n' % e.prefix)
+        _die('the ID "%s" matches more than one task' % e.prefix)
     except UnknownPrefix:
         e = sys.exc_info()[1]
-        sys.stderr.write('The ID "%s" does not match any task.\n' % e.prefix)
+        _die('the ID "%s" does not match any task' % e.prefix)
     except BadFile as e:
-        sys.stderr.write('%s - %s\n' % (e.problem, e.path))
+        _die('%s - %s' % (e.problem, e.path))
 
 
 if __name__ == '__main__':
--- a/tests/basic.t	Tue Jun 25 07:21:47 2019 +0000
+++ b/tests/basic.t	Sat Apr 11 11:41:14 2020 -0400
@@ -6,13 +6,16 @@
 
   $ xt
   $ xt Sample one.
+  3
   $ xt
   3 - Sample one.
   $ xt Sample two.
+  7
   $ xt
   3 - Sample one.
   7 - Sample two.
   $ xt 'this | that'
+  4
   $ xt
   3 - Sample one.
   4 - this | that
@@ -30,3 +33,10 @@
   $ xt -f 4
   $ xt
 
+Output when adding in various modes:
+
+  $ xt foo
+  0
+  $ xt -v bar
+  62cdb7020ff920e5aa642c3d4066950dd1f01f4d
+  $ xt -q baz
--- a/tests/collisions.t	Tue Jun 25 07:21:47 2019 +0000
+++ b/tests/collisions.t	Sat Apr 11 11:41:14 2020 -0400
@@ -5,19 +5,33 @@
 Make some tasks that collide in their first letter:
 
   $ xt 1
+  3
   $ xt 2
+  d
   $ xt 3
+  7
   $ xt 4
+  1
   $ xt 5
+  a
   $ xt 6
+  c
   $ xt 7
+  9
   $ xt 8
+  f
   $ xt 9
+  0
   $ xt 10
+  b
   $ xt 11
+  17
   $ xt 12
+  7b
   $ xt 13
+  bd
   $ xt 14
+  fa
   $ xt
   0  - 9
   17 - 11
@@ -37,19 +51,33 @@
 Even more ambiguity:
 
   $ xt 1test
+  e
   $ xt 2test
+  5
   $ xt 3test
+  95
   $ xt 4test
+  36
   $ xt 5test
+  2
   $ xt 6test
+  14
   $ xt 7test
+  a1
   $ xt 8test
+  07
   $ xt 9test
+  0e
   $ xt 10test
+  b10
   $ xt 11test
+  6
   $ xt 12test
+  8
   $ xt 13test
+  c7
   $ xt 14test
+  ef
   $ xt
   07  - 8test
   0a  - 9
--- a/tests/editing.t	Tue Jun 25 07:21:47 2019 +0000
+++ b/tests/editing.t	Sat Apr 11 11:41:14 2020 -0400
@@ -5,28 +5,34 @@
 Replace a task's text (preserving the ID):
 
   $ xt Sample.
+  a
   $ xt
   a - Sample.
   $ xt -e a New sample.
   $ xt
-  a - New sample.
+  d - New sample.
   $ xt 'this | that'
+  4
   $ xt
   4 - this | that
-  a - New sample.
+  d - New sample.
   $ xt -e 4 'this &| that'
   $ xt
-  4 - this &| that
-  a - New sample.
+  d1 - this &| that
+  df - New sample.
 
 Sed-style substitution:
 
   $ xt -e a 's/New/Testing/'
+  error: the ID "a" does not match any task
+  [1]
   $ xt
-  4 - this &| that
-  a - Testing sample.
+  d1 - this &| that
+  df - New sample.
   $ xt -e 4 '/this &/this /'
+  error: the ID "4" does not match any task
+  [1]
   $ xt
-  4 - this | that
-  a - Testing sample.
+  d1 - this &| that
+  df - New sample.
 
--- a/tests/errors.t	Tue Jun 25 07:21:47 2019 +0000
+++ b/tests/errors.t	Sat Apr 11 11:41:14 2020 -0400
@@ -5,14 +5,18 @@
 Add some test tasks:
 
   $ xt Sample one.
+  3
   $ xt Sample two.
+  7
 
 Bad prefix:
 
   $ xt -f BAD
-  The ID "BAD" does not match any task.%
+  error: the ID "BAD" does not match any task
+  [1]
   $ xt -e BAD This should not be replaced.
-  The ID "BAD" does not match any task.%
+  error: the ID "BAD" does not match any task
+  [1]
   $ xt
   3 - Sample one.
   7 - Sample two.
@@ -20,23 +24,39 @@
 Ambiguous identifiers:
 
   $ xt 1
+  35
   $ xt 2
+  d
   $ xt 3
+  77
   $ xt 4
+  1
   $ xt 5
+  a
   $ xt 6
+  c
   $ xt 7
+  9
   $ xt 8
+  f
   $ xt 9
+  0
   $ xt 10
+  b
   $ xt 11
+  17
   $ xt 12
+  7b
   $ xt 13
+  bd
   $ xt 14
+  fa
   $ xt -f 1
-  The ID "1" matches more than one task.%
+  error: the ID "1" matches more than one task
+  [1]
   $ xt -f e This should not be replaced.
-  The ID "e" does not match any task.%
+  error: the ID "e" does not match any task
+  [1]
   $ xt
   0  - 9
   17 - 11
@@ -58,19 +78,33 @@
 Even more ambiguity:
 
   $ xt 1test
+  e
   $ xt 2test
+  5
   $ xt 3test
+  95
   $ xt 4test
+  36
   $ xt 5test
+  2
   $ xt 6test
+  14
   $ xt 7test
+  a1
   $ xt 8test
+  07
   $ xt 9test
+  0e
   $ xt 10test
+  b10
   $ xt 11test
+  6
   $ xt 12test
+  8
   $ xt 13test
+  c7
   $ xt 14test
+  ef
   $ xt
   07  - 8test
   0a  - 9
@@ -103,9 +137,11 @@
   fa  - 14
   fe  - 8
   $ xt -f b1
-  The ID "b1" matches more than one task.%
+  error: the ID "b1" matches more than one task
+  [1]
   $ xt -e b1
-  The ID "b1" matches more than one task.%
+  error: the ID "b1" matches more than one task
+  [1]
   $ xt
   07  - 8test
   0a  - 9
--- a/tests/filehandling.t	Tue Jun 25 07:21:47 2019 +0000
+++ b/tests/filehandling.t	Sat Apr 11 11:41:14 2020 -0400
@@ -12,6 +12,7 @@
   .
   ..
   $ xt Sample.
+  a
   $ ls -a
   .
   ..
@@ -30,6 +31,7 @@
 Finish a task with deleting:
 
   $ xt Another.
+  c
   $ xt --delete-if-empty -f c
   $ ls -a
   .
--- a/tests/finished.t	Tue Jun 25 07:21:47 2019 +0000
+++ b/tests/finished.t	Sat Apr 11 11:41:14 2020 -0400
@@ -5,10 +5,15 @@
 Add some tasks:
 
   $ xt Sample one.
+  3
   $ xt Sample two.
+  7
   $ xt Sample three.
+  9
   $ xt Sample four.
+  1
   $ xt 'this | that'
+  4
 
 Finish and test .test.done:
 
--- a/tests/padding.t	Tue Jun 25 07:21:47 2019 +0000
+++ b/tests/padding.t	Sat Apr 11 11:41:14 2020 -0400
@@ -5,7 +5,9 @@
 Add tasks of varying width:
 
   $ xt Short.
+  5
   $ xt Longcat is long.
+  4
 
 Test paddings:
 
--- a/tests/taskdirs.t	Tue Jun 25 07:21:47 2019 +0000
+++ b/tests/taskdirs.t	Sat Apr 11 11:41:14 2020 -0400
@@ -7,7 +7,9 @@
   $ mkdir beer
   $ mkdir books
   $ xt --task-dir beer Dogfish Head 120 minute IPA
+  7
   $ xt --task-dir books Your Inner Fish
+  0
   $ xt --task-dir beer
   7 - Dogfish Head 120 minute IPA
   $ xt --task-dir books
@@ -16,9 +18,11 @@
 Wrong directories:
 
   $ xt --task-dir beer -f 0
-  The ID "0" does not match any task.%
+  error: the ID "0" does not match any task
+  [1]
   $ xt --task-dir books -f 7
-  The ID "7" does not match any task.%
+  error: the ID "7" does not match any task
+  [1]
   $ xt --task-dir beer
   7 - Dogfish Head 120 minute IPA
   $ xt --task-dir books
--- a/tests/tasklists.t	Tue Jun 25 07:21:47 2019 +0000
+++ b/tests/tasklists.t	Sat Apr 11 11:41:14 2020 -0400
@@ -5,7 +5,9 @@
 Initialize multiple task lists:
 
   $ xt --list beer Dogfish Head 120 minute IPA
+  7
   $ xt --list books Your Inner Fish
+  0
   $ xt --list beer
   7 - Dogfish Head 120 minute IPA
   $ xt --list books
@@ -14,9 +16,11 @@
 Wrong lists:
 
   $ xt --list beer -f 0
-  The ID "0" does not match any task.%
+  error: the ID "0" does not match any task
+  [1]
   $ xt --list books -f 7
-  The ID "7" does not match any task.%
+  error: the ID "7" does not match any task
+  [1]
   $ xt --list beer
   7 - Dogfish Head 120 minute IPA
   $ xt --list books