require 'site'
local Nylon = require 'nylon.core'()
local Store = require 'pls-buffer'
local function cursorpos( buffer, ptTopLeft, thePoint, wdim )
local charsInto = thePoint - ptTopLeft
local ptRow, ptCol
local drow = 0
local dcol = 0
local charsDrawn = 0
for l, col, eol in buffer:walkFragmentsEOL( ptTopLeft ) do
local toDraw = eol and (eol - col) or #l
if ((charsDrawn + toDraw) >= charsInto) then
return (dcol + charsInto-charsDrawn), drow
else
charsDrawn = charsDrawn + toDraw
end
if eol then
charsDrawn = charsDrawn + 1
dcol = 0
drow = drow + 1
else
dcol = dcol + toDraw
end
end
end
local function mysert_eq( v, shouldbe )
if v ~= shouldbe then
print(string.format('expected value=%d got=%d',shouldbe,v))
assert(false)
end
end
local b = Store.withText('12345678a\n')
b:append('12345678b\n')
b:append('12345678c\n')
mysert_eq(b:end_point(),30)
b:insert(11,'12345678x\n')
mysert_eq(b:end_point(),40)
assert(b:char_at_point(3)=='3')
assert(b:char_at_point(14)=='4')
assert(b:char_at_point(25)=='5')
assert(b:char_at_point(36)=='6')
local function test_buffer( t, buflen)
local b = Store.withText()
b.setIdealStringLength(buflen)
b:insert(1,t)
local re = {}
local rcvd = {}
for l, col, eol in b:walkFragmentsEOL(1) do
table.insert(rcvd, {col,l,eol} )
table.insert(re, l:sub(col,eol))
end
local JSON = require 'JSON'
if true or ( table.concat(re) ~= t ) then
for i, v in ipairs(re) do
print(i,JSON:encode(v))
print(i,JSON:encode(rcvd[i]) )
end
end
assert(table.concat(re)==t)
return b
end
if true then
local t = 'hello\nthere\nbuddy\nroo\n'
test_buffer( t, 99 )
test_buffer( t, 10 )
local t = '0123456789'
test_buffer( t, 5 )
test_buffer( t, 3 )
test_buffer( t, 2 )
test_buffer( t, 1 )
end
local t = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'
test_buffer( t, 100 )
test_buffer( t, 10 )
test_buffer( t, 2 )
test_buffer( t, 1 )
local t = '0123456789'
test_buffer( t, 2 )