#!/usr/bin/env python3

# http://inamidst.com/saxo/
# Created by Sean B. Palmer

import os
import saxo

HEXCODE = 0
CODEPOINT = 1
NAME = 2
CURRENT = 3
ANCIENT = 4
CATEGORY = 5
CHARACTER = 6
DISPLAY = 7

@saxo.pipe
def unicode_by_character(arg):
    ellipsis = False
    if len(arg) > 8:
        arg = arg[:8]
        ellipsis = True

    with saxo.database(dotdir=True) as db:
        if "saxo_unicode" not in db:
            return "The unicode database needs to be updated"

        info = []
        for character in arg:
            query = "SELECT * FROM saxo_unicode WHERE character = ?"
            rows = list(db.query(query, character))
            rows = rows or [tuple("????????")]
            info.append(rows[0])

    if len(info) == 1:
        row = info[0]
        result = "U+%s %s (%s)" % (row[HEXCODE], row[NAME], row[DISPLAY])
    else:
        parts = ["U+%s" % row[HEXCODE] for row in info]
        result = " ".join(parts)

    if ellipsis is True:
        result += " ..."
    return result
