a bit of refactoring

This commit is contained in:
2024-10-08 08:53:43 +01:00
parent 68a1787958
commit 6e4cc9acff
6 changed files with 53 additions and 22 deletions

View File

@@ -1,8 +1,12 @@
structure AppType = structure AppType =
struct struct
type app_type = type app_type =
{buffer: LineGap.t, windowWidth: int, windowHeight: int} {buffer: LineGap.t, windowWidth: int, windowHeight: int, startLine: int}
fun init (buffer, windowWidth, windowHeight): app_type = fun init (buffer, windowWidth, windowHeight) : app_type =
{buffer = buffer, windowWidth = windowWidth, windowHeight = windowHeight} { buffer = buffer
, windowWidth = windowWidth
, windowHeight = windowHeight
, startLine = 0
}
end end

View File

@@ -8,12 +8,13 @@ struct
fun resizeText (app: app_type, newWidth, newHeight) = fun resizeText (app: app_type, newWidth, newHeight) =
let let
val {buffer, windowWidth, windowHeight} = app val {buffer, windowWidth, windowHeight, startLine} = app
val (textVec, newBuffer) =
TextBuilder.build (0, buffer, newWidth, newHeight)
val newApp = val newBuffer = LineGap.goToLine (startLine, buffer)
{buffer = newBuffer, windowWidth = newWidth, windowHeight = newHeight} val textVec =
TextBuilder.build (startLine, newBuffer, newWidth, newHeight)
val newApp = AppWith.bufferAndSize (app, newBuffer, newWidth, newHeight)
val msg = REDRAW_TEXT textVec val msg = REDRAW_TEXT textVec
in in
(newApp, [DRAW msg]) (newApp, [DRAW msg])

15
fcore/app-with.sml Normal file
View File

@@ -0,0 +1,15 @@
structure AppWith =
struct
open AppType
fun bufferAndSize (app: app_type, newBuffer, newWidth, newHeight) =
let
val {buffer = _, windowWidth = _, windowHeight = _, startLine} = app
in
{ buffer = newBuffer
, windowWidth = newWidth
, windowHeight = newHeight
, startLine = startLine
}
end
end

View File

@@ -1,14 +1,16 @@
signature TEXT_BUILDER = signature TEXT_BUILDER =
sig sig
(* Prerequisite: LineGap is moved to requested line first. *)
val build: int * LineGap.t * int * int val build: int * LineGap.t * int * int
-> Real32.real vector * LineGap.t -> Real32.real vector
end end
structure TextBuilder :> TEXT_BUILDER = structure TextBuilder :> TEXT_BUILDER =
struct struct
val xSpace = 12 val xSpace = 13
val xSpace3 = xSpace * 3 val xSpace3 = xSpace * 3
val ySpace = 23 val ySpace = 25
val fontSize = 30.0
(* builds text from a string with char-wrap. (* builds text from a string with char-wrap.
* char-wrap is a similar concept to word-wrap, * char-wrap is a similar concept to word-wrap,
@@ -84,7 +86,7 @@ struct
(* if there is horizontal space, place char on the right *) (* if there is horizontal space, place char on the right *)
let let
val chrVec = chrFun val chrVec = chrFun
(posX, posY, 25.0, 25.0, fWindowWidth, fWindowHeight, r, g, b) (posX, posY, fontSize, fontSize, fWindowWidth, fWindowHeight, r, g, b)
val acc = chrVec :: acc val acc = chrVec :: acc
in in
buildTextString buildTextString
@@ -97,7 +99,7 @@ struct
(* if there is vertical space, place char down below at startX *) (* if there is vertical space, place char down below at startX *)
let let
val chrVec = chrFun val chrVec = chrFun
( startX, posY + 25, 25.0, 25.0 ( startX, posY + ySpace, fontSize, fontSize
, fWindowWidth, fWindowHeight , fWindowWidth, fWindowHeight
, r, g, b , r, g, b
) )
@@ -147,15 +149,23 @@ struct
(rStrHd :: rStrTl, rLnHd :: _) => (rStrHd :: rStrTl, rLnHd :: _) =>
let let
(* get index of line to start building from *) (* get index of line to start building from *)
val lnPos = startLine - curLine
val startIdx = Vector.sub (rLnHd, lnPos)
val startIdx = val startIdx =
if if startLine > curLine then
String.sub (rStrHd, startIdx) = #"\r" let
andalso startIdx < String.size rStrHd - 1 val lnPos = startLine - curLine - 1
andalso String.sub (rStrHd, startIdx + 1) = #"\n" val startIdx = Vector.sub (rLnHd, lnPos)
then (* handle \r\n pair *) startIdx + 2 in
else startIdx + 1 if
String.sub (rStrHd, startIdx) = #"\r"
andalso startIdx < String.size rStrHd - 1
andalso String.sub (rStrHd, startIdx + 1) = #"\n"
then
(* handle \r\n pair *)
startIdx + 2
else startIdx + 1
end
else
0
in in
buildTextString buildTextString
( startIdx, rStrHd, [] ( startIdx, rStrHd, []
@@ -172,6 +182,6 @@ struct
* else we can do. *) * else we can do. *)
[] []
in in
(Vector.concat acc, lineGap) Vector.concat acc
end end
end end

BIN
shf

Binary file not shown.

View File

@@ -10,6 +10,7 @@ message-types/draw-msg.sml
message-types/mailbox-type.sml message-types/mailbox-type.sml
fcore/app-type.sml fcore/app-type.sml
fcore/app-with.sml
fcore/text-builder.sml fcore/text-builder.sml
fcore/app-update.sml fcore/app-update.sml