Skip to content

Commit

Permalink
markdown: linting
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Nov 12, 2024
1 parent 7e9ac69 commit ccdcb43
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 61 deletions.
35 changes: 18 additions & 17 deletions Markdown.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//nolint:gocritic,govet,wsl,revive // this file is TODO. We don't want commentedOutCode lint issues here.
package giu

import (
Expand All @@ -22,19 +21,32 @@ 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
}

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 {
Expand All @@ -45,6 +57,7 @@ func (m *MarkdownWidget) newState() *markdownState {

result := mdLoadImage(link)
m.getState().images[link] = result

return result
})

Expand All @@ -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{
Expand All @@ -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()]
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
82 changes: 39 additions & 43 deletions examples/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,59 @@
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,
giu.Layout{
giu.Row(
giu.Label("Markdown Edition:"),
giu.Button("Reset").OnClick(func() {
markdown = getExampleMarkdownText()
markdown = defaultMd
}),
),
giu.Custom(func() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit ccdcb43

Please sign in to comment.