refactor fcore/text-builder.sml to take non-changing parameters as records, for brevity

This commit is contained in:
2024-11-15 06:43:14 +00:00
parent df9c0e3b51
commit 91688441cf
2 changed files with 86 additions and 58 deletions

View File

@@ -67,11 +67,26 @@ struct
* on an indented line *) * on an indented line *)
(* same as buildTextStringAfterCursor, except this keeps track of absolute (* same as buildTextStringAfterCursor, except this keeps track of absolute
* index and cursor pos too *) * index and cursor pos too *)
type colour_data =
{ r: Real32.real
, g: Real32.real
, b: Real32.real
, hr: Real32.real
, hg: Real32.real
, hb: Real32.real
}
type window_data =
{ w: int
, h: int
, fw: Real32.real
, fh: Real32.real
}
fun buildTextString fun buildTextString
( pos, str, acc, posX, posY, startX ( pos, str, acc, posX, posY, startX
, windowWidth, windowHeight, fWindowWidth, fWindowHeight , tl, absIdx, cursorPos, cursorAcc
, r, g, b, tl, absIdx, cursorPos, cursorAcc, hr, hg, hb , windowData: window_data, colourData: colour_data
) = ) =
if pos < String.size str then if pos < String.size str then
case String.sub (str, pos) of case String.sub (str, pos) of
@@ -82,50 +97,46 @@ struct
(* not in cursur *) (* not in cursur *)
buildTextString buildTextString
( pos + 1, str, acc, posX + xSpace, posY, startX ( pos + 1, str, acc, posX + xSpace, posY, startX
, windowWidth, windowHeight, fWindowWidth, fWindowHeight , tl, absIdx + 1, cursorPos, cursorAcc
, r, g, b, tl, absIdx + 1, cursorPos, cursorAcc, hr, hg, hb , windowData, colourData
) )
else else
(* in cursor *) (* in cursor *)
let let
val cursorAcc = buildCursor (posX, posY, fWindowWidth, fWindowHeight, r, g ,b) val {fw, fh, ...} = windowData
val {r, g, b, ...} = colourData
val cursorAcc = buildCursor (posX, posY, fw, fh, r, g ,b)
in in
buildTextString buildTextString
( pos + 1, str, acc, posX + xSpace, posY, startX ( pos + 1, str, acc, posX + xSpace, posY, startX
, windowWidth, windowHeight, fWindowWidth, fWindowHeight , tl, absIdx + 1, cursorPos, cursorAcc
, r, g, b, tl, absIdx + 1, cursorPos, cursorAcc, hr, hg, hb , windowData, colourData
) )
end end
| #"\n" => | #"\n" =>
if posY + ySpace < windowHeight then if posY + ySpace < #h windowData then
if absIdx <> cursorPos then if absIdx <> cursorPos then
(* not in cursor position, so iterate like normal *) (* not in cursor position, so iterate like normal *)
buildTextString buildTextString
( pos + 1, str, acc, startX, posY + ySpace, startX ( pos + 1, str, acc, startX, posY + ySpace, startX
, windowWidth, windowHeight, fWindowWidth, fWindowHeight , tl, absIdx + 1, cursorPos, cursorAcc
, r, g, b, tl, absIdx + 1, cursorPos, cursorAcc, hr, hg, hb , windowData, colourData
) )
else else
(* in cursor position, so build cursorAcc *) (* in cursor position, so build cursorAcc *)
if pos = String.size str - 1 andalso tl = [] then let
(* if we are at end of lineGap, we want to build cursorAcc val {fw, fh, ...} = windowData
* at different coordinates than usual *) val {r, g, b, ...} = colourData
let
val cursorAcc = val cursorAcc = buildCursor (posX, posY, fw, fh, r, g ,b)
buildCursor (startX, posY + ySpace, fWindowWidth, fWindowHeight, r, g, b) in
in buildTextString
accToDrawMsg (acc, cursorAcc) ( pos + 1, str, acc, startX, posY + ySpace, startX
end , tl, absIdx + 1, cursorPos, cursorAcc
else , windowData, colourData
let )
val cursorAcc = buildCursor (posX, posY, fWindowWidth, fWindowHeight, r, g ,b) end
in
buildTextString
( pos + 1, str, acc, startX, posY + ySpace, startX
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
, r, g, b, tl, absIdx + 1, cursorPos, cursorAcc, hr, hg, hb
)
end
else else
accToDrawMsg (acc, cursorAcc) accToDrawMsg (acc, cursorAcc)
| chr => | chr =>
@@ -134,31 +145,36 @@ struct
in in
if absIdx <> cursorPos then if absIdx <> cursorPos then
(* not equal to cursor *) (* not equal to cursor *)
if posX + xSpace < windowWidth then if posX + xSpace < #w windowData then
let let
val {fw, fh, ...} = windowData
val {r, g, b, ...} = colourData
val chrVec = chrFun val chrVec = chrFun
(posX, posY, fontSize, fontSize, fWindowWidth, fWindowHeight, r, g, b) (posX, posY, fontSize, fontSize, fw, fh, r, g, b)
val acc = chrVec :: acc val acc = chrVec :: acc
in in
buildTextString buildTextString
( pos + 1, str, acc, posX + xSpace, posY, startX ( pos + 1, str, acc, posX + xSpace, posY, startX
, windowWidth, windowHeight, fWindowWidth, fWindowHeight , tl, absIdx + 1, cursorPos, cursorAcc
, r, g, b, tl, absIdx + 1, cursorPos, cursorAcc, hr, hg, hb , windowData, colourData
) )
end end
else if posY + ySpace < windowHeight then else if posY + ySpace < #h windowData then
let let
val {fw, fh, ...} = windowData
val {r, g, b, ...} = colourData
val chrVec = chrFun val chrVec = chrFun
( startX, posY + ySpace, fontSize, fontSize ( startX, posY + ySpace, fontSize, fontSize
, fWindowWidth, fWindowHeight , fw, fh, r, g, b
, r, g, b
) )
val acc = chrVec :: acc val acc = chrVec :: acc
in in
buildTextString buildTextString
( pos + 1, str, acc, startX + xSpace, posY + ySpace, startX ( pos + 1, str, acc, startX + xSpace, posY + ySpace, startX
, windowWidth, windowHeight, fWindowWidth, fWindowHeight , tl, absIdx + 1, cursorPos, cursorAcc
, r, g, b, tl, absIdx + 1, cursorPos, cursorAcc, hr, hg, hb , windowData, colourData
) )
end end
else else
@@ -166,14 +182,15 @@ struct
else else
(* equal to cursor *) (* equal to cursor *)
let let
val cursorAcc = buildCursor (posX, posY, fWindowWidth, fWindowHeight, r, g ,b) val {fw, fh, ...} = windowData
val {r, g, b, hr, hg, hb} = colourData
val cursorAcc = buildCursor (posX, posY, fw, fh, r, g ,b)
in in
if posX + xSpace < windowWidth then if posX + xSpace < #w windowData then
let let
val chrVec = chrFun val chrVec = chrFun
( posX, posY, fontSize, fontSize ( posX, posY, fontSize, fontSize
, fWindowWidth, fWindowHeight , fw, fh , hr, hg, hb
, hr, hg, hb
) )
val acc = chrVec :: acc val acc = chrVec :: acc
in in
@@ -181,16 +198,15 @@ struct
* since cursor was built *) * since cursor was built *)
buildTextString buildTextString
( pos + 1, str, acc, posX + xSpace, posY, startX ( pos + 1, str, acc, posX + xSpace, posY, startX
, windowWidth, windowHeight, fWindowWidth, fWindowHeight , tl, absIdx + 1, cursorPos, cursorAcc
, r, g, b, tl, absIdx + 1, cursorPos, cursorAcc, hr, hg, hb , windowData, colourData
) )
end end
else if posY + ySpace < windowHeight then else if posY + ySpace < #h windowData then
let let
val chrVec = chrFun val chrVec = chrFun
( startX, posY + ySpace, fontSize, fontSize ( startX, posY + ySpace, fontSize, fontSize
, fWindowWidth, fWindowHeight , fw, fh, hr, hg, hb
, hr, hg, hb
) )
val acc = chrVec :: acc val acc = chrVec :: acc
in in
@@ -198,8 +214,8 @@ struct
* since cursor was built *) * since cursor was built *)
buildTextString buildTextString
( pos + 1, str, acc, startX + xSpace, posY + ySpace, startX ( pos + 1, str, acc, startX + xSpace, posY + ySpace, startX
, windowWidth, windowHeight, fWindowWidth, fWindowHeight , tl, absIdx + 1, cursorPos, cursorAcc
, r, g, b, tl, absIdx + 1, cursorPos, cursorAcc, hr, hg, hb , windowData, colourData
) )
end end
else else
@@ -213,8 +229,8 @@ struct
hd :: tl => hd :: tl =>
buildTextString buildTextString
( 0, hd, acc, posX, posY, startX ( 0, hd, acc, posX, posY, startX
, windowWidth, windowHeight, fWindowWidth, fWindowHeight , tl, absIdx, cursorPos, cursorAcc
, r, g, b, tl, absIdx, cursorPos, cursorAcc, hr, hg, hb , windowData, colourData
) )
| [] => | [] =>
accToDrawMsg (acc, cursorAcc) accToDrawMsg (acc, cursorAcc)
@@ -245,16 +261,28 @@ struct
end end
else else
0 0
val absIdx = curIdx + startIdx val absIdx = curIdx + startIdx
val windowData =
{ w = windowWidth
, h = windowHeight
, fw = Real32.fromInt windowWidth
, fh = Real32.fromInt windowHeight
}
val colourData =
{ r = 0.67
, g = 0.51
, b = 0.83
, hr = 0.211
, hg = 0.219
, hb = 0.25
}
in in
buildTextString buildTextString
( startIdx, rStrHd, [] ( startIdx, rStrHd, [], 5, 5, 5
, 5, 5, 5
, windowWidth, windowHeight
, Real32.fromInt windowWidth, Real32.fromInt windowHeight
, 0.67, 0.51, 0.83
, rStrTl, absIdx, cursorPos, [] , rStrTl, absIdx, cursorPos, []
, 0.211, 0.219, 0.25 , windowData, colourData
) )
end end
| (_, _) => | (_, _) =>

BIN
shf

Binary file not shown.