From ccdcb4301723d9910d036dfafef4f0062bf8bc45 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Tue, 12 Nov 2024 18:21:31 +0100 Subject: [PATCH] markdown: linting --- Markdown.go | 35 +++++++-------- examples/markdown/markdown.go | 82 +++++++++++++++++------------------ go.mod | 2 +- go.sum | 2 + 4 files changed, 60 insertions(+), 61 deletions(-) diff --git a/Markdown.go b/Markdown.go index 6b2d6b29..0f55f167 100644 --- a/Markdown.go +++ b/Markdown.go @@ -1,4 +1,3 @@ -//nolint:gocritic,govet,wsl,revive // this file is TODO. We don't want commentedOutCode lint issues here. package giu import ( @@ -22,6 +21,15 @@ func (m *markdownState) Dispose() { // noop } +// MarkdownWidget implements DearImGui markdown extension +// https://github.com/juliettef/imgui_markdown +// It is like LabelWidget but with md formatting. +type MarkdownWidget struct { + md string + id ID + headers [3]immarkdown.MarkdownHeadingFormat +} + func (m *MarkdownWidget) getState() *markdownState { if s := GetState[markdownState](Context, m.id); s != nil { return s @@ -29,12 +37,16 @@ func (m *MarkdownWidget) getState() *markdownState { newState := m.newState() SetState[markdownState](Context, m.id, newState) + return newState } func (m *MarkdownWidget) newState() *markdownState { cfg := immarkdown.NewEmptyMarkdownConfig() - fmtCb := immarkdown.MarkdownFormalCallback(immarkdown.DefaultMarkdownFormatCallback) + fmtCb := immarkdown.MarkdownFormalCallback(func(data *immarkdown.MarkdownFormatInfo, start bool) { + immarkdown.DefaultMarkdownFormatCallback(*data, start) + }) + cfg.SetFormatCallback(&fmtCb) imgCb := immarkdown.MarkdownImageCallback(func(data immarkdown.MarkdownLinkCallbackData) immarkdown.MarkdownImageData { @@ -45,6 +57,7 @@ func (m *MarkdownWidget) newState() *markdownState { result := mdLoadImage(link) m.getState().images[link] = result + return result }) @@ -56,15 +69,6 @@ func (m *MarkdownWidget) newState() *markdownState { } } -// MarkdownWidget implements DearImGui markdown extension -// https://github.com/juliettef/imgui_markdown -// It is like LabelWidget but with md formatting. -type MarkdownWidget struct { - md string - id ID - headers [3]immarkdown.MarkdownHeadingFormat -} - // Markdown creates new markdown widget. func Markdown(md string) *MarkdownWidget { return (&MarkdownWidget{ @@ -81,7 +85,7 @@ func Markdown(md string) *MarkdownWidget { // OnLink sets another than default link callback. // NOTE: due to cimgui-go's limitation https://github.com/AllenDang/cimgui-go?tab=readme-ov-file#callbacks // we clear MarkdownLinkCallback pool every frame. No further action from you should be required (just feel informed). -// ref (*MasterWindow).beforeRender +// ref (*MasterWindow).beforeRender. func (m *MarkdownWidget) OnLink(cb func(url string)) *MarkdownWidget { igCb := immarkdown.MarkdownLinkCallback(func(data immarkdown.MarkdownLinkCallbackData) { link := data.Link()[:data.LinkLength()] @@ -137,6 +141,7 @@ func mdLoadImage(path string) immarkdown.MarkdownImageData { case strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://"): // Load image from url client := &http.Client{Timeout: 5 * time.Second} + resp, respErr := client.Get(path) if respErr != nil { return *immarkdown.NewEmptyMarkdownImageData() @@ -161,11 +166,7 @@ func mdLoadImage(path string) immarkdown.MarkdownImageData { } size := img.Bounds() - - // if current workaround is save - var id imgui.TextureID - - id = backend.NewTextureFromRgba(img).ID + id := backend.NewTextureFromRgba(img).ID result := immarkdown.NewEmptyMarkdownImageData() result.SetUsertextureid(id) diff --git a/examples/markdown/markdown.go b/examples/markdown/markdown.go index b2a9bf60..0afe22f1 100644 --- a/examples/markdown/markdown.go +++ b/examples/markdown/markdown.go @@ -3,55 +3,51 @@ package main import ( - "strings" - "github.com/AllenDang/giu" ) +const defaultMd = ` +Wrapping: +Text wraps automatically. To add a new line, use 'Return'. + +Headers: +# H1 +## H2 +### H3 + +Emphasis: +*emphasis* +_emphasis_ +**strong emphasis** +__strong emphasis__ + +Indents: +On a new line, at the start of the line, add two spaces per indent. + Indent level 1 + Indent level 2 + +Unordered lists: +On a new line, at the start of the line, add two spaces, an asterisks and a space. +For nested lists, add two additional spaces in front of the asterisk per list level increment. + * Unordered List level 1 + * Unordered List level 2 + +Link: +Here is [a link to some cool website!](https://github.com/AllenDang/giu) you must click it! +Image: +![gopher image](./gopher.png) +![gopher image link](https://raw.githubusercontent.com/AllenDang/giu/master/examples/loadimage/gopher.png) + +Horizontal Rule: +*** +___ +` + var ( - markdown = getExampleMarkdownText() + markdown = defaultMd splitLayoutPos float32 = 320 ) -func getExampleMarkdownText() string { - return strings.Join([]string{ - "Wrapping:", - "Text wraps automatically. To add a new line, use 'Return'.", - "", - "Headers:", - "# H1", - "## H2", - "### H3", - "", - "Emphasis:", - "*emphasis*", - "_emphasis_", - "**strong emphasis**", - "__strong emphasis__", - "", - "Indents:", - "On a new line, at the start of the line, add two spaces per indent.", - " Indent level 1", - " Indent level 2", - "", - "Unordered lists:", - "On a new line, at the start of the line, add two spaces, an asterisks and a space.", - "For nested lists, add two additional spaces in front of the asterisk per list level increment.", - " * Unordered List level 1", - " * Unordered List level 2", - "", - "Link:", - "Here is [a link to some cool website!](https://github.com/AllenDang/giu) you must click it!", - "Image:", - "![gopher image](./gopher.png)", - "![gopher image link](https://raw.githubusercontent.com/AllenDang/giu/master/examples/loadimage/gopher.png)", - "", - "Horizontal Rule:", - "***", - "___", - }, "\n") -} - func loop() { giu.SingleWindow().Layout( giu.SplitLayout(giu.DirectionHorizontal, &splitLayoutPos, @@ -59,7 +55,7 @@ func loop() { giu.Row( giu.Label("Markdown Edition:"), giu.Button("Reset").OnClick(func() { - markdown = getExampleMarkdownText() + markdown = defaultMd }), ), giu.Custom(func() { diff --git a/go.mod b/go.mod index f23ded4a..c60129e5 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/AllenDang/giu go 1.23.3 require ( - github.com/AllenDang/cimgui-go v1.1.1-0.20241111163245-8c039e187bcc + github.com/AllenDang/cimgui-go v1.2.0 github.com/AllenDang/go-findfont v0.0.0-20200702051237-9f180485aeb8 github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 github.com/gucio321/glm-go v0.0.0-20241029220517-e1b5a3e011c8 diff --git a/go.sum b/go.sum index 1a127f2e..e4c1d782 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/AllenDang/cimgui-go v1.1.1-0.20241111141409-85e6553b3257 h1:Mel/E72ff github.com/AllenDang/cimgui-go v1.1.1-0.20241111141409-85e6553b3257/go.mod h1:KT0QhbfG00LVdgN/eOGhnrSSG8lMfdBvYmZJCBgp2JM= github.com/AllenDang/cimgui-go v1.1.1-0.20241111163245-8c039e187bcc h1:z19BKKTjz+yJtaXGiu2xsziMuDt7k1td3v17kauJHqE= github.com/AllenDang/cimgui-go v1.1.1-0.20241111163245-8c039e187bcc/go.mod h1:KT0QhbfG00LVdgN/eOGhnrSSG8lMfdBvYmZJCBgp2JM= +github.com/AllenDang/cimgui-go v1.2.0 h1:xlsBNlGW2n4X6WYi0B84iOoAYWQT+iJFKWM2iZvpzNI= +github.com/AllenDang/cimgui-go v1.2.0/go.mod h1:KT0QhbfG00LVdgN/eOGhnrSSG8lMfdBvYmZJCBgp2JM= github.com/AllenDang/go-findfont v0.0.0-20200702051237-9f180485aeb8 h1:dKZMqib/yUDoCFigmz2agG8geZ/e3iRq304/KJXqKyw= github.com/AllenDang/go-findfont v0.0.0-20200702051237-9f180485aeb8/go.mod h1:b4uuDd0s6KRIPa84cEEchdQ9ICh7K0OryZHbSzMca9k= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=