Skip to content

Commit a532edf

Browse files
committed
Add markdown widget
1 parent dd626d8 commit a532edf

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version = "0.1.0"
55

66
[deps]
77
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
8+
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
89
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
910
TERMIOS = "10233e3b-0d6c-416a-8c1a-d3d286bd6cd2"
1011
TextWrap = "b718987f-49a8-5099-9789-dcd902bef87d"

src/TerminalUserInterfaces.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ include("widgets/widgets.jl")
2323

2424
include("widgets/block.jl")
2525

26+
include("widgets/markdown.jl")
2627
include("widgets/paragraph.jl")
2728
include("widgets/progressbar.jl")
2829
include("widgets/list.jl")

src/widgets/markdown.jl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Markdown
2+
3+
Base.@kwdef struct MarkdownBlock
4+
block::Block
5+
text::Markdown.MD
6+
end
7+
8+
function draw(d::MarkdownBlock, area::Rect, buf::Buffer)
9+
draw(d.block, area, buf);
10+
text_area = inner(d.block, area)
11+
12+
title_area = Rect(left(area), top(area), width(area), height(area) ÷ 5)
13+
# Assume first block is always the header
14+
draw(d.text.content[1], title_area, buf)
15+
16+
body_area = Rect(left(area), bottom(title_area), width(area), height(area) - height(title_area))
17+
body_area = Rect(body_area, Margin(5, 5))
18+
items = d.text.content[2:end]
19+
inner_area = Rect(left(body_area), top(body_area) - height(body_area) ÷ length(items), width(body_area), height(body_area) ÷ length(items))
20+
for (i, c) in enumerate(d.text.content[2:end])
21+
inner_area = Rect(left(inner_area), bottom(inner_area), width(inner_area), height(inner_area))
22+
draw(c, inner_area, buf)
23+
end
24+
end
25+
26+
function draw(h::Markdown.Header, area::Rect, buf::Buffer)
27+
b = Block(border_type = BorderTypeLight)
28+
draw(b, area, buf);
29+
30+
title = h.text[1]
31+
32+
row = top(area) + height(area) ÷ 2
33+
col = width(area) ÷ 2 - ( length(title) ÷ 2 )
34+
set(buf, col, row, title, Crayon(bold = true))
35+
end
36+
37+
function draw(l::Markdown.List, area::Rect, buf::Buffer)
38+
39+
for (i, item) in enumerate(l.items)
40+
item_area = Rect(
41+
left(area),
42+
top(area) + (i - 1) * (height(area) ÷ length(l.items)),
43+
width(area),
44+
height(area) ÷ length(l.items)
45+
)
46+
row = top(item_area)
47+
col = left(item_area)
48+
set(buf, col, row, " * ", Crayon(bold = true, foreground = :red))
49+
inner_area = Rect(item_area, Margin(0, 5))
50+
draw(item[1], inner_area, buf)
51+
end
52+
53+
end
54+
55+
function draw(p::Markdown.Paragraph, area::Rect, buf::Buffer)
56+
57+
row = top(area)
58+
col = left(area)
59+
for c in p.content
60+
if typeof(c) == String
61+
l = length(c)
62+
set(buf, col, row, c)
63+
col += l
64+
elseif typeof(c) == Markdown.Bold
65+
l = length(c.text[1])
66+
set(buf, col, row, c.text[1], Crayon(bold = true))
67+
col += l
68+
elseif typeof(c) == Markdown.Italic
69+
l = length(c.text[1])
70+
set(buf, col, row, c.text[1], Crayon(italics = true))
71+
col += l
72+
end
73+
end
74+
75+
end

0 commit comments

Comments
 (0)