Textarea line separators on Mac
-
My PC <textarea> lines are separated by crlf: 13, 10. Does anyone know what that is on a Mac?
In the following script, Ruby launches a WebDialog with a text area with two lines ("one" and "two"). The WebDialog calls Ruby after it is loaded. The Ruby callback grabs the text area's value, and writes it to the console, one number per character. The separator(s) will appear between 101 ("e") and 116 ("t").
Thank you!
# ta_sep.rb require 'sketchup' html=" <html> <body> <textarea id='ta'>one two</textarea> <script> onload = function() { location = 'skp;go'; } </script> </body> </html>" wd = UI;;WebDialog.new( "Test", true, "test", 400, 300, 100, 100, true ) wd.set_html( html ) wd.add_action_callback( "go" ) do | dlg, msg | val = wd.get_element_value( 'ta' ) for i in 1..val.length do puts val[i-1] end end wd.show()
-
Anyone?
-
-
Any now my question.
Why are you coding at that level? Why not just bust the input data up into ruby "newlines"?
-
@unknownuser said:
You're welcome!
Thank you!
@unknownuser said:
Why are you coding at that level?
Stupidity or ignorance. Maybe both.
@unknownuser said:
Why not just bust the input data up into ruby "newlines"?
Hmmm. This is the code that gets a WebDialog <textarea>'s data into a disk file:
# write the file str = wd_console.get_element_value( 'console' ) str.gsub!( /\r\n/, "\n" ) # why? doesn't look PC, but it works file = File.new( pathname, 'w' ) file.puts( str ) file.close()
I'm thinking that will work fine on a Mac, because the
gsub!()
will find zero occurrences. Now the other direction: you read the file as a string and pass the string to the textarea and it will be Win/Mac compatible. -
Unless the files you create on a Mac via this dialog will be moved over to Windows for the purpose of being read with Notepad (and this is only because Notepad is stupid), I personally see no productivity or functionality gains from bothering with each platforms newline patterns, unless you just want to.
But heh, that's just me.
-
@unknownuser said:
I personally see no productivity or functionality gains from bothering with each platforms newline patterns, unless you just want to.
This isn't the file I want given the input textarea:
You have to convert crlf to newline because Ruby converts newline to crlf as it writes the file on a PC. Ugh.
# ta_sep.rb require 'sketchup' html=" <html> <body> <textarea id='ta'>one two</textarea> <script> onload = function() { location = 'skp;go'; } </script> </body> </html>" wd = UI;;WebDialog.new( "Test", true, "test", 400, 300, 100, 100, true ) wd.set_html( html ) wd.add_action_callback( "go" ) do | dlg, msg | val = wd.get_element_value( 'ta' ) # val.gsub!( /\r\n/, "\n" ) pn = UI.savepanel( "Save As ...", File.dirname(__FILE__), 't.txt' ) file = File.new( pn, 'w' ) file.puts( val ) file.close() end wd.show()
I think this is correct cross-platform code if you uncomment the
gsub!()
. For this I be thanking you and Notepad++ (View/Show Symbol/Show All Characters).Ruby's lovely ability to handle everything with newlines, platform-independently, does not extend to text read from a browser's textarea widgets.
Advertisement