Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor internal state handlings #109

Merged
merged 15 commits into from Jan 4, 2024
Merged
3 changes: 2 additions & 1 deletion src/pocof.Test/PocofUI.fs
Expand Up @@ -5,6 +5,7 @@ open FsUnitTyped
open System
open pocof.LanguageExtension
open pocof.PocofData
open pocof.PocofQuery
open pocof.PocofScreen

let generateLine x y =
Expand Down Expand Up @@ -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 []

Expand Down
82 changes: 73 additions & 9 deletions src/pocof/Data.fs
Expand Up @@ -131,28 +131,36 @@ module PocofData =
| ScrollPageDown
// autocomplete
| CompleteProperty
static member fromString =

module Action =
let fromString =
tryFromStringExcludes<Action>
<| set [ "AddQuery" ]

type Matcher =
| EQ
| LIKE
| MATCH
static member fromString = fromString<Matcher>
override __.ToString() = toString __ |> String.lower

module Matcher =
let fromString = fromString<Matcher>

type Operator =
| AND
| OR
| NONE
static member fromString = fromString<Operator>
override __.ToString() = toString __ |> String.lower

module Operator =
let fromString = fromString<Operator>

type Layout =
| TopDown
| BottomUp
static member fromString = fromString<Layout>

module Layout =
let fromString = fromString<Layout>

type PropertySearch =
| NoSearch
Expand All @@ -162,10 +170,6 @@ module PocofData =
type Refresh =
| Required
| NotRequired
static member ofBool =
function
| true -> Required
| _ -> NotRequired

type KeyPattern = { Modifier: int; Key: ConsoleKey }

Expand Down Expand Up @@ -269,6 +273,29 @@ module PocofData =
<| [ " "; string __.Operator ]
|> 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 }

let toggleInvertFilter (condition: QueryCondition) =
{ condition with Invert = not condition.Invert }

type InternalState =
{ QueryState: QueryState
QueryCondition: QueryCondition
Expand Down Expand Up @@ -324,17 +351,54 @@ 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 }

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 }

let updateWindowWidth (state: InternalState) =
{ 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 =
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 =
Expand All @@ -357,7 +421,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 =
Expand Down