done with functionality relating to performing file operations on terminal

This commit is contained in:
2025-08-26 16:15:40 +01:00
parent dcd3c3117e
commit 2de690f325
8 changed files with 84 additions and 70 deletions

View File

@@ -38,6 +38,7 @@ message-types/file-msg.sml
message-types/input-msg.sml
message-types/update-msg.sml
fcore/file-string.sml
fcore/quad-tree.sml
fcore/common-update.sml

BIN
dsc

Binary file not shown.

View File

@@ -48,34 +48,37 @@ struct
fun getSaveSquaresMsg (model: app_type) =
let
val {layerTree, canvasWidth, canvasHeight, ...} = model
val str =
val {layerTree, canvasWidth, canvasHeight, openFilePath, ...} = model
val saveString =
CollisionTree.toSaveString (layerTree, canvasWidth, canvasHeight)
val msg = SAVE_SQUARES str
val msg = SAVE_SQUARES {output = saveString, filepath = openFilePath}
in
(model, [FILE msg])
end
fun getLoadSquaresMsg model =
(model, [FILE LOAD_SQUARES])
fun getLoadSquaresMsg (model: app_type) =
let val msg = LOAD_SQUARES {filepath = #openFilePath model}
in (model, [FILE msg])
end
fun getExportSquaresMsg (model: app_type) =
let
val {layerTree, canvasWidth, canvasHeight, ...} = model
val {layerTree, canvasWidth, canvasHeight, openFilePath, ...} = model
val maxSide = Int.max (canvasWidth, canvasHeight)
val squares = LayerTree.flatten (maxSide, layerTree)
val exportString =
CollisionTree.toExportString (squares, canvasWidth, canvasHeight)
val msg = EXPORT_SQUARES exportString
val msg = EXPORT_SQUARES {output = exportString, filepath = openFilePath}
in
(model, [FILE msg])
end
fun getCollisionMsg (model: app_type) =
let
val {layerTree, canvasWidth, canvasHeight, modalNum, ...} = model
val {layerTree, canvasWidth, canvasHeight, modalNum, openFilePath, ...} =
model
val maxSide = Int.max (canvasWidth, canvasHeight)
val squares = LayerTree.flatten (maxSide, layerTree)
@@ -83,7 +86,10 @@ struct
val exportString =
CollisionTree.toCollisionString
(squares, canvasWidth, canvasHeight, modalNum)
val msg = EXPORT_COLLISIONS exportString
val exportFilePath = FileString.getCollisionFilename openFilePath
val msg =
EXPORT_COLLISIONS {output = exportString, filepath = exportFilePath}
val model = AppWith.modalNum (model, 0)
in

59
fcore/file-string.sml Normal file
View File

@@ -0,0 +1,59 @@
structure FileString =
struct
fun findLastChr (str, pos, findChr) =
if pos < 0 then ~1
else if String.sub (str, pos) = findChr then pos
else findLastChr (str, pos - 1, findChr)
fun extractFileName str =
let
val lastSlash = findLastChr (str, String.size str - 1, #"/")
val strStart = lastSlash + 1
in
if lastSlash = ~1 then str
else String.substring (str, strStart, String.size str - strStart)
end
fun removeFileExtension str =
let val lastDot = findLastChr (str, String.size str - 1, #".")
in if lastDot = ~1 then str else String.substring (str, 0, lastDot)
end
local
fun finish acc =
let val acc = List.rev acc
in String.implode acc
end
(* convert from kebab-case or snake_case to PascalCase *)
fun loop (#"-" :: chr :: tl, acc) =
let val acc = Char.toUpper chr :: acc
in loop (tl, acc)
end
| loop (#"_" :: chr :: tl, acc) =
let val acc = Char.toUpper chr :: acc
in loop (tl, acc)
end
| loop ([#"-"], acc) = finish acc
| loop ([#"_"], acc) = finish acc
| loop (chr :: tl, acc) =
loop (tl, chr :: acc)
| loop ([], acc) = finish acc
in
fun filenameToStructureName str =
let
val str = removeFileExtension str
val str = extractFileName str
in
(* capitalise first character in string *)
case String.explode str of
chr :: tl => let val chr = Char.toUpper chr in loop (tl, [chr]) end
| [] => ""
end
end
fun getCollisionFilename str =
let val str = removeFileExtension str
in str ^ "-collisions.sml"
end
end

View File

@@ -1,57 +1,5 @@
structure CollisionTree =
struct
fun findLastChr (str, pos, findChr) =
if pos < 0 then ~1
else if String.sub (str, pos) = findChr then pos
else findLastChr (str, pos - 1, findChr)
fun extractFileName str =
let
val lastSlash = findLastChr (str, String.size str - 1, #"/")
val strStart = lastSlash + 1
in
if lastSlash = ~1 then str
else String.substring (str, strStart, String.size str - strStart)
end
local
fun removeFileExtension str =
let val lastDot = findLastChr (str, String.size str - 1, #".")
in if lastDot = ~1 then str else String.substring (str, 0, lastDot)
end
fun finish acc =
let val acc = List.rev acc
in String.implode acc
end
(* convert from kebab-case or snake_case to PascalCase *)
fun loop (#"-" :: chr :: tl, acc) =
let val acc = Char.toUpper chr :: acc
in loop (tl, acc)
end
| loop (#"_" :: chr :: tl, acc) =
let val acc = Char.toUpper chr :: acc
in loop (tl, acc)
end
| loop ([#"-"], acc) = finish acc
| loop ([#"_"], acc) = finish acc
| loop (chr :: tl, acc) =
loop (tl, chr :: acc)
| loop ([], acc) = finish acc
in
fun filenameToStructureName str =
let
val str = removeFileExtension str
val str = extractFileName str
in
(* capitalise first character in string *)
case String.explode str of
chr :: tl => let val chr = Char.toUpper chr in loop (tl, [chr]) end
| [] => ""
end
end
structure BinTree =
struct
datatype 'a bintree =

File diff suppressed because one or more lines are too long

View File

@@ -49,10 +49,10 @@ struct
let
val _ =
case Mailbox.recv fileMailbox of
SAVE_SQUARES str => saveString (filename, str)
| EXPORT_SQUARES str => saveString (exportFilename, str)
| EXPORT_COLLISIONS str => saveString (collisionFilename, str)
| LOAD_SQUARES => loadSquares (filename, inputMailbox)
SAVE_SQUARES {filepath, output} => saveString (filepath, output)
| EXPORT_SQUARES {filepath, output} => saveString (filepath, output)
| EXPORT_COLLISIONS {filepath, output} => saveString (filepath, output)
| LOAD_SQUARES filename => loadSquares (filename, inputMailbox)
in
run (fileMailbox, inputMailbox)
end

View File

@@ -1,8 +1,8 @@
structure FileMessage =
struct
datatype t =
SAVE_SQUARES of string
| LOAD_SQUARES
| EXPORT_SQUARES of string
| EXPORT_COLLISIONS of string
SAVE_SQUARES of {output: string, filepath: string}
| LOAD_SQUARES of {filepath: string}
| EXPORT_SQUARES of {output: string, filepath: string}
| EXPORT_COLLISIONS of {output: string, filepath: string}
end