From e8a454b3176f015ada1a2a8ea59ea6a4dfde2a3a Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 12:57:37 +0900 Subject: [PATCH 01/15] Move static member functions to modules. --- src/pocof/Data.fs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index 4d1b24a..a4f6bde 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -131,7 +131,9 @@ module PocofData = | ScrollPageDown // autocomplete | CompleteProperty - static member fromString = + + module Action = + let fromString = tryFromStringExcludes <| set [ "AddQuery" ] @@ -139,20 +141,26 @@ module PocofData = | EQ | LIKE | MATCH - static member fromString = fromString override __.ToString() = toString __ |> String.lower + module Matcher = + let fromString = fromString + type Operator = | AND | OR | NONE - static member fromString = fromString override __.ToString() = toString __ |> String.lower + module Operator = + let fromString = fromString + type Layout = | TopDown | BottomUp - static member fromString = fromString + + module Layout = + let fromString = fromString type PropertySearch = | NoSearch @@ -162,7 +170,9 @@ module PocofData = type Refresh = | Required | NotRequired - static member ofBool = + + module Refresh = + let ofBool = function | true -> Required | _ -> NotRequired From 60904a98f1254725b3ce5724540e7c8ce63119af Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 12:58:41 +0900 Subject: [PATCH 02/15] Add a note for the window beginning position adjustment. --- src/pocof/Data.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index a4f6bde..c77f172 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -367,7 +367,7 @@ module PocofData = let qs = { Query = p.Query Cursor = String.length p.Query - WindowBeginningX = 0 // TODO: adjust with query size. + WindowBeginningX = 0 // NOTE: adjust later. WindowWidth = p.ConsoleWidth } let s = From 63c5fc8bdea623a5e1d9fb85f02b17fc0fa22cd4 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 13:31:18 +0900 Subject: [PATCH 03/15] Resolve the todo for prepareNotification. --- src/pocof.Test/PocofUI.fs | 3 ++- src/pocof/Handle.fs | 24 ++++++++---------------- src/pocof/Query.fs | 6 +++++- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/pocof.Test/PocofUI.fs b/src/pocof.Test/PocofUI.fs index bd25f2c..1e04c46 100644 --- a/src/pocof.Test/PocofUI.fs +++ b/src/pocof.Test/PocofUI.fs @@ -5,6 +5,7 @@ open FsUnitTyped open System open pocof.LanguageExtension open pocof.PocofData +open pocof.PocofQuery open pocof.PocofScreen let generateLine x y = @@ -145,7 +146,7 @@ module ``Buff writeScreen`` = ConsoleWidth = rui.width Refresh = Required} - let state = { state with Notification = pocof.PocofQuery.prepareNotification state } + let state = state |> InternalState.prepareNotification buff.writeTopDown state [] <| Ok [] diff --git a/src/pocof/Handle.fs b/src/pocof/Handle.fs index f5cfbb6..068bd62 100644 --- a/src/pocof/Handle.fs +++ b/src/pocof/Handle.fs @@ -15,9 +15,9 @@ module PocofHandle = QueryState = qs PropertySearch = QueryState.getCurrentProperty qs } |> InternalState.refresh + |> InternalState.prepareNotification - let notification = prepareNotification state - { state with Notification = notification }, pos, { context with Queries = prepareQuery state } + state, pos, { context with Queries = prepareQuery state } let private moveBackward (state: InternalState) (pos: Position) (context: QueryContext) = let qs = QueryState.moveCursor state.QueryState -1 @@ -80,11 +80,9 @@ module PocofHandle = { state with QueryState = qs PropertySearch = QueryState.getCurrentProperty qs } - - let notification = prepareNotification s + |> InternalState.prepareNotification { s with - Notification = notification Refresh = (state.QueryState.Query <> qs.Query || state.QueryState.Cursor <> qs.Cursor) @@ -102,11 +100,9 @@ module PocofHandle = { state with QueryState = qs PropertySearch = QueryState.getCurrentProperty qs } - - let notification = prepareNotification s + |> InternalState.prepareNotification { s with - Notification = notification Refresh = (state.QueryState.Query <> qs.Query || state.QueryState.Cursor <> qs.Cursor) @@ -124,11 +120,9 @@ module PocofHandle = { state with QueryState = qs PropertySearch = QueryState.getCurrentProperty qs } - - let notification = prepareNotification s + |> InternalState.prepareNotification { s with - Notification = notification Refresh = state.QueryState.Query <> qs.Query |> Refresh.ofBool }, @@ -149,11 +143,9 @@ module PocofHandle = { state with QueryState = qs PropertySearch = QueryState.getCurrentProperty qs } - - let notification = prepareNotification s + |> InternalState.prepareNotification { s with - Notification = notification Refresh = state.QueryState.Query <> qs.Query |> Refresh.ofBool }, @@ -167,9 +159,9 @@ module PocofHandle = match state.QueryCondition.Matcher with | EQ -> LIKE | LIKE -> MATCH - | MATCH -> EQ - Notification = prepareNotification state } + | MATCH -> EQ } |> InternalState.refresh + |> InternalState.prepareNotification |> InternalState.updateWindowWidth state, pos, { context with Is = prepareIs state } diff --git a/src/pocof/Query.fs b/src/pocof/Query.fs index 33428d5..b9c4096 100644 --- a/src/pocof/Query.fs +++ b/src/pocof/Query.fs @@ -112,7 +112,7 @@ module PocofQuery = | false, true -> not | _ -> id - let prepareNotification (state: InternalState) = + let private prepareNotification (state: InternalState) = match state.QueryCondition.Matcher with | MATCH -> try @@ -135,6 +135,10 @@ module PocofQuery = Is = is Answer = answer } + type InternalState with // TODO: There might be a better way. + static member prepareNotification state = + { state with Notification = prepareNotification state } + let run (context: QueryContext) (entries: Entry list) (props: Map) = #if DEBUG Logger.logFile context.Queries From 54c620c3dd5611b27f420c6c71e0e97946086c94 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 13:38:51 +0900 Subject: [PATCH 04/15] Refinement. --- src/pocof/Handle.fs | 52 +++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/src/pocof/Handle.fs b/src/pocof/Handle.fs index 068bd62..bace096 100644 --- a/src/pocof/Handle.fs +++ b/src/pocof/Handle.fs @@ -79,16 +79,14 @@ module PocofHandle = let s = { state with QueryState = qs - PropertySearch = QueryState.getCurrentProperty qs } + PropertySearch = QueryState.getCurrentProperty qs + Refresh = + (state.QueryState.Query <> qs.Query + || state.QueryState.Cursor <> qs.Cursor) + |> Refresh.ofBool } |> InternalState.prepareNotification - { s with - Refresh = - (state.QueryState.Query <> qs.Query - || state.QueryState.Cursor <> qs.Cursor) - |> Refresh.ofBool }, - pos, - { context with Queries = prepareQuery s } + s, pos, { context with Queries = prepareQuery s } let private removeForwardChar (state: InternalState) (pos: Position) (context: QueryContext) = match state.QueryState.Cursor with @@ -99,16 +97,14 @@ module PocofHandle = let s = { state with QueryState = qs - PropertySearch = QueryState.getCurrentProperty qs } + PropertySearch = QueryState.getCurrentProperty qs + Refresh = + (state.QueryState.Query <> qs.Query + || state.QueryState.Cursor <> qs.Cursor) + |> Refresh.ofBool } |> InternalState.prepareNotification - { s with - Refresh = - (state.QueryState.Query <> qs.Query - || state.QueryState.Cursor <> qs.Cursor) - |> Refresh.ofBool }, - pos, - { context with Queries = prepareQuery s } + s, pos, { context with Queries = prepareQuery s } let private removeQueryHead (state: InternalState) (pos: Position) (context: QueryContext) = match state.QueryState.Cursor with @@ -119,15 +115,13 @@ module PocofHandle = let s = { state with QueryState = qs - PropertySearch = QueryState.getCurrentProperty qs } + PropertySearch = QueryState.getCurrentProperty qs + Refresh = + state.QueryState.Query <> qs.Query + |> Refresh.ofBool } |> InternalState.prepareNotification - { s with - Refresh = - state.QueryState.Query <> qs.Query - |> Refresh.ofBool }, - pos, - { context with Queries = prepareQuery s } + s, pos, { context with Queries = prepareQuery s } let private removeQueryTail (state: InternalState) (pos: Position) (context: QueryContext) = let l = String.length state.QueryState.Query @@ -142,15 +136,13 @@ module PocofHandle = let s = { state with QueryState = qs - PropertySearch = QueryState.getCurrentProperty qs } + PropertySearch = QueryState.getCurrentProperty qs + Refresh = + state.QueryState.Query <> qs.Query + |> Refresh.ofBool } |> InternalState.prepareNotification - { s with - Refresh = - state.QueryState.Query <> qs.Query - |> Refresh.ofBool }, - pos, - { context with Queries = prepareQuery s } + s, pos, { context with Queries = prepareQuery s } let private switchMatcher (state: InternalState) (pos: Position) (context: QueryContext) = let state = From 4159ab680f57168b23337465b3fbe7f9df81a9b9 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 14:10:55 +0900 Subject: [PATCH 05/15] Refactor updating the query state. --- src/pocof/Data.fs | 5 +++++ src/pocof/Handle.fs | 41 ++++++++++++++--------------------------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index c77f172..34806a5 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -334,6 +334,11 @@ module PocofData = + state.QueryState.Cursor - state.QueryState.WindowBeginningX + let updateQueryState (qs: QueryState) (state: InternalState) = + { state with + QueryState = qs + PropertySearch = QueryState.getCurrentProperty qs } + let refresh (state: InternalState) = { state with Refresh = Required } let noRefresh (state: InternalState) = { state with Refresh = NotRequired } diff --git a/src/pocof/Handle.fs b/src/pocof/Handle.fs index bace096..ac78be5 100644 --- a/src/pocof/Handle.fs +++ b/src/pocof/Handle.fs @@ -11,9 +11,8 @@ module PocofHandle = let qs = QueryState.addQuery state.QueryState s let state = - { state with - QueryState = qs - PropertySearch = QueryState.getCurrentProperty qs } + state + |> InternalState.updateQueryState qs |> InternalState.refresh |> InternalState.prepareNotification @@ -27,10 +26,8 @@ module PocofHandle = | 0 -> NotRequired | _ -> Required - { state with - QueryState = qs - PropertySearch = QueryState.getCurrentProperty qs - Refresh = refresh }, + { state with Refresh = refresh } + |> InternalState.updateQueryState qs, pos, context @@ -42,20 +39,16 @@ module PocofHandle = | true -> Required | _ -> NotRequired - { state with - QueryState = qs - PropertySearch = QueryState.getCurrentProperty qs - Refresh = refresh }, + { state with Refresh = refresh } + |> InternalState.updateQueryState qs, pos, context let private moveHead (state: InternalState) (pos: Position) (context: QueryContext) = let qs = QueryState.setCursor state.QueryState 0 - { state with - QueryState = qs - PropertySearch = NoSearch - Refresh = state.QueryState.Cursor <> 0 |> Refresh.ofBool }, + { state with Refresh = state.QueryState.Cursor <> 0 |> Refresh.ofBool } + |> InternalState.updateQueryState qs, pos, context @@ -63,10 +56,8 @@ module PocofHandle = let l = String.length state.QueryState.Query let qs = QueryState.setCursor state.QueryState l - { state with - QueryState = qs - PropertySearch = QueryState.getCurrentProperty qs - Refresh = state.QueryState.Cursor <> l |> Refresh.ofBool }, + { state with Refresh = state.QueryState.Cursor <> l |> Refresh.ofBool } + |> InternalState.updateQueryState qs, pos, context @@ -78,12 +69,11 @@ module PocofHandle = let s = { state with - QueryState = qs - PropertySearch = QueryState.getCurrentProperty qs Refresh = (state.QueryState.Query <> qs.Query || state.QueryState.Cursor <> qs.Cursor) |> Refresh.ofBool } + |> InternalState.updateQueryState qs |> InternalState.prepareNotification s, pos, { context with Queries = prepareQuery s } @@ -96,12 +86,11 @@ module PocofHandle = let s = { state with - QueryState = qs - PropertySearch = QueryState.getCurrentProperty qs Refresh = (state.QueryState.Query <> qs.Query || state.QueryState.Cursor <> qs.Cursor) |> Refresh.ofBool } + |> InternalState.updateQueryState qs |> InternalState.prepareNotification s, pos, { context with Queries = prepareQuery s } @@ -114,11 +103,10 @@ module PocofHandle = let s = { state with - QueryState = qs - PropertySearch = QueryState.getCurrentProperty qs Refresh = state.QueryState.Query <> qs.Query |> Refresh.ofBool } + |> InternalState.updateQueryState qs |> InternalState.prepareNotification s, pos, { context with Queries = prepareQuery s } @@ -135,11 +123,10 @@ module PocofHandle = let s = { state with - QueryState = qs - PropertySearch = QueryState.getCurrentProperty qs Refresh = state.QueryState.Query <> qs.Query |> Refresh.ofBool } + |> InternalState.updateQueryState qs |> InternalState.prepareNotification s, pos, { context with Queries = prepareQuery s } From 7e68ae7c51ffa7d4efe49c38c4c9ab76592606f7 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 14:15:29 +0900 Subject: [PATCH 06/15] Rename functions. --- src/pocof/Handle.fs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/pocof/Handle.fs b/src/pocof/Handle.fs index ac78be5..fcd6706 100644 --- a/src/pocof/Handle.fs +++ b/src/pocof/Handle.fs @@ -162,7 +162,7 @@ module PocofHandle = Queries = prepareQuery state Test = prepareTest state } - let private switchCaseSensitive (state: InternalState) (pos: Position) (context: QueryContext) = + let private toggleCaseSensitive (state: InternalState) (pos: Position) (context: QueryContext) = let state = { state with InternalState.QueryCondition.CaseSensitive = not state.QueryCondition.CaseSensitive } |> InternalState.refresh @@ -170,7 +170,7 @@ module PocofHandle = state, pos, { context with Is = prepareIs state } - let private switchInvertFilter (state: InternalState) (pos: Position) (context: QueryContext) = + let private toggleInvertFilter (state: InternalState) (pos: Position) (context: QueryContext) = let state = { state with InternalState.QueryCondition.Invert = not state.QueryCondition.Invert } |> InternalState.refresh @@ -178,7 +178,7 @@ module PocofHandle = state, pos, { context with Answer = prepareAnswer state } - let private switchSuppressProperties (state: InternalState) (pos: Position) (context: QueryContext) = + let private toggleSuppressProperties (state: InternalState) (pos: Position) (context: QueryContext) = let state = { state with SuppressProperties = not state.SuppressProperties } |> InternalState.refresh @@ -260,9 +260,9 @@ module PocofHandle = | KillEndOfLine -> removeQueryTail state pos context | RotateMatcher -> switchMatcher state pos context | RotateOperator -> switchOperator state pos context - | ToggleCaseSensitive -> switchCaseSensitive state pos context - | ToggleInvertFilter -> switchInvertFilter state pos context - | ToggleSuppressProperties -> switchSuppressProperties state pos context + | ToggleCaseSensitive -> toggleCaseSensitive state pos context + | ToggleInvertFilter -> toggleInvertFilter state pos context + | ToggleSuppressProperties -> toggleSuppressProperties state pos context | SelectUp -> InternalState.noRefresh state, pos, context // TODO: implement it. | SelectDown -> InternalState.noRefresh state, pos, context // TODO: implement it. | ScrollPageUp -> InternalState.noRefresh state, pos, context // TODO: implement it. From 3fa5a9f456655ef89dcb5a53bb936df88b66549a Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 14:37:29 +0900 Subject: [PATCH 07/15] Refactor toggle state functions. --- src/pocof/Data.fs | 22 ++++++++++++++++++++++ src/pocof/Handle.fs | 9 ++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index 34806a5..2044ba1 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -279,6 +279,13 @@ module PocofData = <| [ " "; string __.Operator ] |> String.concat "" + module QueryCondition = + let toggleCaseSensitive (condition: QueryCondition) = + { condition with CaseSensitive = not condition.CaseSensitive } + + let toggleInvertFilter (condition: QueryCondition) = + { condition with Invert = not condition.Invert } + type InternalState = { QueryState: QueryState QueryCondition: QueryCondition @@ -350,6 +357,21 @@ module PocofData = { state with InternalState.QueryState.WindowWidth = getWindowWidth state } |> adjustCursor + let toggleCaseSensitive (state: InternalState) = + { state with + QueryCondition = + state.QueryCondition + |> QueryCondition.toggleCaseSensitive } + + let toggleInvertFilter (state: InternalState) = + { state with + QueryCondition = + state.QueryCondition + |> QueryCondition.toggleInvertFilter } + + let toggleSuppressProperties (state: InternalState) = + { state with SuppressProperties = not state.SuppressProperties } + type Position = { Y: int; Height: int } type IncomingParameters = diff --git a/src/pocof/Handle.fs b/src/pocof/Handle.fs index fcd6706..bfa2409 100644 --- a/src/pocof/Handle.fs +++ b/src/pocof/Handle.fs @@ -164,7 +164,8 @@ module PocofHandle = let private toggleCaseSensitive (state: InternalState) (pos: Position) (context: QueryContext) = let state = - { state with InternalState.QueryCondition.CaseSensitive = not state.QueryCondition.CaseSensitive } + state + |> InternalState.toggleCaseSensitive |> InternalState.refresh |> InternalState.updateWindowWidth @@ -172,7 +173,8 @@ module PocofHandle = let private toggleInvertFilter (state: InternalState) (pos: Position) (context: QueryContext) = let state = - { state with InternalState.QueryCondition.Invert = not state.QueryCondition.Invert } + state + |> InternalState.toggleInvertFilter |> InternalState.refresh |> InternalState.updateWindowWidth @@ -180,7 +182,8 @@ module PocofHandle = let private toggleSuppressProperties (state: InternalState) (pos: Position) (context: QueryContext) = let state = - { state with SuppressProperties = not state.SuppressProperties } + state + |> InternalState.toggleSuppressProperties |> InternalState.refresh state, pos, context From f06f79d331afe4e54551af31a61cd2bedfa3d28b Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 16:34:01 +0900 Subject: [PATCH 08/15] Refactor handle functions with refreshIfTrue helper. --- src/pocof/Data.fs | 5 ++++ src/pocof/Handle.fs | 57 ++++++++++++++++++++------------------------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index 2044ba1..078b0b6 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -350,6 +350,11 @@ module PocofData = let noRefresh (state: InternalState) = { state with Refresh = NotRequired } + let refreshIfTrue (b: bool) (state: InternalState) = + match b with + | true -> refresh state + | _ -> noRefresh state + let adjustCursor (state: InternalState) = { state with QueryState = QueryState.adjustCursor state.QueryState } diff --git a/src/pocof/Handle.fs b/src/pocof/Handle.fs index bfa2409..b9a2b51 100644 --- a/src/pocof/Handle.fs +++ b/src/pocof/Handle.fs @@ -21,12 +21,8 @@ module PocofHandle = let private moveBackward (state: InternalState) (pos: Position) (context: QueryContext) = let qs = QueryState.moveCursor state.QueryState -1 - let refresh = - match state.QueryState.Cursor with - | 0 -> NotRequired - | _ -> Required - - { state with Refresh = refresh } + state + |> InternalState.refreshIfTrue (state.QueryState.Cursor <> 0) |> InternalState.updateQueryState qs, pos, context @@ -34,12 +30,11 @@ module PocofHandle = let private moveForward (state: InternalState) (pos: Position) (context: QueryContext) = let qs = QueryState.moveCursor state.QueryState 1 - let refresh = - match state.QueryState.Cursor < String.length state.QueryState.Query with - | true -> Required - | _ -> NotRequired - - { state with Refresh = refresh } + state + |> InternalState.refreshIfTrue ( + state.QueryState.Cursor + <> String.length state.QueryState.Query + ) |> InternalState.updateQueryState qs, pos, context @@ -47,7 +42,8 @@ module PocofHandle = let private moveHead (state: InternalState) (pos: Position) (context: QueryContext) = let qs = QueryState.setCursor state.QueryState 0 - { state with Refresh = state.QueryState.Cursor <> 0 |> Refresh.ofBool } + state + |> InternalState.refreshIfTrue (state.QueryState.Cursor <> 0) |> InternalState.updateQueryState qs, pos, context @@ -56,7 +52,8 @@ module PocofHandle = let l = String.length state.QueryState.Query let qs = QueryState.setCursor state.QueryState l - { state with Refresh = state.QueryState.Cursor <> l |> Refresh.ofBool } + state + |> InternalState.refreshIfTrue (state.QueryState.Cursor <> l) |> InternalState.updateQueryState qs, pos, context @@ -68,11 +65,11 @@ module PocofHandle = let qs = QueryState.backspaceQuery state.QueryState 1 let s = - { state with - Refresh = - (state.QueryState.Query <> qs.Query - || state.QueryState.Cursor <> qs.Cursor) - |> Refresh.ofBool } + state + |> InternalState.refreshIfTrue ( + state.QueryState.Query <> qs.Query + || state.QueryState.Cursor <> qs.Cursor + ) |> InternalState.updateQueryState qs |> InternalState.prepareNotification @@ -85,11 +82,11 @@ module PocofHandle = let qs = QueryState.deleteQuery state.QueryState 1 let s = - { state with - Refresh = - (state.QueryState.Query <> qs.Query - || state.QueryState.Cursor <> qs.Cursor) - |> Refresh.ofBool } + state + |> InternalState.refreshIfTrue ( + state.QueryState.Query <> qs.Query + || state.QueryState.Cursor <> qs.Cursor + ) |> InternalState.updateQueryState qs |> InternalState.prepareNotification @@ -102,10 +99,8 @@ module PocofHandle = let qs = QueryState.backspaceQuery state.QueryState state.QueryState.Cursor let s = - { state with - Refresh = - state.QueryState.Query <> qs.Query - |> Refresh.ofBool } + state + |> InternalState.refreshIfTrue (state.QueryState.Query <> qs.Query) |> InternalState.updateQueryState qs |> InternalState.prepareNotification @@ -122,10 +117,8 @@ module PocofHandle = <| l - state.QueryState.Cursor let s = - { state with - Refresh = - state.QueryState.Query <> qs.Query - |> Refresh.ofBool } + state + |> InternalState.refreshIfTrue (state.QueryState.Query <> qs.Query) |> InternalState.updateQueryState qs |> InternalState.prepareNotification From 376b42f092fcdb077f93e5094f61e32d649e3ffb Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 16:52:54 +0900 Subject: [PATCH 09/15] Refactor query handling in PocofHandle and QueryContext. --- src/pocof/Handle.fs | 20 ++++++++++---------- src/pocof/Query.fs | 12 ++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/pocof/Handle.fs b/src/pocof/Handle.fs index b9a2b51..b35e579 100644 --- a/src/pocof/Handle.fs +++ b/src/pocof/Handle.fs @@ -73,7 +73,7 @@ module PocofHandle = |> InternalState.updateQueryState qs |> InternalState.prepareNotification - s, pos, { context with Queries = prepareQuery s } + s, pos, context |> QueryContext.prepareQuery s let private removeForwardChar (state: InternalState) (pos: Position) (context: QueryContext) = match state.QueryState.Cursor with @@ -90,7 +90,7 @@ module PocofHandle = |> InternalState.updateQueryState qs |> InternalState.prepareNotification - s, pos, { context with Queries = prepareQuery s } + s, pos, context |> QueryContext.prepareQuery s let private removeQueryHead (state: InternalState) (pos: Position) (context: QueryContext) = match state.QueryState.Cursor with @@ -104,7 +104,7 @@ module PocofHandle = |> InternalState.updateQueryState qs |> InternalState.prepareNotification - s, pos, { context with Queries = prepareQuery s } + s, pos, context |> QueryContext.prepareQuery s let private removeQueryTail (state: InternalState) (pos: Position) (context: QueryContext) = let l = String.length state.QueryState.Query @@ -122,7 +122,7 @@ module PocofHandle = |> InternalState.updateQueryState qs |> InternalState.prepareNotification - s, pos, { context with Queries = prepareQuery s } + s, pos, context |> QueryContext.prepareQuery s let private switchMatcher (state: InternalState) (pos: Position) (context: QueryContext) = let state = @@ -136,7 +136,7 @@ module PocofHandle = |> InternalState.prepareNotification |> InternalState.updateWindowWidth - state, pos, { context with Is = prepareIs state } + state, pos, context |> QueryContext.prepareIs state let private switchOperator (state: InternalState) (pos: Position) (context: QueryContext) = let state = @@ -151,9 +151,9 @@ module PocofHandle = state, pos, - { context with - Queries = prepareQuery state - Test = prepareTest state } + context + |> QueryContext.prepareQuery state + |> QueryContext.prepareTest state let private toggleCaseSensitive (state: InternalState) (pos: Position) (context: QueryContext) = let state = @@ -162,7 +162,7 @@ module PocofHandle = |> InternalState.refresh |> InternalState.updateWindowWidth - state, pos, { context with Is = prepareIs state } + state, pos, context |> QueryContext.prepareIs state let private toggleInvertFilter (state: InternalState) (pos: Position) (context: QueryContext) = let state = @@ -171,7 +171,7 @@ module PocofHandle = |> InternalState.refresh |> InternalState.updateWindowWidth - state, pos, { context with Answer = prepareAnswer state } + state, pos, context |> QueryContext.prepareAnswer state let private toggleSuppressProperties (state: InternalState) (pos: Position) (context: QueryContext) = let state = diff --git a/src/pocof/Query.fs b/src/pocof/Query.fs index b9c4096..756bd2e 100644 --- a/src/pocof/Query.fs +++ b/src/pocof/Query.fs @@ -139,6 +139,18 @@ module PocofQuery = static member prepareNotification state = { state with Notification = prepareNotification state } + type QueryContext with // TODO: There might be a better way. + static member prepareQuery state context = + { context with Queries = prepareQuery state } + + static member prepareIs state context = { context with Is = prepareIs state } + + static member prepareTest state context = + { context with Test = prepareTest state } + + static member prepareAnswer state context = + { context with Answer = prepareAnswer state } + let run (context: QueryContext) (entries: Entry list) (props: Map) = #if DEBUG Logger.logFile context.Queries From c1e865de7a7587d86ff6a1074d980d0e4e0d7dc0 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 17:29:25 +0900 Subject: [PATCH 10/15] Refactor move and remove functions for the query. --- src/pocof/Handle.fs | 120 ++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 72 deletions(-) diff --git a/src/pocof/Handle.fs b/src/pocof/Handle.fs index b35e579..b109253 100644 --- a/src/pocof/Handle.fs +++ b/src/pocof/Handle.fs @@ -18,68 +18,58 @@ module PocofHandle = state, pos, { context with Queries = prepareQuery state } - let private moveBackward (state: InternalState) (pos: Position) (context: QueryContext) = - let qs = QueryState.moveCursor state.QueryState -1 + let private moveCursor (cursor: int) (limit: int) (state: InternalState) (pos: Position) (context: QueryContext) = + let qs = QueryState.moveCursor state.QueryState cursor state - |> InternalState.refreshIfTrue (state.QueryState.Cursor <> 0) + |> InternalState.refreshIfTrue (state.QueryState.Cursor <> limit) |> InternalState.updateQueryState qs, pos, context - let private moveForward (state: InternalState) (pos: Position) (context: QueryContext) = - let qs = QueryState.moveCursor state.QueryState 1 + let private moveBackward = moveCursor -1 0 - state - |> InternalState.refreshIfTrue ( - state.QueryState.Cursor - <> String.length state.QueryState.Query - ) - |> InternalState.updateQueryState qs, - pos, - context - - let private moveHead (state: InternalState) (pos: Position) (context: QueryContext) = - let qs = QueryState.setCursor state.QueryState 0 + let private moveForward (state: InternalState) = + moveCursor 1 + <| String.length state.QueryState.Query + <| state - state - |> InternalState.refreshIfTrue (state.QueryState.Cursor <> 0) - |> InternalState.updateQueryState qs, - pos, - context - - let private moveTail (state: InternalState) (pos: Position) (context: QueryContext) = - let l = String.length state.QueryState.Query - let qs = QueryState.setCursor state.QueryState l + let private setCursor (cursor: int) (state: InternalState) (pos: Position) (context: QueryContext) = + let qs = QueryState.setCursor state.QueryState cursor state - |> InternalState.refreshIfTrue (state.QueryState.Cursor <> l) + |> InternalState.refreshIfTrue (state.QueryState.Cursor <> cursor) |> InternalState.updateQueryState qs, pos, context - let private removeBackwardChar (state: InternalState) (pos: Position) (context: QueryContext) = + let private moveHead = setCursor 0 + + let private moveTail (state: InternalState) = + setCursor + <| String.length state.QueryState.Query + <| state + + type private Direction = + | Backward + | Forward + + let private removeChar + (direction: Direction) + (size: int) + (limit: int) + (state: InternalState) + (pos: Position) + (context: QueryContext) + = match state.QueryState.Cursor with - | 0 -> InternalState.noRefresh state, pos, context + | x when x = limit -> InternalState.noRefresh state, pos, context | _ -> - let qs = QueryState.backspaceQuery state.QueryState 1 - - let s = - state - |> InternalState.refreshIfTrue ( - state.QueryState.Query <> qs.Query - || state.QueryState.Cursor <> qs.Cursor - ) - |> InternalState.updateQueryState qs - |> InternalState.prepareNotification - - s, pos, context |> QueryContext.prepareQuery s - - let private removeForwardChar (state: InternalState) (pos: Position) (context: QueryContext) = - match state.QueryState.Cursor with - | x when x = String.length state.QueryState.Query -> InternalState.noRefresh state, pos, context - | _ -> - let qs = QueryState.deleteQuery state.QueryState 1 + let qs = + match direction with + | Backward -> QueryState.backspaceQuery state.QueryState + | Forward -> QueryState.deleteQuery state.QueryState + <| size let s = state @@ -92,37 +82,23 @@ module PocofHandle = s, pos, context |> QueryContext.prepareQuery s - let private removeQueryHead (state: InternalState) (pos: Position) (context: QueryContext) = - match state.QueryState.Cursor with - | 0 -> InternalState.noRefresh state, pos, context - | _ -> - let qs = QueryState.backspaceQuery state.QueryState state.QueryState.Cursor + let private removeBackwardChar = removeChar Backward 1 0 - let s = - state - |> InternalState.refreshIfTrue (state.QueryState.Query <> qs.Query) - |> InternalState.updateQueryState qs - |> InternalState.prepareNotification + let private removeForwardChar (state: InternalState) = + removeChar Forward 1 + <| String.length state.QueryState.Query + <| state - s, pos, context |> QueryContext.prepareQuery s + let private removeQueryHead (state: InternalState) = + removeChar Backward state.QueryState.Cursor 0 state - let private removeQueryTail (state: InternalState) (pos: Position) (context: QueryContext) = + let private removeQueryTail (state: InternalState) = let l = String.length state.QueryState.Query - match state.QueryState.Cursor with - | x when x = l -> InternalState.noRefresh state, pos, context - | _ -> - let qs = - QueryState.deleteQuery state.QueryState - <| l - state.QueryState.Cursor - - let s = - state - |> InternalState.refreshIfTrue (state.QueryState.Query <> qs.Query) - |> InternalState.updateQueryState qs - |> InternalState.prepareNotification - - s, pos, context |> QueryContext.prepareQuery s + removeChar Forward + <| l - state.QueryState.Cursor + <| l + <| state let private switchMatcher (state: InternalState) (pos: Position) (context: QueryContext) = let state = From 7963afcc9ea8ab3f9fb2b28d765959f0805aae94 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 19:19:10 +0900 Subject: [PATCH 11/15] Refinement. --- src/pocof/Handle.fs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/pocof/Handle.fs b/src/pocof/Handle.fs index b109253..cb1a8c9 100644 --- a/src/pocof/Handle.fs +++ b/src/pocof/Handle.fs @@ -57,11 +57,15 @@ module PocofHandle = let private removeChar (direction: Direction) (size: int) - (limit: int) (state: InternalState) (pos: Position) (context: QueryContext) = + let limit = + match direction with + | Backward -> 0 + | Forward -> String.length state.QueryState.Query + match state.QueryState.Cursor with | x when x = limit -> InternalState.noRefresh state, pos, context | _ -> @@ -82,22 +86,17 @@ module PocofHandle = s, pos, context |> QueryContext.prepareQuery s - let private removeBackwardChar = removeChar Backward 1 0 + let private removeBackwardChar = removeChar Backward 1 - let private removeForwardChar (state: InternalState) = - removeChar Forward 1 - <| String.length state.QueryState.Query - <| state + let private removeForwardChar = removeChar Forward 1 let private removeQueryHead (state: InternalState) = - removeChar Backward state.QueryState.Cursor 0 state + removeChar Backward state.QueryState.Cursor state let private removeQueryTail (state: InternalState) = - let l = String.length state.QueryState.Query - removeChar Forward - <| l - state.QueryState.Cursor - <| l + <| String.length state.QueryState.Query + - state.QueryState.Cursor <| state let private switchMatcher (state: InternalState) (pos: Position) (context: QueryContext) = From 099e634628a5a8c4fd1d51aa216c03e7b8ad010c Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 19:24:36 +0900 Subject: [PATCH 12/15] Refactor query matcher and operator rotation. --- src/pocof/Data.fs | 28 ++++++++++++++++++++++++++++ src/pocof/Handle.fs | 16 ++++------------ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index 078b0b6..620cb72 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -280,6 +280,22 @@ module PocofData = |> String.concat "" module QueryCondition = + let rotateMatcher (condition: QueryCondition) = + { condition with + Matcher = + match condition.Matcher with + | EQ -> LIKE + | LIKE -> MATCH + | MATCH -> EQ } + + let rotateOperator (condition: QueryCondition) = + { condition with + Operator = + match condition.Operator with + | OR -> AND + | AND -> NONE + | NONE -> OR } + let toggleCaseSensitive (condition: QueryCondition) = { condition with CaseSensitive = not condition.CaseSensitive } @@ -362,6 +378,18 @@ module PocofData = { state with InternalState.QueryState.WindowWidth = getWindowWidth state } |> adjustCursor + let rotateMatcher (state: InternalState) = + { state with + QueryCondition = + state.QueryCondition + |> QueryCondition.rotateMatcher } + + let rotateOperator (state: InternalState) = + { state with + QueryCondition = + state.QueryCondition + |> QueryCondition.rotateOperator } + let toggleCaseSensitive (state: InternalState) = { state with QueryCondition = diff --git a/src/pocof/Handle.fs b/src/pocof/Handle.fs index cb1a8c9..849ec2e 100644 --- a/src/pocof/Handle.fs +++ b/src/pocof/Handle.fs @@ -101,12 +101,8 @@ module PocofHandle = let private switchMatcher (state: InternalState) (pos: Position) (context: QueryContext) = let state = - { state with - InternalState.QueryCondition.Matcher = - match state.QueryCondition.Matcher with - | EQ -> LIKE - | LIKE -> MATCH - | MATCH -> EQ } + state + |> InternalState.rotateMatcher |> InternalState.refresh |> InternalState.prepareNotification |> InternalState.updateWindowWidth @@ -115,12 +111,8 @@ module PocofHandle = let private switchOperator (state: InternalState) (pos: Position) (context: QueryContext) = let state = - { state with - InternalState.QueryCondition.Operator = - match state.QueryCondition.Operator with - | OR -> AND - | AND -> NONE - | NONE -> OR } + state + |> InternalState.rotateOperator |> InternalState.refresh |> InternalState.updateWindowWidth From 5b4d86e86175b67a0535f0425ac88a7848be5372 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Wed, 3 Jan 2024 19:27:11 +0900 Subject: [PATCH 13/15] Remove the unused function Refresh.ofBool. --- src/pocof/Data.fs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index 620cb72..fbcdbc5 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -171,12 +171,6 @@ module PocofData = | Required | NotRequired - module Refresh = - let ofBool = - function - | true -> Required - | _ -> NotRequired - type KeyPattern = { Modifier: int; Key: ConsoleKey } type InternalConfig = From 984f419f2a85c5e4e051d48e88cbb64067d8c142 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Thu, 4 Jan 2024 11:16:33 +0900 Subject: [PATCH 14/15] Rewrite the type extension to simple module. --- src/pocof/Query.fs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/pocof/Query.fs b/src/pocof/Query.fs index 756bd2e..ad49345 100644 --- a/src/pocof/Query.fs +++ b/src/pocof/Query.fs @@ -135,20 +135,20 @@ module PocofQuery = Is = is Answer = answer } - type InternalState with // TODO: There might be a better way. - static member prepareNotification state = + module InternalState = + let prepareNotification state = { state with Notification = prepareNotification state } - type QueryContext with // TODO: There might be a better way. - static member prepareQuery state context = + module QueryContext = + let prepareQuery state context = { context with Queries = prepareQuery state } - static member prepareIs state context = { context with Is = prepareIs state } + let prepareIs state context = { context with Is = prepareIs state } - static member prepareTest state context = + let prepareTest state context = { context with Test = prepareTest state } - static member prepareAnswer state context = + let prepareAnswer state context = { context with Answer = prepareAnswer state } let run (context: QueryContext) (entries: Entry list) (props: Map) = From 02fd6be928700bcba83e2308dacaff7e11204af9 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Thu, 4 Jan 2024 11:22:19 +0900 Subject: [PATCH 15/15] Make prepare* functions private. --- src/pocof/Handle.fs | 4 ++-- src/pocof/Query.fs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/pocof/Handle.fs b/src/pocof/Handle.fs index 849ec2e..bc41890 100644 --- a/src/pocof/Handle.fs +++ b/src/pocof/Handle.fs @@ -16,7 +16,7 @@ module PocofHandle = |> InternalState.refresh |> InternalState.prepareNotification - state, pos, { context with Queries = prepareQuery state } + state, pos, context |> QueryContext.prepareQuery state let private moveCursor (cursor: int) (limit: int) (state: InternalState) (pos: Position) (context: QueryContext) = let qs = QueryState.moveCursor state.QueryState cursor @@ -179,7 +179,7 @@ module PocofHandle = |> InternalState.refresh |> InternalState.adjustCursor - state, pos, { context with Queries = prepareQuery state } + state, pos, context |> QueryContext.prepareQuery state match state.PropertySearch with | NoSearch -> InternalState.noRefresh state, pos, context diff --git a/src/pocof/Query.fs b/src/pocof/Query.fs index ad49345..5fac48f 100644 --- a/src/pocof/Query.fs +++ b/src/pocof/Query.fs @@ -85,7 +85,7 @@ module PocofQuery = <| zs | _ -> parseQuery <| Normal x :: acc <| xs - let prepareQuery (state: InternalState) = + let private prepareQuery (state: InternalState) = match state.QueryCondition.Operator with | NONE -> [ Normal state.QueryState.Query ] | _ -> @@ -95,19 +95,19 @@ module PocofQuery = |> List.ofSeq |> parseQuery [] - let prepareTest (state: InternalState) = + let private prepareTest (state: InternalState) = match state.QueryCondition.Operator with | OR -> List.exists | _ -> List.forall - let prepareIs (state: InternalState) = + let private prepareIs (state: InternalState) = match state.QueryCondition.Matcher with | EQ -> equals << equalOpt | LIKE -> likes << likeOpt | MATCH -> matches << matchOpt <| state.QueryCondition.CaseSensitive - let prepareAnswer (state: InternalState) = + let private prepareAnswer (state: InternalState) = match String.IsNullOrWhiteSpace state.QueryState.Query, state.QueryCondition.Invert with | false, true -> not | _ -> id