Skip to content

Commit

Permalink
Merge pull request #109 from krymtkts:feature/refactor
Browse files Browse the repository at this point in the history
Refactor internal state handlings
  • Loading branch information
krymtkts committed Jan 4, 2024
2 parents 563324f + 02fd6be commit 44681b5
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 168 deletions.
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

0 comments on commit 44681b5

Please sign in to comment.