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

Simplify console width calculation. #285

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/pocof.Test/Handle.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ module invokeAction =
PropertyMap = Map []
Prompt = "query"
WordDelimiters = ";:,.[]{}()/\\|!?^&*-=+'\"–—―"
ConsoleWidth = 60
ConsoleWidth = 0
Refresh = Refresh.Required }
|> InternalState.updateWindowWidth
|> InternalState.updateConsoleWidth 60

let position: Position = { Y = 0; Height = 20 }

Expand Down
44 changes: 16 additions & 28 deletions src/pocof.Test/Screen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,7 @@ module ``Buff writeScreen`` =
[<Fact>]
let ``should render top down.`` () =
let rui = new MockRawUI()

let state =
{ state with ConsoleWidth = rui.width } |> InternalState.updateWindowWidth

let state = state |> InternalState.updateConsoleWidth rui.width
let rui = getRenderedScreen rui state Layout.TopDown

let expected =
Expand All @@ -197,9 +194,7 @@ module ``Buff writeScreen`` =
(rui :> IRawUI).GetCursorPosition()
|> (fun (x, y) -> (rui :> IRawUI).SetCursorPosition <| x / 2 <| y / 2 + 1)

let state =
{ state with ConsoleWidth = rui.width } |> InternalState.updateWindowWidth

let state = state |> InternalState.updateConsoleWidth rui.width
let rui = getRenderedScreen rui state Layout.TopDownHalf

let expected =
Expand Down Expand Up @@ -228,9 +223,8 @@ module ``Buff writeScreen`` =
Operator = Operator.Or
CaseSensitive = false
Invert = true }
Prompt = "prompt"
ConsoleWidth = rui.width }
|> InternalState.updateWindowWidth
Prompt = "prompt" }
|> InternalState.updateConsoleWidth rui.width

let rui = getRenderedScreen rui state Layout.BottomUp

Expand All @@ -256,9 +250,8 @@ module ``Buff writeScreen`` =
Operator = Operator.Or
CaseSensitive = false
Invert = true }
Prompt = "prompt"
ConsoleWidth = rui.width }
|> InternalState.updateWindowWidth
Prompt = "prompt" }
|> InternalState.updateConsoleWidth rui.width

let rui = getRenderedScreen rui state Layout.BottomUpHalf

Expand All @@ -280,9 +273,8 @@ module ``Buff writeScreen`` =
InternalState.QueryState.Query = @"\"
InternalState.QueryState.Cursor = 1
InternalState.QueryCondition.CaseSensitive = false
Prompt = "prompt"
ConsoleWidth = rui.width }
|> InternalState.updateWindowWidth
Prompt = "prompt" }
|> InternalState.updateConsoleWidth rui.width
|> Pocof.Query.InternalState.prepareNotification

let rui = getRenderedScreen rui state Layout.TopDown
Expand All @@ -309,9 +301,8 @@ module ``Buff writeScreen`` =
PropertySearch = PropertySearch.Search("")
Properties = props
PropertyMap = props |> List.map (fun s -> (s.ToLower(), s)) |> Map
Prompt = "prompt"
ConsoleWidth = rui.width }
|> InternalState.updateWindowWidth
Prompt = "prompt" }
|> InternalState.updateConsoleWidth rui.width
|> Pocof.Query.InternalState.prepareNotification

buff.WriteScreen Layout.TopDown state PSeq.empty
Expand All @@ -335,9 +326,8 @@ module ``Buff writeScreen`` =
InternalState.QueryState.Query = @":unknown"
InternalState.QueryState.Cursor = 8
InternalState.QueryCondition.CaseSensitive = false
Prompt = "prompt"
ConsoleWidth = rui.width }
|> InternalState.updateWindowWidth
Prompt = "prompt" }
|> InternalState.updateConsoleWidth rui.width

buff.WriteScreen Layout.TopDown state PSeq.empty <| Error "Property not found"

Expand All @@ -363,9 +353,8 @@ module ``Buff writeScreen`` =
InternalState.QueryState.Query = ""
InternalState.QueryState.Cursor = 0
InternalState.QueryCondition.CaseSensitive = false
Prompt = "prompt"
ConsoleWidth = rui.width }
|> InternalState.updateWindowWidth
Prompt = "prompt" }
|> InternalState.updateConsoleWidth rui.width

let entries =
[ 1..10 ]
Expand Down Expand Up @@ -398,9 +387,8 @@ module ``Buff writeScreen`` =
InternalState.QueryState.Query = ""
InternalState.QueryState.Cursor = 0
InternalState.QueryCondition.CaseSensitive = false
Prompt = "prompt"
ConsoleWidth = rui.width }
|> InternalState.updateWindowWidth
Prompt = "prompt" }
|> InternalState.updateConsoleWidth rui.width

let entries =
[ 1..100 ]
Expand Down
21 changes: 4 additions & 17 deletions src/pocof/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,6 @@ module Data =
let queryInfo (state: InternalState) (count: int) =
$" %O{state.QueryCondition} [%d{count}]"

let getWindowWidth (state: InternalState) =
let left = prompt state

#if DEBUG
Logger.LogFile [ $"ConsoleWidth '{state.ConsoleWidth}' left '{String.length left}'" ]
#endif
// TODO: it will decide at startup.
state.ConsoleWidth - String.length left - 1 // TODO: to adjust the right margin.

let getX (state: InternalState) =
(prompt state |> String.length) + state.QueryState.Cursor
- state.QueryState.WindowBeginningCursor
Expand All @@ -500,10 +491,6 @@ module Data =
| _ -> noRefresh
<| state

let updateWindowWidth (state: InternalState) =
{ state with
InternalState.QueryState.WindowWidth = getWindowWidth state }

let rotateMatcher (state: InternalState) =
{ state with
QueryCondition = state.QueryCondition |> QueryCondition.rotateMatcher }
Expand All @@ -526,8 +513,8 @@ module Data =

let updateConsoleWidth (consoleWidth: int) (state: InternalState) =
{ state with
ConsoleWidth = consoleWidth }
|> updateWindowWidth
ConsoleWidth = consoleWidth
InternalState.QueryState.WindowWidth = prompt state |> String.length |> (+) 1 |> (-) consoleWidth }

type Position = { Y: int; Height: int }

Expand Down Expand Up @@ -572,9 +559,9 @@ module Data =
PropertyMap = p.PropertiesMap
Prompt = p.Prompt
WordDelimiters = p.WordDelimiters
ConsoleWidth = p.ConsoleWidth
ConsoleWidth = 0 // NOTE: adjust later.
Refresh = Refresh.Required }
|> InternalState.updateWindowWidth
|> InternalState.updateConsoleWidth p.ConsoleWidth

{ Layout = Layout.fromString p.Layout
Keymaps = p.Keymaps
Expand Down
19 changes: 3 additions & 16 deletions src/pocof/Handle.fs
Original file line number Diff line number Diff line change
Expand Up @@ -289,34 +289,21 @@ module Handle =
|> InternalState.rotateMatcher
|> InternalState.refresh
|> InternalState.prepareNotification
|> InternalState.updateWindowWidth

state, pos, context |> QueryContext.prepareQuery state

let private rotateOperator (state: InternalState) (pos: Position) (context: QueryContext) =
let state =
state
|> InternalState.rotateOperator
|> InternalState.refresh
|> InternalState.updateWindowWidth
let state = state |> InternalState.rotateOperator |> InternalState.refresh

state, pos, context |> QueryContext.prepareQuery state |> QueryContext.prepareTest state

let private toggleCaseSensitive (state: InternalState) (pos: Position) (context: QueryContext) =
let state =
state
|> InternalState.toggleCaseSensitive
|> InternalState.refresh
|> InternalState.updateWindowWidth
let state = state |> InternalState.toggleCaseSensitive |> InternalState.refresh

state, pos, context |> QueryContext.prepareQuery state

let private toggleInvertFilter (state: InternalState) (pos: Position) (context: QueryContext) =
let state =
state
|> InternalState.toggleInvertFilter
|> InternalState.refresh
|> InternalState.updateWindowWidth
let state = state |> InternalState.toggleInvertFilter |> InternalState.refresh

state, pos, context |> QueryContext.prepareQuery state

Expand Down
Loading