Repository: comonicon/Comonicon.jl
Pull request #199 (2022-05-15 20:12:48)

polish printing
Polish printing of subcmds

codecov[bot]: # [Codecov](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#199](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (87c285c) into [master](https://codecov.io/gh/comonicon/Comonicon.jl/commit/0cf687e7f298fe503855b37571d4fe3184f127aa?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (0cf687e) will **decrease** coverage by `81.99%`.
> The diff coverage is `0.00%`.

```diff
@@            Coverage Diff             @@
##           master    #199       +/-   ##
==========================================
- Coverage   82.92%   0.93%   -82.00%     
==========================================
  Files          21      20        -1     
  Lines        1423    1396       -27     
==========================================
- Hits         1180      13     -1167     
- Misses        243    1383     +1140     
```


| [Impacted Files](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [src/ast/printing.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2FzdC9wcmludGluZy5qbA==) | `0.00% <0.00%> (-89.10%)` | :arrow_down: |
| [src/ast/utils.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2FzdC91dGlscy5qbA==) | `0.00% <0.00%> (-84.85%)` | :arrow_down: |
| [src/compat.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2NvbXBhdC5qbA==) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [src/exceptions.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2V4Y2VwdGlvbnMuamw=) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [src/builder/app.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2J1aWxkZXIvYXBwLmps) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [src/frontend/markdown.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2Zyb250ZW5kL21hcmtkb3duLmps) | `0.00% <0.00%> (-94.12%)` | :arrow_down: |
| [src/builder/install.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2J1aWxkZXIvaW5zdGFsbC5qbA==) | `0.00% <0.00%> (-93.25%)` | :arrow_down: |
| [src/codegen/zsh.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2NvZGVnZW4venNoLmps) | `0.00% <0.00%> (-92.11%)` | :arrow_down: |
| [src/codegen/bash.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2NvZGVnZW4vYmFzaC5qbA==) | `0.00% <0.00%> (-90.48%)` | :arrow_down: |
| [src/builder/cli.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2J1aWxkZXIvY2xpLmps) | `0.00% <0.00%> (-89.71%)` | :arrow_down: |
| ... and [12 more](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Last update [0cf687e...87c285c](https://codecov.io/gh/comonicon/Comonicon.jl/pull/199?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).


Patch:
diff --git a/src/ast/printing.jl b/src/ast/printing.jl
index 757cda9..3241e09 100644
--- a/src/ast/printing.jl
+++ b/src/ast/printing.jl
@@ -80,6 +80,8 @@ end
 
 function print_content(io::IO, desc::Description, t::Terminal)
     isnothing(desc.content) || print_within(io, desc.content, t.width, 0)
+    # if Intro section is empty, use brief description
+    isnothing(desc.brief) || print_within(io, desc.brief, t.width, 0)
     return
 end
 
@@ -169,7 +171,7 @@ function print_body(io::IO, cmd::NodeCommand, t::Terminal)
     section(io, "Commands")
     for each in values(cmd.subcmds)
         print_name_brief(io, each, t)
-        println(io)
+        section(io)
     end
     section(io, "Flags")
     print_help(io, t)
@@ -239,8 +241,14 @@ function print_with_brief(f, io::IO, cmd, t::Terminal)
 
     print(io, tab(2))
     f(io, cmd, t)
-    isnothing(cmd.description.brief) && return
-    print_indent_content(io, cmd.description.brief, t, firstline)
+
+    if isnothing(cmd.description.brief)
+        isnothing(cmd.description.content) && return
+        brief = content_brief(cmd.description.content; max_width=t.right)
+    else
+        brief = cmd.description.brief
+    end
+    print_indent_content(io, brief, t, firstline)
     return
 end
 
diff --git a/src/ast/utils.jl b/src/ast/utils.jl
index 0785d82..303a975 100644
--- a/src/ast/utils.jl
+++ b/src/ast/utils.jl
@@ -61,3 +61,18 @@ function splitlines(s, width = 80)
     isempty(current_line) || push!(lines, strip(join(current_line)))
     return lines
 end
+
+function content_brief(s; max_width = 80)
+    words = splittext(s)
+    brief = String[]
+    char_count = 0
+    for word in words
+        char_count += length(word) + 1
+        # length should be max_width-3
+        # but we overcount a space for
+        # the last word.
+        char_count > max_width-2 && break
+        push!(brief, word)
+    end
+    return join(brief, ' ') * "..."
+end


Test patch:
diff --git a/test/ast/utils.jl b/test/ast/utils.jl
new file mode 100644
index 0000000..7b8cf5b
--- /dev/null
+++ b/test/ast/utils.jl
@@ -0,0 +1,9 @@
+using Test
+using Faker
+using Comonicon.AST
+
+@testset "content_brief" begin
+    for _ in 1:10
+        @test length(AST.content_brief(Faker.text(); max_width=80)) ≤ 80
+    end
+end
diff --git a/test/runtests.jl b/test/runtests.jl
index fc32cc3..0a01100 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -16,6 +16,7 @@ end
 
 @testset "ast" begin
     include("ast/ast.jl")
+    include("ast/utils.jl")
 end
 
 @testset "frontend" begin