nandi/frqpublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/nandi/frq.git
git clone ssh://git@rickub.com/nandi/frq.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

The chat screen, in Nim 87a28cc · on main · nandi · 6h ago
ttextruns.nim · 72 lines · 2.7 KBNim Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
## Link detection in message text — the part of the chat screen that is pure
## string work, and the part most worth pinning down.

import std/[sequtils, unittest]
import frq/textruns

proc kinds(s: string): seq[RunKind] = textRuns(s).mapIt(it.kind)
proc values(s: string): seq[string] = textRuns(s).mapIt(it.value)

suite "textRuns":
  test "plain text is one run":
    check kinds("hello there") == @[rkText]
    check values("hello there") == @["hello there"]

  test "a bare URL is one link":
    check kinds("https://example.com") == @[rkLink]

  test "text around a link":
    check kinds("see https://example.com now") == @[rkText, rkLink, rkText]
    check values("see https://example.com now") ==
      @["see ", "https://example.com", " now"]

  test "two links in one line":
    check kinds("a http://x.com b https://y.com") ==
      @[rkText, rkLink, rkText, rkLink]

  test "http as well as https":
    check kinds("http://example.com") == @[rkLink]

  test "the message's own ends are trimmed":
    check values("  hello  ") == @["hello"]

  test "but spaces between words are somebody's typing":
    check values("a  b") == @["a  b"]

  test "empty text is no runs":
    check textRuns("").len == 0

  test "whitespace-only text is no runs, not an empty one":
    check textRuns("   ").len == 0

suite "trimTrailingPunctuation":
  test "a sentence's full stop is not part of the URL":
    check trimTrailingPunctuation("https://x.com.") == "https://x.com"
    check trimTrailingPunctuation("https://x.com,") == "https://x.com"
    check trimTrailingPunctuation("https://x.com?") == "https://x.com"

  test "several at once":
    check trimTrailingPunctuation("https://x.com...") == "https://x.com"

  test "a closing bracket goes when the URL opened none":
    check trimTrailingPunctuation("https://x.com)") == "https://x.com"

  test "but stays when it did — a wikipedia path keeps its brackets":
    check trimTrailingPunctuation("https://en.wikipedia.org/wiki/Foo_(bar)") ==
      "https://en.wikipedia.org/wiki/Foo_(bar)"

  test "the trimmed punctuation comes back as text":
    # Not dropped: somebody typed it.
    check values("see https://x.com. ok") == @["see ", "https://x.com", ". ok"]

suite "firstImageUrl":
  test "a png link":
    check firstImageUrl("look https://x.com/a.png yes") == "https://x.com/a.png"
  test "a freeq media link":
    check firstImageUrl("https://irc.freeq.at/api/v1/media/a/b/picture.png") ==
      "https://irc.freeq.at/api/v1/media/a/b/picture.png"
  test "no picture at all":
    check firstImageUrl("just words and https://x.com/page") == ""
  test "the first one wins":
    check firstImageUrl("https://a.com/1.png https://b.com/2.png") ==
      "https://a.com/1.png"