# shellcheck shell=sh        disable=SC2039,SC1090,SC3043,SC2263
# x-cmd cve lib/la/fmt — output formatters for `x cve ls` rows.
#
# All formatters take tab-separated rows on stdin and emit a different
# representation on stdout. Centralized here so adding a new format is a
# one-file change and the dispatcher in _index stays terse.

# Render one row as a small fixed-width table (used in fzf).
# Layout: "CVE  SCORE  GHSA  DESC"
___x_cmd_cve_la_fmt_row(){
    awk -F '\t' '
        {
            cve   = $1
            score = ($5 == "" ? "  - " : sprintf("%4s", $5))
            ghsa  = ($4 == "" ? "       " : substr($4, 1, 19))
            desc  = $6
            printf "%-18s %5s  %-19s  %s\n", cve, score, ghsa, desc
        }
    '
}

# Pretty fixed-width table with a header row.
___x_cmd_cve_la_fmt_table(){
    printf "%-18s %5s  %-19s  %s\n" "CVE" "SCORE" "GHSA" "DESC"
    ___x_cmd_cve_la_fmt_row
}

# Pretty multi-line blocks (for `x cve ls --yml`).
___x_cmd_cve_la_fmt_yml(){
    awk -F '\t' '
        {
            printf "cve:      %s\nyear:     %s\nno:       %s\nghsa:     %s\nscore:    %s\n",
                   $1, $2, $3, ($4 == "" ? "—" : $4), ($5 == "" ? "—" : $5)
            if ($6 != "") printf "desc:     %s\n", $6
            printf "\n"
        }
    '
}

___x_cmd_cve_la_fmt_json(){
    awk -F '\t' '
        function esc(s) {
            gsub(/\\/, "\\\\", s)
            gsub(/"/,  "\\\"", s)
            return s
        }
        {
            printf "{\"cve\":\"%s\",\"year\":\"%s\",\"no\":\"%s\",\"ghsa\":\"%s\",\"score\":\"%s\",\"desc\":\"%s\"}\n",
                   $1, $2, $3, $4, $5, esc($6)
        }
    '
}

___x_cmd_cve_la_fmt_csv(){
    awk -F '\t' '
        { gsub(/"/, "\"\"", $6); printf "%s,%s,%s,%s,%s,\"%s\"\n", $1, $2, $3, $4, $5, $6 }
    '
}