Skip to content

Commit

Permalink
Search all words, instead of substring
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Jan 5, 2019
1 parent 34cab9d commit 6b89ac2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
40 changes: 31 additions & 9 deletions README.markdown
Expand Up @@ -8,7 +8,7 @@ Identify a character:
cpoint dec utf-8 html name
'€' U+20AC 8364 0xe282ac € EURO SIGN

Or an entire string. "i" is a shortcut for "identify":
Or an entire string. `i` is a shortcut for `identify`:

$ uni i h€łłø
cpoint dec utf-8 html name
Expand Down Expand Up @@ -36,10 +36,6 @@ Or a range from a file:

Note that these are **byte** offsets, not *character* offsets:

$ uni i 'README.markdown:#128'

$ uni i 'README.markdown:#128-130'

$ uni i 'README.markdown:#130'
uni: WARNING: input string is not valid UTF-8
cpoint dec utf-8 html name
Expand All @@ -55,12 +51,38 @@ Search description:
cpoint dec utf-8 html name
'₠' U+20A0 8352 e2 82 a0 ₠ EURO-CURRENCY SIGN
'€' U+20AC 8364 e2 82 ac € EURO SIGN
'💶' U+1F4B6 128182 f0 9f 92 b6 💶 BANKNOTE WITH EURO SIGN
'𐡷' U+10877 67703 f0 90 a1 b7 𐡷 PALMYRENE LEFT-POINTING FLEURON
'🏰' U+1F3F0 127984 f0 9f 8f b0 🏰 EUROPEAN CASTLE
'𐫱' U+10AF1 68337 f0 90 ab b1 𐫱 MANICHAEAN PUNCTUATION FLEURON
'𐡸' U+10878 67704 f0 90 a1 b8 𐡸 PALMYRENE RIGHT-POINTING FLEURON
'𐫱' U+10AF1 68337 f0 90 ab b1 𐫱 MANICHAEAN PUNCTUATION FLEURON
'🌍' U+1F30D 127757 f0 9f 8c 8d 🌍 EARTH GLOBE EUROPE-AFRICA
'🏤' U+1F3E4 127972 f0 9f 8f a4 🏤 EUROPEAN POST OFFICE
'🏰' U+1F3F0 127984 f0 9f 8f b0 🏰 EUROPEAN CASTLE
'💶' U+1F4B6 128182 f0 9f 92 b6 💶 BANKNOTE WITH EURO SIGN

The `s` command is a shortcut for `search`. Multiple words are matched
individually:

$ uni s earth globe
cpoint dec utf-8 html name
'🌍' U+1F30D 127757 f0 9f 8c 8d 🌍 EARTH GLOBE EUROPE-AFRICA
'🌎' U+1F30E 127758 f0 9f 8c 8e 🌎 EARTH GLOBE AMERICAS
'🌏' U+1F30F 127759 f0 9f 8c 8f 🌏 EARTH GLOBE ASIA-AUSTRALIA

$ uni s globe earth
cpoint dec utf-8 html name
'🌍' U+1F30D 127757 f0 9f 8c 8d 🌍 EARTH GLOBE EUROPE-AFRICA
'🌎' U+1F30E 127758 f0 9f 8c 8e 🌎 EARTH GLOBE AMERICAS
'🌏' U+1F30F 127759 f0 9f 8c 8f 🌏 EARTH GLOBE ASIA-AUSTRALIA

The `s` command is a shortcut for `search`.
Use standard shell quoting for more literal matches:

$ uni s rightwards black arrow
cpoint dec utf-8 html name
'➡' U+27A1 10145 e2 9e a1 ➡ BLACK RIGHTWARDS ARROW
'➤' U+27A4 10148 e2 9e a4 ➤ BLACK RIGHTWARDS ARROWHEAD
'➥' U+27A5 10149 e2 9e a5 ➥ HEAVY BLACK CURVED DOWNWARDS AND RIGHTWARDS ARROW
[..]

$ uni s 'rightwards black arrow'
cpoint dec utf-8 html name
'⮕' U+2B95 11157 e2 ae 95 ⮕ RIGHTWARDS BLACK ARROW
26 changes: 24 additions & 2 deletions uni.go
Expand Up @@ -48,22 +48,42 @@ var rangeNames = []string{
}

func main() {
// TODO: Better argument parsing.
// TODO: Add option to switch off header.
// TODO: Add option for TSV and/or JSON output.
if len(os.Args) < 2 {
fatal(errors.New("wrong arguments"))
}

// TODO: idea: add command to print/search emojis?
switch strings.ToLower(os.Args[1]) {
default:
fatal(errors.New("wrong arguments"))

case "identify", "i":
identify(strings.Join(os.Args[2:], ""))

// TODO: stable output, ordered by code point (due to map it's not stable
// now).
case "search", "s":
if len(os.Args) < 3 {
fatal(errors.New("need search term"))
}

// TODO: don't print with 0 matches.
header()
q := strings.ToUpper(strings.Join(os.Args[2:], " "))
words := make([]string, len(os.Args)-2)
for i := range os.Args[2:] {
words[i] = strings.ToUpper(os.Args[i+2])
}
for cp, name := range uniData {
if strings.Contains(name, q) {
m := 0
for _, w := range words {
if strings.Contains(name, w) {
m++
}
}
if m == len(words) {
printEntry(cp, name)
}
}
Expand Down Expand Up @@ -125,6 +145,7 @@ func identify(in string) {
_, _ = fmt.Fprintf(os.Stderr, "uni: WARNING: input string is not valid UTF-8\n")
}

// TODO: don't print with 0 matches.
header()
for _, c := range in {
find := fmt.Sprintf("%04X", c)
Expand Down Expand Up @@ -159,6 +180,7 @@ func inRange(c rune) string {
return ""
}

// TODO: check terminal size; don't print full descriptin if it doesn't fit.
func printEntry(cp, name string) {
r, _ := strconv.ParseInt(cp, 16, 64)
rs := strconv.FormatInt(r, 10)
Expand Down

0 comments on commit 6b89ac2

Please sign in to comment.