Hello I am working on parsing responses from the web.
I would like to display the response string on screen,
I have found the text wrap function
textWrapWidth(object.size[x])
which is beautiful and saves some work…
how ever my pitfall is that it seems codea’s native text function will not support strings longer then 290 chars
I have been using str = string.sub(str, 1, 250) to try and limit to see what i can work with…
I would really enjoy not having to create some sort of functions that would break the large string up and display it in multiple text funcs… calculating position of each one… So does any one know of any solutions to display large strings… mainly JSON responses or raw HTML to be parsed…
Can you post an example showing the issue, please?. I just copied and pasted a random text of 1750 characters and I had no issue displaying it after setting the text wrap width.
I can get longer than 290 chars, about 2000 chars in one text function, it works, it cqn just get really slow
Here is an example… I have gotten it up to 4000chars but i would like to display an entire text so i could scroll through…
@Ruttyj Try this. I took out a lot of code just to keep this small. I added comments where I added or changed code. I also added comments where code needs to be modified to make it work for any length of data. I dont have time right now to fix it the right way, but I’ll let you work on it. Just slide your finger up or down the screen to scroll.
EDIT: Corrected code.
--displayMode(FULLSCREEN)
function setup()
tab={}
dy=0 -- added
x = 1
y = 2
str = nil
url = "http://data.gc.ca/data/en/api/action/package_show?id=aba94801-fd1c-4897-a24f-64d4381c504a"
http.request(url, handelResponse )
end
function handelResponse( data, status, headers )
-- code added
str=data
len=string.len(str)
s=1
add=math.min(4000,len)
while true do
table.insert(tab,string.sub(str,s,s+add))
if s+add+1>len then
break
else
s=s+add+1
end
end
end
function draw()
background(40, 40, 50)
if str ~= nil then
if displayed == nil then displayed = true --only show once
print("------------------------------")
print("type", type(str))
print("length", str.len(str))
print("------------------------------")
print(str)
end
end
textWrapWidth(WIDTH*0.9)
textMode(CORNER)
textAlign(LEFT)
-- code added
h1=0
if str ~= nil then
for z=#tab,1,-1 do
text(tab[z],5,h1+dy)
w,h=textSize(tab[z])
h1=h1+h
end
else
text("-- NO DATA --", WIDTH/2, HEIGHT/2)
end
end
function touched(t) -- code added to scroll the screen
dy=dy+t.deltaY
end
mhmmm very nice touch with the scroll… at least now its displaying the full text… just gotta work on finding out where the string the textWrap breaks the lines so we can patch up two lines so it dosen’t break mid-word
would have to find the line height and the width of the last wraped line where the break occurs so it could be on the same line and correct x offset… the remainding width would have to be cliped from the block so they match up
–referring above ------------------------------------
so if “be on the same line and correct x off” was the last line of the old string
find the width of this string - the wrapWidth = amount of next string to bridge
find exacly where to clip that string then append text("set… the remainding width would have to be cliped from the block ")
to the same line
anyways plenty of fun to be had… might even be able to load in a lua script and do the colored syntax highlighting (each colored block of text would have to fit together nicely)…
@Ruttyj I’m not sure what you’re using this for, but patching up the different groups might not be that easy. One thing might be to determine how much room you have on the end of the first group, remove that much out of the second group and append it to the end of the first group. Do that with each group. One thing I noticed with this is if you change the font, then the amount of characters that can be displayed with the text command will change. If you ever have text that isn’t displaying, try reducing the amount of characters per table entry. I lowered it to 4000 characters because it wasn’t displaying a group when I tried increasing the number of characters in data.
EDIT: The character also determines how many can be displayed. The letter “i” takes up less space than the letter “w”. It seems like “text” can only show a certain width and height.
@Ruttyj On my iPad 1, it appears that the height of the text can’t exceed 2048 pixels. In other words, if I have a string with a lot of characters in it, if I do a w,h=textSize(string) and ‘h’ is larger than 2048, it won’t display anything. The size of ‘h’ will depend on the characters being displayed and the width of the display (textWrapWidth). Using the default font, the width of each character varies, and ‘i’ takes less width than a ‘w’.
@Ruttyj Here’s a version that you don’t have to do anything with. I skipped the table and everything is in a string. The scrolling isn’t smooth, but that’s because of the data being displayed and the way I scroll. Again, move your finger up/down the screen.
displayMode(FULLSCREEN)
function setup()
textMode(CORNER)
textWrapWidth(WIDTH)
dy=1
str=""
url = "http://data.gc.ca/data/en/api/action/package_show?id=aba94801-fd1c-4897-a24f-64d4381c504a"
http.request(url, handelResponse )
end
function handelResponse( data, status, headers )
str=data
end
function draw()
background(40, 40, 50)
fill(255)
if str~="" then
str1=string.sub(str,dy,dy+5200)
w,h=textSize(str1)
text(str1,0,HEIGHT-h)
else
text("No Data",WIDTH/2,HEIGHT/2)
end
end
function touched(t)
dy=dy+t.deltaY*5
if dy<1 then
dy=1
end
end