diff options
author | Cody Hiar <codyfh@gmail.com> | 2018-03-23 20:31:56 -0600 |
---|---|---|
committer | Cody Hiar <codyfh@gmail.com> | 2018-03-23 20:31:56 -0600 |
commit | e3fb796429c674f2b15a3320144ecd3fbf3139f0 (patch) | |
tree | e5a26ba184bd212f63afc2c9fe3e7927275298fa /completers.py | |
parent | 20cb5be7c9e8b1542ae86a8541bc65b4cb33ee6b (diff) |
Split cli into different files, wrap keyboard exception
Diffstat (limited to 'completers.py')
-rw-r--r-- | completers.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/completers.py b/completers.py new file mode 100644 index 0000000..9e6bb91 --- /dev/null +++ b/completers.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- +"""Custom Completers for better cli selections.""" +import datetime + +from fuzzyfinder import fuzzyfinder +from prompt_toolkit.completion import Completer, Completion + + +class FuzzyCompleter(Completer): + """Fuzzy Completer Alpha Sorted.""" + + def __init__(self, words): + """Initialize.""" + self.words = words + + def get_completions(self, document, complete_event): + """Use fuzzyfinder for completions.""" + word_before_cursor = document.text_before_cursor + words = fuzzyfinder(word_before_cursor, self.words) + for x in words: + yield Completion(x, -len(word_before_cursor)) + + +class DateFuzzyCompleter(Completer): + """Fuzzy Completer For Dates.""" + + def get_completions(self, document, complete_event): + """Use fuzzyfinder for date completions. + + The fuzzyfind auto sorts by alpha so this is to show dates relative to + the current date instead of by day of week. + """ + base = datetime.datetime.today() + date_format = '%a, %Y-%m-%d' + date_list = [(base - datetime.timedelta(days=x)).strftime(date_format) + for x in range(0, 30)] + word_before_cursor = document.text_before_cursor + words = fuzzyfinder(word_before_cursor, date_list) + + def sort_by_date(date_str: str): + return datetime.datetime.strptime(date_str, date_format) + + # Re-sort by date rather than day name + words = sorted(words, key=sort_by_date, reverse=True) + for x in words: + yield Completion(x, -len(word_before_cursor)) + + +class WeekFuzzyCompleter(Completer): + """Fuzzy Completer For Weeks.""" + + def get_completions(self, document, complete_event): + """Use fuzzyfinder for week completions.""" + def datetime_to_week_str(dt: datetime.datetime): + """Convert a datetime to weekstring. + + datetime.datetime(2018, 2, 26, 0, 0) => '2018-02-26 to 2018-03-04' + """ + if dt.weekday() != 0: + monday_dt = dt - datetime.timedelta(days=dt.weekday()) + else: + monday_dt = dt + sunday_dt = monday_dt + datetime.timedelta(days=6) + return '{} to {}'.format( + monday_dt.strftime('%Y-%m-%d'), sunday_dt.strftime('%Y-%m-%d')) + + base = datetime.datetime.today() + week_list = [datetime_to_week_str(base - datetime.timedelta(weeks=x)) + for x in range(0, 5)] + word_before_cursor = document.text_before_cursor + words = fuzzyfinder(word_before_cursor, week_list) + words = sorted(words, reverse=True) + for x in words: + yield Completion(x, -len(word_before_cursor)) |