Bladeren bron

Switch to termcolor for color management

theenglishway (time) 6 jaren geleden
bovenliggende
commit
e246e76c27
7 gewijzigde bestanden met toevoegingen van 53 en 10 verwijderingen
  1. 5 4
      planner/cli.py
  2. 1 1
      planner/models/comments.py
  3. 21 1
      planner/models/items.py
  4. 1 1
      planner/models/milestones.py
  5. 3 2
      planner/models/mixins.py
  6. 1 1
      planner/models/sprints.py
  7. 21 0
      planner/utils.py

+ 5 - 4
planner/cli.py

@@ -3,6 +3,7 @@ import json
 from tinydb import Query
 from planner import db
 from planner.models import *
+from planner.utils import Term
 
 
 @click.group()
@@ -42,9 +43,9 @@ def save_or_display_errors(model, data):
     if ok :
         m = model.get(m_doc)
         m.save()
-        click.secho(f"{m} was successfully added", fg='green')
+        click.echo(Term.success(f"{m} was successfully added"))
     else:
-        click.secho(f"Errors : {m_doc}", fg='red')
+        click.echo(Term.failure(f"Errors : {m_doc}"))
 
 
 @milestone.command('new')
@@ -158,10 +159,10 @@ def edit(ctx, short_uuid):
         if edited_json:
             updated_instance = json.loads(edited_json)
             db.table(model.class_name()).update(updated_instance, doc_ids=[result.doc_id])
-            click.secho(f"Successfully updated {updated_instance}", fg='green')
+            click.echo(Term.success(f"Successfully updated {updated_instance}"))
 
     else:
-        click.secho(f"No matching result found", fg='red')
+        click.echo(Term.failure(f"No matching result found"))
 
 
 milestone.add_command(list)

+ 1 - 1
planner/models/comments.py

@@ -8,7 +8,7 @@ from .items import Item
 @dataclass(repr=False)
 class Comment(TemplateMixin, SchemaMixin, DbMixin):
     cache_keys = ('item_id', 'date_added')
-    template_term = """{{ obj.text }} {{ Fore.MAGENTA }}{{ obj.short_uuid }}{{ Style.RESET_ALL }}"""
+    template_term = """{{ obj.text }} {{ Term.uuid(obj.short_uuid) }}"""
 
     document: dict
     text: str

+ 21 - 1
planner/models/items.py

@@ -7,12 +7,13 @@ from .mixins import TemplateMixin, SchemaMixin, DbMixin
 from .milestones import Milestone
 from .sprints import Sprint
 from planner import db
+from planner.utils import Term
 
 
 @dataclass(repr=False)
 class Item(TemplateMixin, SchemaMixin, DbMixin):
     cache_keys = ('name', 'milestone_id', 'sprint_id')
-    template_term = """{{ obj.name }} {{ Fore.MAGENTA }}{{ obj.short_uuid }}{{ Style.RESET_ALL }}
+    template_term = """{{ obj.name }} {{ Term.uuid(obj.short_uuid) }} ({{ obj.term_priority }}, {{ obj.term_length }})
     {% for comment in obj.comments %}
         {{- comment.as_term }}
     {%- endfor %}"""
@@ -111,6 +112,25 @@ class Item(TemplateMixin, SchemaMixin, DbMixin):
 
         return category_to_kls[document.get('category')]
 
+    @property
+    def term_priority(self):
+        if '-' in self.priority:
+            return Term.not_important(self.priority)
+        elif '+' in self.priority:
+            return Term.important(self.priority)
+        else:
+            return Term.mildly_important(self.priority)
+
+
+    @property
+    def term_length(self):
+        if self.length in ['minutes', 'hours']:
+            return Term.not_important(self.length)
+        elif self.length in ['weeks', 'months']:
+            return Term.important(self.length)
+        else:
+            return Term.mildly_important(self.length or '')
+
     def __repr__(self):
         return "{}(name='{}', milestone='{}')".format(self.__class__.__qualname__, self.name, repr(self.milestone))
 

+ 1 - 1
planner/models/milestones.py

@@ -12,7 +12,7 @@ from .mixins import TemplateMixin, SchemaMixin, DbMixin
 @dataclass(repr=False)
 class Milestone(TemplateMixin, SchemaMixin, DbMixin):
     cache_keys = ('name',)
-    template_term = """Milestone '{{- obj.name }}' {{ Fore.MAGENTA }}{{ obj.short_uuid }}{{ Style.RESET_ALL }}
+    template_term = """Milestone '{{- obj.name }}' {{ Term.uuid(obj.short_uuid) }}
     {% for item in obj.items %}
         {{- item.as_term }}
     {% endfor %}"""

+ 3 - 2
planner/models/mixins.py

@@ -1,8 +1,9 @@
 import yaml
 from jinja2 import Environment
 import uuid
-from colorama import Fore, Back, Style
 from planner import db, validator
+from planner.utils import Term
+
 
 
 class TemplateMixin:
@@ -13,7 +14,7 @@ class TemplateMixin:
     def as_term(self):
         return self.env.from_string(
             self.template_term,
-            globals={'Fore': Fore, 'Back': Back, 'Style': Style}
+            globals={'Term': Term}
         ).render(obj=self)
 
     @property

+ 1 - 1
planner/models/sprints.py

@@ -11,7 +11,7 @@ from planner import db
 @dataclass(repr=False)
 class Sprint(TemplateMixin, SchemaMixin, DbMixin):
     cache_keys = ('name',)
-    template_term = """Sprint '{{- obj.name }}' {{ Fore.MAGENTA }}{{ obj.short_uuid }}{{ Style.RESET_ALL }}
+    template_term = """Sprint '{{- obj.name }}' {{ Term.uuid(obj.short_uuid) }}
     {% for item in obj.items %}
         {{- item.as_term }}
     {% endfor %}"""

+ 21 - 0
planner/utils.py

@@ -0,0 +1,21 @@
+from termcolor import colored
+
+
+class Term:
+    def success(text):
+        return colored(text, 'green')
+
+    def failure(text):
+        return colored(text, 'red')
+
+    def important(text):
+        return colored(text, 'red')
+
+    def not_important(text):
+        return colored(text, 'green')
+
+    def mildly_important(text):
+        return colored(text, 'yellow')
+
+    def uuid(text):
+        return colored(text, 'magenta')