From 4ea66069b26f61a7f54abe79c3fae7a9e6c80981 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Sat, 4 Nov 2023 21:13:52 +0900 Subject: [PATCH 1/3] Simple implementation of bottom up layout. --- src/pocof/Library.fs | 4 +--- src/pocof/UI.fs | 57 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/pocof/Library.fs b/src/pocof/Library.fs index d22d3202..3041f58d 100644 --- a/src/pocof/Library.fs +++ b/src/pocof/Library.fs @@ -125,9 +125,7 @@ type SelectPocofCommand() = member val Prompt = "query" with get, set [] - [] + [] member val Layout = string TopDown with get, set [] diff --git a/src/pocof/UI.fs b/src/pocof/UI.fs index 8fdc10f5..22f6c6e4 100644 --- a/src/pocof/UI.fs +++ b/src/pocof/UI.fs @@ -67,10 +67,12 @@ module PocofScreen = (entries: PocofData.Entry list) (props: Result) = - __.writeScreenLine 0 + let basePosition = 0 + + __.writeScreenLine basePosition <| __.prompt + ">" + state.Query - __.writeRightInfo state entries.Length 0 + __.writeRightInfo state entries.Length basePosition // PocofDebug.logFile "./debug.log" [ List.length entries ] @@ -106,7 +108,7 @@ module PocofScreen = __.setCursorPosition <| __.getCursorPositionX state.Query x - <| 0 + <| basePosition member __.writeBottomUp (state: PocofData.InternalState) @@ -114,10 +116,55 @@ module PocofScreen = (entries: PocofData.Entry list) (props: Result) = - // TODO: implement it from Write-BottomUp. + let basePosition = __.rui.WindowSize.Height - 1 + + __.setCursorPosition + <| __.getCursorPositionX state.Query x + <| basePosition + + __.writeScreenLine basePosition + <| __.prompt + ">" + state.Query + + __.writeRightInfo state entries.Length basePosition + + // PocofDebug.logFile "./debug.log" [ List.length entries ] + + __.writeScreenLine (basePosition - 1) + <| match state.Notification with + | "" -> + match props with + | Ok (p) -> (String.concat " " p).[.. __.rui.WindowSize.Width - 1] + | Error (e) -> "note>" + e + | _ -> "note>" + state.Notification + + let h = __.rui.WindowSize.Height - 3 + + let out = + match List.length entries < h with + | true -> entries + | _ -> List.take h entries + |> PocofData.unwrap + |> __.invoke + |> Seq.fold + (fun acc s -> + s.Split Environment.NewLine + |> List.ofArray + |> (@) acc) + [] + + seq { 0..h } + |> Seq.iter (fun i -> + match List.tryItem i out with + | Some s -> __.writeScreenLine <| basePosition - i - 2 <| s + // logFile "./debug.log" [ s ] + | None -> + __.writeScreenLine + <| basePosition - i - 2 + <| String.Empty) + __.setCursorPosition <| __.getCursorPositionX state.Query x - <| __.rui.CursorPosition.Y + <| basePosition let init (rui: PSHostRawUserInterface) (prompt: string) (invoke: list -> seq) = let buf = new Buff(rui, prompt, invoke, Console.TreatControlCAsInput) From 1c59038aeb9698d5d1febabb1405e735434b591b Mon Sep 17 00:00:00 2001 From: krymtkts Date: Sat, 11 Nov 2023 11:40:20 +0900 Subject: [PATCH 2/3] Add pester tests for checking Layout parameters. --- tests/pocof.Tests.ps1 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/pocof.Tests.ps1 b/tests/pocof.Tests.ps1 index 091a508b..377e2da4 100644 --- a/tests/pocof.Tests.ps1 +++ b/tests/pocof.Tests.ps1 @@ -25,6 +25,24 @@ Describe 'pocof' { $_ | Should @p } } + + Context 'Checking Select-Pocof parameters' -ForEach @{ + InputObject = 'Hello,world' + Expected = 'Hello,world' + BaseParam = @{NonInteractive = $true }; + } { + Context 'In mode with empty query' -ForEach @( + @{ Layout = 'TopDown' }, + @{ Layout = 'BottomUp' } + ) { + It "Given Layout '', '' should be returned." -TestCases @( + @{Params = $BaseParam + $_ } + ) { + $InputObject | Select-Pocof @Params -ErrorAction SilentlyContinue | Should -BeExactly -ExpectedValue $Expected + } + } + } + Context 'Select-Pocof cmdlet' -ForEach @{ InputObject = 'Hello,world'; BaseParam = @{NonInteractive = $true }; } { From a50e66f19641756fbd46d5da5c0c3544139d74fd Mon Sep 17 00:00:00 2001 From: krymtkts Date: Sat, 11 Nov 2023 13:16:52 +0900 Subject: [PATCH 3/3] Refactor. --- src/pocof/UI.fs | 78 ++++++++++++------------------------------------- 1 file changed, 18 insertions(+), 60 deletions(-) diff --git a/src/pocof/UI.fs b/src/pocof/UI.fs index 22f6c6e4..ef697cf5 100644 --- a/src/pocof/UI.fs +++ b/src/pocof/UI.fs @@ -61,13 +61,19 @@ module PocofScreen = line.PadRight __.rui.WindowSize.Width |> Console.Write - member __.writeTopDown + member __.writeScreen + (layout: PocofData.Layout) (state: PocofData.InternalState) (x: int) (entries: PocofData.Entry list) (props: Result) = - let basePosition = 0 + let basePosition, firstLine, toHeight = + match layout with + | PocofData.TopDown -> 0, 1, (+) 2 + | PocofData.BottomUp -> + let basePosition = __.rui.WindowSize.Height - 1 + basePosition, basePosition - 1, (-) (basePosition - 2) __.writeScreenLine basePosition <| __.prompt + ">" + state.Query @@ -76,7 +82,7 @@ module PocofScreen = // PocofDebug.logFile "./debug.log" [ List.length entries ] - __.writeScreenLine 1 + __.writeScreenLine firstLine <| match state.Notification with | "" -> match props with @@ -101,70 +107,22 @@ module PocofScreen = seq { 0..h } |> Seq.iter (fun i -> - match List.tryItem i out with - | Some s -> __.writeScreenLine <| i + 2 <| s - // logFile "./debug.log" [ s ] - | None -> __.writeScreenLine <| i + 2 <| String.Empty) + __.writeScreenLine + <| toHeight i + <| match List.tryItem i out with + | Some s -> + // logFile "./debug.log" [ s ] + s + | None -> String.Empty) __.setCursorPosition <| __.getCursorPositionX state.Query x <| basePosition - member __.writeBottomUp - (state: PocofData.InternalState) - (x: int) - (entries: PocofData.Entry list) - (props: Result) - = - let basePosition = __.rui.WindowSize.Height - 1 - - __.setCursorPosition - <| __.getCursorPositionX state.Query x - <| basePosition - - __.writeScreenLine basePosition - <| __.prompt + ">" + state.Query - - __.writeRightInfo state entries.Length basePosition - // PocofDebug.logFile "./debug.log" [ List.length entries ] - - __.writeScreenLine (basePosition - 1) - <| match state.Notification with - | "" -> - match props with - | Ok (p) -> (String.concat " " p).[.. __.rui.WindowSize.Width - 1] - | Error (e) -> "note>" + e - | _ -> "note>" + state.Notification + member __.writeTopDown = __.writeScreen PocofData.TopDown - let h = __.rui.WindowSize.Height - 3 - - let out = - match List.length entries < h with - | true -> entries - | _ -> List.take h entries - |> PocofData.unwrap - |> __.invoke - |> Seq.fold - (fun acc s -> - s.Split Environment.NewLine - |> List.ofArray - |> (@) acc) - [] - - seq { 0..h } - |> Seq.iter (fun i -> - match List.tryItem i out with - | Some s -> __.writeScreenLine <| basePosition - i - 2 <| s - // logFile "./debug.log" [ s ] - | None -> - __.writeScreenLine - <| basePosition - i - 2 - <| String.Empty) - - __.setCursorPosition - <| __.getCursorPositionX state.Query x - <| basePosition + member __.writeBottomUp = __.writeScreen PocofData.BottomUp let init (rui: PSHostRawUserInterface) (prompt: string) (invoke: list -> seq) = let buf = new Buff(rui, prompt, invoke, Console.TreatControlCAsInput)