1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v doc: implement color highlighting for the stdout format, enable it by default (#9312)

This commit is contained in:
Swastik Baranwal
2021-03-15 16:51:19 +05:30
committed by GitHub
parent 2d2e4610e7
commit 26138f98af
3 changed files with 132 additions and 2 deletions

View File

@ -11,6 +11,7 @@ import v.doc
import v.pref
import v.vmod
import json
import term
const (
allowed_formats = ['md', 'markdown', 'json', 'text', 'stdout', 'html', 'htm']
@ -44,6 +45,7 @@ struct Config {
mut:
pub_only bool = true
show_loc bool // for plaintext
is_color bool
is_multi bool
is_vlib bool
is_verbose bool
@ -86,7 +88,12 @@ fn (vd VDoc) gen_json(d doc.Doc) string {
fn (vd VDoc) gen_plaintext(d doc.Doc) string {
cfg := vd.cfg
mut pw := strings.new_builder(200)
pw.writeln('$d.head.content\n')
if cfg.is_color {
content_arr := d.head.content.split(' ')
pw.writeln('${term.blue(content_arr[0])} ${term.green(content_arr[1])}\n')
} else {
pw.writeln('$d.head.content\n')
}
comments := if cfg.include_examples {
d.head.merge_comments()
} else {
@ -103,7 +110,11 @@ fn (vd VDoc) write_plaintext_content(contents []doc.DocNode, mut pw strings.Buil
cfg := vd.cfg
for cn in contents {
if cn.content.len > 0 {
pw.writeln(cn.content)
if cfg.is_color {
pw.writeln(color_highlight(cn.content, vd.docs[0].table))
} else {
pw.writeln(cn.content)
}
if cn.comments.len > 0 && !cfg.pub_only {
comments := if cfg.include_examples {
cn.merge_comments()
@ -369,6 +380,7 @@ fn (vd VDoc) vprintln(str string) {
fn parse_arguments(args []string) Config {
mut cfg := Config{}
cfg.is_color = term.can_show_color_on_stdout()
for i := 0; i < args.len; i++ {
arg := args[i]
current_args := args[i..]
@ -386,6 +398,12 @@ fn parse_arguments(args []string) Config {
cfg.output_type = set_output_type_from_str(format)
i++
}
'-color' {
cfg.is_color = true
}
'-no-color' {
cfg.is_color = false
}
'-inline-assets' {
cfg.inline_assets = true
}