def update_suggestions(self, text=""):
        """
            * from previous activity | set time | minutes ago | start now
            * to ongoing | set time

            * activity
            * [@category]
            * #tags, #tags, #tags

            * we will leave description for later

            all our magic is space separated, strictly, start-end can be just dash

            phases:

            [start_time] | [-end_time] | activity | [@category] | [#tag]
        """

        res = []

        fact = Fact(text)
        now = dt.datetime.now()

        # figure out what we are looking for
        # time -> activity[@category] -> tags -> description
        # presence of an attribute means that we are not looking for the previous one
        # we still might be looking for the current one though
        looking_for = "start_time"
        fields = ["start_time", "end_time", "activity", "category", "tags", "description", "done"]
        for field in reversed(fields):
            if getattr(fact, field, None):
                looking_for = field
                if text[-1] == " ":
                    looking_for = fields[fields.index(field)+1]
                break


        fragments = [f for f in re.split("[\s|#]", text)]
        current_fragment = fragments[-1] if fragments else ""


        search = extract_search(text)

        matches = []
        for match, score in self.suggestions:
            if search in match:
                if match.startswith(search):
                    score += 10**8 # boost beginnings
                matches.append((match, score))

        # need to limit these guys, sorry
        matches = sorted(matches, key=lambda x: x[1], reverse=True)[:7]

        for match, score in matches:
            label = (fact.start_time or now).strftime("%H:%M")
            if fact.end_time:
                label += fact.end_time.strftime("-%H:%M")

            markup_label = label + " " + (stuff.escape_pango(match).replace(search, "<b>%s</b>" % search) if search else match)
            label += " " + match

            res.append(DataRow(markup_label, match, label))

        # list of tuples (description, variant)
        variants = []

        if self.original_fact:
            # editing an existing fact

            variant_fact = None
            if self.original_fact.end_time is None:
                description = "stop now"
                variant_fact = self.original_fact.copy()
                variant_fact.end_time = now
            elif self.original_fact == self.todays_facts[-1]:
                # that one is too dangerous, except for the last entry
                description = "keep up"
                # Do not use Fact(..., end_time=None): it would be a no-op
                variant_fact = self.original_fact.copy()
                variant_fact.end_time = None

            if variant_fact:
                variant = variant_fact.serialized(prepend_date=False)
                variants.append((description, variant))

        else:
            # brand new fact
            description = "start now"
            variant = now.strftime("%H:%M ")
            variants.append((description, variant))

            prev_fact = self.todays_facts[-1] if self.todays_facts else None
            if prev_fact and prev_fact.end_time:
                since = stuff.format_duration(now - prev_fact.end_time)
                description = "from previous activity, %s ago" % since
                variant = prev_fact.end_time.strftime("%H:%M ")
                variants.append((description, variant))

            description = "start activity -n minutes ago (1 or 3 digits allowed)"
            variant = "-"
            variants.append((description, variant))

        text = text.strip()
        if text:
            description = "clear"
            variant = ""
            variants.append((description, variant))

        for (description, variant) in variants:
            res.append(DataRow(variant, description=description))

        self.complete_tree.set_rows(res)
def update_suggestions ( self , text = "" ) : res = [ ] fact = Fact ( text ) now = dt . datetime . now ( ) looking_for = "start_time" fields = [ "start_time" , "end_time" , "activity" , "category" , "tags" , "description" , "done" ] for field in reversed ( fields ) : if getattr ( fact , field , None ) : looking_for = field if text [ - 1 ] == " " : looking_for = fields [ fields . index ( field ) + 1 ] break fragments = [ f for f in re . split ( "[\s|#]" , text ) ] current_fragment = fragments [ - 1 ] if fragments else "" search = extract_search ( text ) matches = [ ] for match , score in self . suggestions : if search in match : if match . startswith ( search ) : score += 10 ** 8 matches . append ( ( match , score ) ) matches = sorted ( matches , key = lambda x : x [ 1 ] , reverse = True ) [ : 7 ] for match , score in matches : label = ( fact . start_time or now ) . strftime ( "%H:%M" ) if fact . end_time : label += fact . end_time . strftime ( "-%H:%M" ) markup_label = label + " " + ( stuff . escape_pango ( match ) . replace ( search , "<b>%s</b>" % search ) if search else match ) label += " " + match res . append ( DataRow ( markup_label , match , label ) ) variants = [ ] if self . original_fact : variant_fact = None if self . original_fact . end_time is None : description = "stop now" variant_fact = self . original_fact . copy ( ) variant_fact . end_time = now elif self . original_fact == self . todays_facts [ - 1 ] : description = "keep up" variant_fact = self . original_fact . copy ( ) variant_fact . end_time = None if variant_fact : variant = variant_fact . serialized ( prepend_date = False ) variants . append ( ( description , variant ) ) else : description = "start now" variant = now . strftime ( "%H:%M " ) variants . append ( ( description , variant ) ) prev_fact = self . todays_facts [ - 1 ] if self . todays_facts else None if prev_fact and prev_fact . end_time : since = stuff . format_duration ( now - prev_fact . end_time ) description = "from previous activity, %s ago" % since variant = prev_fact . end_time . strftime ( "%H:%M " ) variants . append ( ( description , variant ) ) description = "start activity -n minutes ago (1 or 3 digits allowed)" variant = "-" variants . append ( ( description , variant ) ) text = text . strip ( ) if text : description = "clear" variant = "" variants . append ( ( description , variant ) ) for ( description , variant ) in variants : res . append ( DataRow ( variant , description = description ) ) self . complete_tree . set_rows ( res )
* from previous activity | set time | minutes ago | start now
            * to ongoing | set time

            * activity
            * [@category]
            * #tags, #tags, #tags

            * we will leave description for later

            all our magic is space separated, strictly, start-end can be just dash

            phases:

            [start_time] | [-end_time] | activity | [@category] | [#tag]