From f5e4338350b1626cd6629d089712893a83ff48f3 Mon Sep 17 00:00:00 2001 From: Ian McLerran Date: Tue, 4 Jun 2024 19:52:54 -0500 Subject: [PATCH] Roc format all of src dir --- src/ArgParser.roc | 8 +-- src/Controller.roc | 30 +++++++--- src/Model.roc | 64 ++++++++++++++-------- src/Repo.roc | 4 +- src/Tests.roc | 134 +++++++++++++++++++++++++++------------------ src/View.roc | 13 +++-- src/test-stub.roc | 2 +- 7 files changed, 158 insertions(+), 97 deletions(-) diff --git a/src/ArgParser.roc b/src/ArgParser.roc index b4eaead..3c992f8 100644 --- a/src/ArgParser.roc +++ b/src/ArgParser.roc @@ -17,7 +17,7 @@ cliParser = appName: <- Param.maybeStr { name: "app-name", help: "Name your new roc app." }, platform: <- Param.maybeStr { name: "platform", help: "The platform to use." }, packages: <- Param.strList { name: "packages", help: "Any packages to use." }, - + } |> Cli.finish { name: "roc-start", @@ -27,10 +27,10 @@ cliParser = } |> Cli.assertValid -tuiSubcommand = +tuiSubcommand = Cli.weave {} - |> Subcommand.finish { + |> Subcommand.finish { name: "tui", description: "Use the TUI app to browse and search for platforms and packages.", mapper: Tui, - } \ No newline at end of file + } diff --git a/src/Controller.roc b/src/Controller.roc index 55ac5a6..5e43a38 100644 --- a/src/Controller.roc +++ b/src/Controller.roc @@ -29,24 +29,34 @@ getActions = \model -> when model.state is PlatformSelect _ -> [Exit, SingleSelect, CursorUp, CursorDown] - |> \actions -> if List.len model.fullMenu < List.len model.platformList - then List.append actions ClearFilter else List.append actions Search + |> \actions -> if + List.len model.fullMenu < List.len model.platformList + then + List.append actions ClearFilter + else + List.append actions Search |> List.append GoBack |> \actions -> if Model.isNotFirstPage model then List.append actions PrevPage else actions |> \actions -> if Model.isNotLastPage model then List.append actions NextPage else actions + PackageSelect _ -> [Exit, MultiSelect, MultiConfirm, CursorUp, CursorDown] - |> \actions -> if List.len model.fullMenu < List.len model.packageList - then List.append actions ClearFilter else List.append actions Search + |> \actions -> if + List.len model.fullMenu < List.len model.packageList + then + List.append actions ClearFilter + else + List.append actions Search |> List.append GoBack |> \actions -> if Model.isNotFirstPage model then List.append actions PrevPage else actions |> \actions -> if Model.isNotLastPage model then List.append actions NextPage else actions + Confirmation _ -> [Exit, Finish, GoBack] InputAppName _ -> [Exit, TextConfirm, TextInput, TextBackspace] Search _ -> [Exit, SearchGo, Cancel, TextInput, TextBackspace] _ -> [Exit] -applyAction : { model: Model, action: UserAction, keyPress ? [KeyPress Key, None] } -> [Step Model, Done Model] +applyAction : { model : Model, action : UserAction, keyPress ? [KeyPress Key, None] } -> [Step Model, Done Model] applyAction = \{ model, action, keyPress ? None } -> if actionIsAvailable model action then when model.state is @@ -91,7 +101,7 @@ packageSelect = \model, action -> PrevPage -> Step (Model.prevPage model) _ -> Step model -searchActionHandler : Model, UserAction, { sender: [Platform, Package], keyPress ? [KeyPress Key, None] } -> [Step Model, Done Model] +searchActionHandler : Model, UserAction, { sender : [Platform, Package], keyPress ? [KeyPress Key, None] } -> [Step Model, Done Model] searchActionHandler = \model, action, { sender, keyPress ? None } -> when action is Exit -> Done (Model.toUserExitedState model) @@ -99,18 +109,21 @@ searchActionHandler = \model, action, { sender, keyPress ? None } -> when sender is Platform -> Step (Model.toPlatformSelectState model) Package -> Step (Model.toPackageSelectState model) + TextBackspace -> Step (Model.backspaceBuffer model) TextInput -> when keyPress is KeyPress key -> Step (Model.appendToBuffer model key) None -> Step model - Cancel -> + + Cancel -> when sender is Platform -> Step (model |> Model.clearSearchBuffer |> Model.toPlatformSelectState) Package -> Step (model |> Model.clearSearchBuffer |> Model.toPackageSelectState) + _ -> Step model -inputAppName : Model, UserAction, { keyPress ? [KeyPress Key, None]} -> [Step Model, Done Model] +inputAppName : Model, UserAction, { keyPress ? [KeyPress Key, None] } -> [Step Model, Done Model] inputAppName = \model, action, { keyPress ? None } -> when action is Exit -> Done (Model.toUserExitedState model) @@ -119,6 +132,7 @@ inputAppName = \model, action, { keyPress ? None } -> when keyPress is KeyPress key -> Step (Model.appendToBuffer model key) None -> Step model + TextBackspace -> Step (Model.backspaceBuffer model) _ -> Step model diff --git a/src/Model.roc b/src/Model.roc index 2fa1fec..ba653ec 100644 --- a/src/Model.roc +++ b/src/Model.roc @@ -32,7 +32,7 @@ Model : { screen : Core.ScreenSize, cursor : Core.Position, menuRow : I32, - pageFirstItem: U64, + pageFirstItem : U64, menu : List Str, fullMenu : List Str, selected : List Str, @@ -70,10 +70,10 @@ init = \platformList, packageList -> { packageList, selected: [], inputs: List.withCapacity 1000, - state: InputAppName { nameBuffer: [], config: emptyConfig } + state: InputAppName { nameBuffer: [], config: emptyConfig }, } -paginate: Model -> Model +paginate : Model -> Model paginate = \model -> maxItems = model.screen.height - (model.menuRow + 1) |> Num.toU64 pageFirstItem = @@ -91,7 +91,7 @@ paginate = \model -> cursor = { row: curRow, col: model.cursor.col } { model & menu, pageFirstItem, cursor } -nextPage: Model -> Model +nextPage : Model -> Model nextPage = \model -> maxItems = model.screen.height - (model.menuRow + 1) |> Num.toU64 if isNotLastPage model then @@ -102,7 +102,7 @@ nextPage = \model -> else model -prevPage: Model -> Model +prevPage : Model -> Model prevPage = \model -> maxItems = model.screen.height - (model.menuRow + 1) |> Num.toU64 if isNotFirstPage model then @@ -121,7 +121,6 @@ isNotLastPage = \model -> maxItems = model.screen.height - (model.menuRow + 1) |> Num.toU64 model.pageFirstItem + maxItems < List.len model.fullMenu - moveCursor : Model, [Up, Down] -> Model moveCursor = \model, direction -> if List.len model.menu > 0 then @@ -151,6 +150,7 @@ toInputAppNameState = \model -> cursor: { row: 2, col: 2 }, state: InputAppName { config, nameBuffer: config.appName |> Str.toUtf8 }, } + _ -> model toPlatformSelectState : Model -> Model @@ -162,29 +162,37 @@ toPlatformSelectState = \model -> { model & cursor: { row: 2, col: 2 }, state: PlatformSelect { config: newConfig }, - } |> paginate + } + |> paginate + Search { config, searchBuffer } -> { model & fullMenu: model.platformList |> List.keepIf \item -> Str.contains item (searchBuffer |> Str.fromUtf8 |> Result.withDefault ""), cursor: { row: 2, col: 2 }, state: PlatformSelect { config }, - } |> paginate + } + |> paginate + PackageSelect { config } -> - configWithPackages = when (addSelectedPackagesToConfig model).state is - PackageSelect data -> data.config - _ -> config + configWithPackages = + when (addSelectedPackagesToConfig model).state is + PackageSelect data -> data.config + _ -> config { model & pageFirstItem: 0, fullMenu: model.platformList, cursor: { row: 2, col: 2 }, - state: PlatformSelect { config: configWithPackages } - } |> paginate + state: PlatformSelect { config: configWithPackages }, + } + |> paginate + _ -> { model & menu: model.platformList, cursor: { row: 2, col: 2 }, state: PlatformSelect { config: { platform: "", appName: "", packages: [] } }, - } |> paginate + } + |> paginate toPackageSelectState : Model -> Model toPackageSelectState = \model -> @@ -197,7 +205,8 @@ toPackageSelectState = \model -> cursor: { row: 2, col: 2 }, selected: config.packages, state: PackageSelect { config: { config & platform } }, - } |> paginate + } + |> paginate Search { config, searchBuffer } -> { model & @@ -206,7 +215,8 @@ toPackageSelectState = \model -> cursor: { row: 2, col: 2 }, selected: config.packages, state: PackageSelect { config }, - } |> paginate + } + |> paginate Confirmation { config } -> { model & @@ -215,7 +225,8 @@ toPackageSelectState = \model -> selected: config.packages, cursor: { row: 2, col: 2 }, state: PackageSelect { config }, - } |> paginate + } + |> paginate _ -> { model & @@ -223,7 +234,8 @@ toPackageSelectState = \model -> fullMenu: model.packageList, cursor: { row: 2, col: 2 }, state: PackageSelect { config: { platform: "", appName: "", packages: [] } }, - } |> paginate + } + |> paginate toFinishedState : Model -> Model toFinishedState = \model -> @@ -266,13 +278,17 @@ clearSearchFilter = \model -> PackageSelect _ -> { model & fullMenu: model.packageList, - #cursor: { row: model.menuRow, col: 2 }, - } |> paginate + # cursor: { row: model.menuRow, col: 2 }, + } + |> paginate + PlatformSelect _ -> { model & fullMenu: model.platformList, - #cursor: { row: model.menuRow, col: 2 }, - } |> paginate + # cursor: { row: model.menuRow, col: 2 }, + } + |> paginate + _ -> model appendToBuffer : Model, Key -> Model @@ -281,6 +297,7 @@ appendToBuffer = \model, key -> Search { searchBuffer, config, sender } -> newBuffer = List.concat searchBuffer (Keys.keyToSlugStr key |> Str.toUtf8) { model & state: Search { config, sender, searchBuffer: newBuffer } } + InputAppName { nameBuffer, config } -> newBuffer = List.concat nameBuffer (Keys.keyToSlugStr key |> Str.toUtf8) { model & state: InputAppName { config, nameBuffer: newBuffer } } @@ -293,6 +310,7 @@ backspaceBuffer = \model -> Search { searchBuffer, config, sender } -> newBuffer = List.dropLast searchBuffer 1 { model & state: Search { config, sender, searchBuffer: newBuffer } } + InputAppName { nameBuffer, config } -> newBuffer = List.dropLast nameBuffer 1 { model & state: InputAppName { config, nameBuffer: newBuffer } } @@ -313,7 +331,7 @@ toggleSelected = \model -> if List.contains model.selected item then { model & selected: List.dropIf model.selected \i -> i == item } else - { model & selected: List.append model.selected item } + { model & selected: List.append model.selected item } addSelectedPackagesToConfig : Model -> Model addSelectedPackagesToConfig = \model -> diff --git a/src/Repo.roc b/src/Repo.roc index e25a2fa..eb65ab2 100644 --- a/src/Repo.roc +++ b/src/Repo.roc @@ -1,5 +1,5 @@ module [RepositoryEntry, RemoteRepoEntry, CacheRepoEntry] RepositoryEntry : { alias : Str, version : Str, url : Str } -RemoteRepoEntry : { repo: Str, owner: Str, alias: Str, platform: Bool } -CacheRepoEntry : { repo: Str, owner: Str, alias: Str, version: Str, url: Str, platform: Bool } +RemoteRepoEntry : { repo : Str, owner : Str, alias : Str, platform : Bool } +CacheRepoEntry : { repo : Str, owner : Str, alias : Str, version : Str, url : Str, platform : Bool } diff --git a/src/Tests.roc b/src/Tests.roc index 63ecaf7..3ae5a3a 100644 --- a/src/Tests.roc +++ b/src/Tests.roc @@ -6,64 +6,85 @@ stubForMain = {} emptyConfig = { appName: "", platform: "", packages: [] } -#==================== +# ==================== # TEST MODEL -#==================== +# ==================== -expect # TEST: Model.init +expect + # TEST: Model.init model = Model.init [] [] - model.menuRow == 2 && - model.pageFirstItem == 0 && - model.cursor == { row: 2, col: 2 } && - model.state == InputAppName { nameBuffer: [], config: emptyConfig } - -expect # TEST: InputAppName to PlatformSelect w/ empty buffer + model.menuRow + == 2 + && model.pageFirstItem + == 0 + && model.cursor + == { row: 2, col: 2 } + && model.state + == InputAppName { nameBuffer: [], config: emptyConfig } + +expect + # TEST: InputAppName to PlatformSelect w/ empty buffer model = Model.init [] [] newModel = Model.toPlatformSelectState model - newModel.state == PlatformSelect { config: { emptyConfig & appName: "main" } } && - newModel.cursor.row == newModel.menuRow + newModel.state + == PlatformSelect { config: { emptyConfig & appName: "main" } } + && newModel.cursor.row + == newModel.menuRow -expect # TEST: InputAppName to PlatformSelect w/ non-empty buffer +expect + # TEST: InputAppName to PlatformSelect w/ non-empty buffer initModel = Model.init [] [] model = { initModel & - state: InputAppName { nameBuffer: ['h', 'e', 'l', 'l', 'o'], config: emptyConfig } + state: InputAppName { nameBuffer: ['h', 'e', 'l', 'l', 'o'], config: emptyConfig }, } newModel = Model.toPlatformSelectState model - newModel.state == PlatformSelect { config: { emptyConfig & appName: "hello" } } && - newModel.cursor.row == newModel.menuRow + newModel.state + == PlatformSelect { config: { emptyConfig & appName: "hello" } } + && newModel.cursor.row + == newModel.menuRow -expect # TEST: InputAppName to PlatformSelect w/ non-empty config +expect + # TEST: InputAppName to PlatformSelect w/ non-empty config initModel = Model.init [] [] model = { initModel & - state: InputAppName { nameBuffer: [], config: { appName: "main", platform: "test", packages: ["test"] } } + state: InputAppName { nameBuffer: [], config: { appName: "main", platform: "test", packages: ["test"] } }, } newModel = Model.toPlatformSelectState model - newModel.state == PlatformSelect { config: { appName: "main", platform: "test", packages: ["test"] } } && - newModel.cursor.row == newModel.menuRow + newModel.state + == PlatformSelect { config: { appName: "main", platform: "test", packages: ["test"] } } + && newModel.cursor.row + == newModel.menuRow -expect # TEST: InputAppName to PlatformSelect w/ empty buffer & existing appName in config +expect + # TEST: InputAppName to PlatformSelect w/ empty buffer & existing appName in config initModel = Model.init [] [] model = { initModel & - state: InputAppName { nameBuffer: [], config: { emptyConfig & appName: "hello" } } + state: InputAppName { nameBuffer: [], config: { emptyConfig & appName: "hello" } }, } newModel = Model.toPlatformSelectState model - newModel.state == PlatformSelect { config: { emptyConfig & appName: "main" } } && - newModel.cursor.row == newModel.menuRow + newModel.state + == PlatformSelect { config: { emptyConfig & appName: "main" } } + && newModel.cursor.row + == newModel.menuRow -expect # TEST: InputAppName to UserExited +expect + # TEST: InputAppName to UserExited model = Model.init [] [] newModel = Model.toUserExitedState model newModel.state == UserExited -expect # TEST: paginate InputAppName - initModel = Model.init [] [] - model = { initModel & state: InputAppName { nameBuffer: ['a'], config: { appName: "b", platform: "c", packages: ["d", "e"]} } } +expect + # TEST: paginate InputAppName + initModel = Model.init [] [] + model = { initModel & state: InputAppName { nameBuffer: ['a'], config: { appName: "b", platform: "c", packages: ["d", "e"] } } } newModel = Model.paginate model model == newModel -expect # TEST: InputAppName - appendToBuffer w/ legal Key +expect + # TEST: InputAppName - appendToBuffer w/ legal Key model = Model.init [] [] - newModel = model + newModel = + model |> Model.appendToBuffer LowerA |> Model.appendToBuffer UpperA |> Model.appendToBuffer LowerZ @@ -75,9 +96,11 @@ expect # TEST: InputAppName - appendToBuffer w/ legal Key |> Model.appendToBuffer Number9 newModel.state == InputAppName { nameBuffer: ['a', 'A', 'z', 'Z', '_', '-', '_', '0', '9'], config: emptyConfig } -expect # TEST: InputAppName - appendToBuffer w/ illegal Key +expect + # TEST: InputAppName - appendToBuffer w/ illegal Key model = Model.init [] [] - newModel = model + newModel = + model |> Model.appendToBuffer Up |> Model.appendToBuffer Down |> Model.appendToBuffer Left @@ -117,61 +140,64 @@ expect # TEST: InputAppName - appendToBuffer w/ illegal Key |> Model.appendToBuffer Delete newModel == model -expect # TEST: InputAppName - backspaceBuffer +expect + # TEST: InputAppName - backspaceBuffer model = Model.init [] [] - newModel = model + newModel = + model |> Model.appendToBuffer LowerA |> Model.backspaceBuffer newModel.state == InputAppName { nameBuffer: [], config: emptyConfig } -expect # TEST: PlatformSelect - moveCursor Up w/ only one item +expect + # TEST: PlatformSelect - moveCursor Up w/ only one item initModel = Model.init ["platform1"] [] model = Model.toPlatformSelectState initModel newModel = model |> Model.moveCursor Up newModel == model -expect # TEST: PlatformSelect - moveCursor Down w/ only one item +expect + # TEST: PlatformSelect - moveCursor Down w/ only one item initModel = Model.init ["platform1"] [] model = Model.toPlatformSelectState initModel newModel = model |> Model.moveCursor Down newModel == model -expect # TEST: PlatformSelect - moveCursor Up w/ cursor starting at bottom - initModel = Model.init ["platform1", "platform2", "platform3"] [] +expect + # TEST: PlatformSelect - moveCursor Up w/ cursor starting at bottom + initModel = + Model.init ["platform1", "platform2", "platform3"] [] |> Model.toPlatformSelectState model = { initModel & - cursor: { row: initModel.menuRow + 2, col: 2 } + cursor: { row: initModel.menuRow + 2, col: 2 }, } newModel = model |> Model.moveCursor Up newModel.cursor.row == model.menuRow + 1 -expect # TEST: PlatformSelect - moveCursor Down w/ cursor starting at top - model = Model.init ["platform1", "platform2", "platform3"] [] +expect + # TEST: PlatformSelect - moveCursor Down w/ cursor starting at top + model = + Model.init ["platform1", "platform2", "platform3"] [] |> Model.toPlatformSelectState newModel = model |> Model.moveCursor Down newModel.cursor.row == model.menuRow + 1 -expect # TEST: PlatformSelect - moveCursor Up w/ cursor starting at top - model = Model.init ["platform1", "platform2", "platform3"] [] +expect + # TEST: PlatformSelect - moveCursor Up w/ cursor starting at top + model = + Model.init ["platform1", "platform2", "platform3"] [] |> Model.toPlatformSelectState newModel = model |> Model.moveCursor Up newModel.cursor.row == model.menuRow + 2 -expect # TEST: PlatformSelect - moveCursor Down w/ cursor starting at bottom - initModel = Model.init ["platform1", "platform2", "platform3"] [] +expect + # TEST: PlatformSelect - moveCursor Down w/ cursor starting at bottom + initModel = + Model.init ["platform1", "platform2", "platform3"] [] |> Model.toPlatformSelectState model = { initModel & - cursor: { row: initModel.menuRow + 2, col: 2 } + cursor: { row: initModel.menuRow + 2, col: 2 }, } newModel = model |> Model.moveCursor Down newModel.cursor.row == model.menuRow - - - - - - - - - diff --git a/src/View.roc b/src/View.roc index 2a78e0f..b8ada83 100644 --- a/src/View.roc +++ b/src/View.roc @@ -11,7 +11,8 @@ renderControlsPrompt = \text, screen -> Core.drawText text { r: screen.height - renderOuterBorder = \screen -> renderBox 0 0 screen.width screen.height (CustomBorder { tl: "╒", t: "═", tr: "╕" }) (Standard Cyan) controlPromptsDict : Dict UserAction Str -controlPromptsDict = Dict.empty {} +controlPromptsDict = + Dict.empty {} |> Dict.insert SingleSelect "ENTER : SELECT" |> Dict.insert MultiSelect "SPACE : SELECT" |> Dict.insert MultiConfirm "ENTER : CONFIRM" @@ -32,7 +33,8 @@ controlPromptsDict = Dict.empty {} |> Dict.insert NextPage "> NEXT" controlPromptsShortDict : Dict UserAction Str -controlPromptsShortDict = Dict.empty {} +controlPromptsShortDict = + Dict.empty {} |> Dict.insert SingleSelect "ENTER" |> Dict.insert MultiSelect "SPACE" |> Dict.insert MultiConfirm "ENTER" @@ -55,7 +57,8 @@ controlPromptsShortDict = Dict.empty {} controlsPromptStr = \model -> actions = Controller.getActions model promptsDict = if model.screen.width // Num.toI32 (List.len actions) < 16 then controlPromptsShortDict else controlPromptsDict - actionStrs = Controller.getActions model + actionStrs = + Controller.getActions model |> List.map \action -> Dict.get promptsDict action |> Result.withDefault "" |> List.dropIf (\str -> Str.isEmpty str) @@ -86,7 +89,7 @@ renderPackageSelect = \model -> renderOuterBorder model.screen, [ renderScreenPrompt "SELECT 0+ PACKAGES:", - Core.drawCursor { fg: Standard Magenta, char: ">"}, + Core.drawCursor { fg: Standard Magenta, char: ">" }, ], renderMultipleChoiceMenu model, @@ -105,7 +108,7 @@ renderInputAppName = \model -> [ renderScreenPrompt "ENTER THE APP NAME:", Core.drawCursor { fg: Standard Magenta, char: ">" }, - Core.drawText (nameBuffer |> Str.fromUtf8 |> Result.withDefault "") { r: 2, c: 4, fg: Standard White } + Core.drawText (nameBuffer |> Str.fromUtf8 |> Result.withDefault "") { r: 2, c: 4, fg: Standard White }, ], ] diff --git a/src/test-stub.roc b/src/test-stub.roc index 78ffd55..af6105e 100644 --- a/src/test-stub.roc +++ b/src/test-stub.roc @@ -10,4 +10,4 @@ import Tests import cli.Task -main = Tests.stubForMain |> Task.ok \ No newline at end of file +main = Tests.stubForMain |> Task.ok