CLI tool to search unicode data backed by SQLite3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.2 KiB

// Copyright © 2015-2017 Daniele Tricoli <eriol@mornie.org>.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package format // import "eriol.xyz/piken/format"
import (
"fmt"
"reflect"
"strconv"
"strings"
"eriol.xyz/piken/sql"
)
type TextFormatter struct {
baseFormatter
Separator string
}
func NewTextFormatter(fields []string, separator string, glyph bool) *TextFormatter {
return &TextFormatter{
baseFormatter: baseFormatter{fields: fields, showGlyph: glyph},
Separator: separator}
}
func (t *TextFormatter) Format(s *sql.UnicodeData) (string, error) {
var buffer []string
glyph, err := CodePointToGlyph(s.CodePoint)
if err != nil {
return "", err
}
for _, field := range t.Fields() {
r := reflect.ValueOf(s)
f := reflect.Indirect(r).FieldByName(field)
buffer = append(buffer, f.String())
}
if t.ShowGlyph() {
buffer = append(buffer, glyph)
}
return strings.Join(buffer, t.Separator), nil
}
// Convert an unicode codepoint into a string.
func CodePointToGlyph(codepoint string) (string, error) {
s, err := strconv.ParseInt(codepoint, 16, 32)
if err != nil {
return "", err
}
return fmt.Sprintf("%c", s), nil
}