hedron-crystal / hedron

An extendable UI library in Crystal, with markup capabilities.
MIT License
85 stars 5 forks source link

#text methods are broken #8

Closed adoxography closed 6 years ago

adoxography commented 6 years ago

Hedron::Entry#text and Hedron::MultilineEntry#text both fail when called, with the message

type must be String, not Pointer(UInt8)

It looks like the issue is that both methods try to implicitly cast the Pointer(Uint8) (a.k.a LibC::Char*) into String via their return type, but that's not allowed. As mentioned in this Stack Overflow answer, the pointer should used to create a new String, so the following changes fix it for me:

diff --git a/src/hedron/ui/entry.cr b/src/hedron/ui/entry.cr
index b084b2e..ccdc950 100644
--- a/src/hedron/ui/entry.cr
+++ b/src/hedron/ui/entry.cr
@@ -42,7 +42,7 @@ module Hedron
     end

     def text : String
-      return UI.entry_text(to_unsafe)
+      return String.new(UI.entry_text(to_unsafe))
     end

     def text=(entry_text : String)
diff --git a/src/hedron/ui/multiline_entry.cr b/src/hedron/ui/multiline_entry.cr
index 6d977ae..1c688bf 100644
--- a/src/hedron/ui/multiline_entry.cr
+++ b/src/hedron/ui/multiline_entry.cr
@@ -46,7 +46,7 @@ module Hedron
     end

     def text : String
-      return UI.multiline_entry_text(to_unsafe)
+      return String.new(UI.multiline_entry_text(to_unsafe))
     end

     def text=(entry_text : String)

Happy to open a PR if desired. :)

hanyuone commented 6 years ago

Sorry for the late reply - sure, go ahead and open a PR! I'm currently in the process of rewriting most of the code of Hedron on my Windows device (because of lacklustre documentation and custom widgets), so it's fairly slow at the moment.