LCOV - code coverage report
Current view: top level - messagedata - markup.go Coverage Total Hit
Test: coverage.lcov Lines: 90.2 % 51 46
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package messagedata
       2              : 
       3              : import (
       4              :         "image/color"
       5              :         "strings"
       6              : 
       7              :         "github.com/kijimaD/ruins/internal/gamelog"
       8              : )
       9              : 
      10              : // markupStyle はマークアップタグの前景色と背景色。背景色は keyword のみ持つ
      11              : type markupStyle struct {
      12              :         fg *color.RGBA
      13              :         bg *color.RGBA
      14              : }
      15              : 
      16            1 : func fg(c color.RGBA) markupStyle { return markupStyle{fg: &c} }
      17              : 
      18              : // tagStyles はメッセージ用マークアップのタグをスタイルへ対応させる。
      19              : // 前景色は gamelog と同じパレットで揃え、keyword だけ赤文字+暗赤背景で強調する
      20              : var tagStyles = map[string]markupStyle{
      21              :         "keyword":  {fg: &color.RGBA{R: 255, G: 100, B: 100, A: 255}, bg: &color.RGBA{R: 80, G: 20, B: 20, A: 180}},
      22              :         "item":     fg(gamelog.ColorCyan),
      23              :         "player":   fg(gamelog.ColorGreen),
      24              :         "npc":      fg(gamelog.ColorYellow),
      25              :         "damage":   fg(gamelog.ColorRed),
      26              :         "success":  fg(gamelog.ColorGreen),
      27              :         "warning":  fg(gamelog.ColorYellow),
      28              :         "error":    fg(gamelog.ColorRed),
      29              :         "location": fg(gamelog.ColorOrange),
      30              :         "system":   fg(gamelog.ColorCyan),
      31              : }
      32              : 
      33              : var namedColors = map[string]color.RGBA{
      34              :         "white": gamelog.ColorWhite, "red": gamelog.ColorRed, "green": gamelog.ColorGreen,
      35              :         "blue": gamelog.ColorBlue, "yellow": gamelog.ColorYellow, "cyan": gamelog.ColorCyan,
      36              :         "magenta": gamelog.ColorMagenta, "orange": gamelog.ColorOrange, "purple": gamelog.ColorPurple,
      37              : }
      38              : 
      39              : func tagStyle(tag string) (markupStyle, bool) {
      40            1 :         if s, ok := tagStyles[tag]; ok {
      41            1 :                 return s, true
      42            1 :         }
      43            1 :         if name, ok := strings.CutPrefix(tag, "color_"); ok {
      44            0 :                 if c, ok := namedColors[name]; ok {
      45            0 :                         return fg(c), true
      46            0 :                 }
      47              :         }
      48            1 :         return markupStyle{}, false
      49              : }
      50              : 
      51              : // AddMarkup は <tag>...</tag> マークアップ付きテキストを追加する。
      52              : // タグの外は既定色、keyword は赤文字+背景、意味タグは前景色になる。
      53              : // 改行 \n は行分割する。未知タグや閉じ忘れは literal として本文に残す
      54              : func (m *MessageData) AddMarkup(text string) *MessageData {
      55            1 :         m.ensureCurrentLine()
      56            1 :         for _, seg := range parseMarkupSegments(text) {
      57              :                 // 断片ごとに \n で行を割る。AddText と同じく空文字の断片も落とさない
      58            1 :                 lines := strings.Split(seg.text, "\n")
      59            1 :                 for i, line := range lines {
      60            1 :                         if i > 0 {
      61            1 :                                 m.TextSegmentLines = append(m.TextSegmentLines, []TextSegment{})
      62            1 :                         }
      63            1 :                         idx := len(m.TextSegmentLines) - 1
      64            1 :                         m.TextSegmentLines[idx] = append(m.TextSegmentLines[idx], TextSegment{
      65            1 :                                 Text:            line,
      66            1 :                                 Color:           seg.style.fg,
      67            1 :                                 BackgroundColor: seg.style.bg,
      68            1 :                         })
      69              :                 }
      70              :         }
      71            1 :         return m
      72              : }
      73              : 
      74              : type markupSegment struct {
      75              :         text  string
      76              :         style markupStyle
      77              : }
      78              : 
      79              : // parseMarkupSegments は <tag>...</tag> をスタイル付き断片へ分解する。
      80              : // タグの外は既定スタイル、対応しないタグや閉じ括弧の無いマークは literal として残す
      81              : func parseMarkupSegments(s string) []markupSegment {
      82            1 :         var segs []markupSegment
      83            1 :         add := func(text string, style markupStyle) {
      84            1 :                 if text != "" {
      85            1 :                         segs = append(segs, markupSegment{text: text, style: style})
      86            1 :                 }
      87              :         }
      88            1 :         for len(s) > 0 {
      89            1 :                 open := strings.IndexByte(s, '<')
      90            1 :                 if open < 0 {
      91            1 :                         add(s, markupStyle{})
      92            1 :                         break
      93              :                 }
      94            1 :                 add(s[:open], markupStyle{})
      95            1 :                 s = s[open:]
      96            1 : 
      97            1 :                 gt := strings.IndexByte(s, '>')
      98            1 :                 if gt < 0 {
      99            0 :                         add(s, markupStyle{})
     100            0 :                         break
     101              :                 }
     102            1 :                 tag := s[1:gt]
     103            1 :                 style, known := tagStyle(tag)
     104            1 :                 closeTag := "</" + tag + ">"
     105            1 :                 ci := strings.Index(s[gt+1:], closeTag)
     106            1 :                 if !known || ci < 0 {
     107            1 :                         add(s[:gt+1], markupStyle{})
     108            1 :                         s = s[gt+1:]
     109            1 :                         continue
     110              :                 }
     111            1 :                 add(s[gt+1:gt+1+ci], style)
     112            1 :                 s = s[gt+1+ci+len(closeTag):]
     113              :         }
     114            1 :         return segs
     115              : }
        

Generated by: LCOV version 2.0-1