Skip to content

Commit

Permalink
Merge pull request #298 from krymtkts:feature/cleanup-prompt
Browse files Browse the repository at this point in the history
Refactor internal state initialization.
  • Loading branch information
krymtkts authored Jan 5, 2025
2 parents 4753f92 + 17bc104 commit eada57f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 44 deletions.
3 changes: 2 additions & 1 deletion src/pocof.Test/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ module initConfig =
SuppressProperties = true
Properties = [ "name"; "attributes" ]
PropertyMap = Map [ ("name", "name"); ("attributes", "attributes") ]
Prompt = "prompt"
Prompt = "prompt>"
PromptLength = 7
WordDelimiters = ";:,.[]{}()/\\|!?^&*-=+'\"–—―"
ConsoleWidth = 60
Refresh = Refresh.Required },
Expand Down
3 changes: 2 additions & 1 deletion src/pocof.Test/Handle.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ module invokeAction =
SuppressProperties = false
Properties = []
PropertyMap = Map []
Prompt = "query"
Prompt = "query>"
PromptLength = 6
WordDelimiters = ";:,.[]{}()/\\|!?^&*-=+'\"–—―"
ConsoleWidth = 0
Refresh = Refresh.Required }
Expand Down
3 changes: 2 additions & 1 deletion src/pocof.Test/Pocof.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ let initState () : InternalState =
SuppressProperties = false
Properties = [ "Name"; "LastModified"; "Path" ]
PropertyMap = Map [ ("name", "Name"); ("lastmodified", "LastWriteTime"); ("path", "FullName") ]
Prompt = "query"
Prompt = "query>"
PromptLength = 6
WordDelimiters = ";:,.[]{}()/\\|!?^&*-=+'\"–—―"
ConsoleWidth = 60
Refresh = Refresh.Required }
Expand Down
3 changes: 2 additions & 1 deletion src/pocof.Test/Query.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ let initState () : Data.InternalState =
SuppressProperties = false
Properties = [ "Name"; "Attribute"; "Length" ]
PropertyMap = Map [ ("name", "Name"); ("attribute", "Attribute"); ("length", "Length") ]
Prompt = "query"
Prompt = "query>"
PromptLength = 6
WordDelimiters = ";:,.[]{}()/\\|!?^&*-=+'\"–—―"
ConsoleWidth = 60
Refresh = Data.Refresh.Required }
Expand Down
28 changes: 18 additions & 10 deletions src/pocof.Test/Screen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ module ``Buff writeScreen`` =
SuppressProperties = false
Properties = []
PropertyMap = Map []
Prompt = "query"
Prompt = "query>"
PromptLength = 6
WordDelimiters = ";:,.[]{}()/\\|!?^&*-=+'\"–—―"
ConsoleWidth = 0
Refresh = Refresh.Required }
Expand Down Expand Up @@ -222,7 +223,8 @@ module ``Buff writeScreen`` =
Operator = Operator.Or
CaseSensitive = false
Invert = true }
Prompt = "prompt" }
Prompt = "prompt>"
PromptLength = 7 }
|> InternalState.updateConsoleWidth rui.width

let rui = getRenderedScreen rui state Layout.BottomUp
Expand All @@ -249,7 +251,8 @@ module ``Buff writeScreen`` =
Operator = Operator.Or
CaseSensitive = false
Invert = true }
Prompt = "prompt" }
Prompt = "prompt>"
PromptLength = 7 }
|> InternalState.updateConsoleWidth rui.width

let rui = getRenderedScreen rui state Layout.BottomUpHalf
Expand All @@ -272,7 +275,8 @@ module ``Buff writeScreen`` =
InternalState.QueryState.Query = @"\"
InternalState.QueryState.Cursor = 1
InternalState.QueryCondition.CaseSensitive = false
Prompt = "prompt" }
Prompt = "prompt>"
PromptLength = 7 }
|> InternalState.updateConsoleWidth rui.width
|> Pocof.Query.InternalState.prepareNotification

Expand Down Expand Up @@ -300,7 +304,8 @@ module ``Buff writeScreen`` =
PropertySearch = PropertySearch.Search("")
Properties = props
PropertyMap = props |> List.map (fun s -> (s.ToLower(), s)) |> Map
Prompt = "prompt" }
Prompt = "prompt>"
PromptLength = 7 }
|> InternalState.updateConsoleWidth rui.width
|> Pocof.Query.InternalState.prepareNotification

Expand All @@ -325,7 +330,8 @@ module ``Buff writeScreen`` =
InternalState.QueryState.Query = @":unknown"
InternalState.QueryState.Cursor = 8
InternalState.QueryCondition.CaseSensitive = false
Prompt = "prompt" }
Prompt = "prompt>"
PromptLength = 7 }
|> InternalState.updateConsoleWidth rui.width

buff.WriteScreen Layout.TopDown state PSeq.empty <| Error "Property not found"
Expand All @@ -352,7 +358,8 @@ module ``Buff writeScreen`` =
InternalState.QueryState.Query = ""
InternalState.QueryState.Cursor = 0
InternalState.QueryCondition.CaseSensitive = false
Prompt = "prompt" }
Prompt = "prompt>"
PromptLength = 7 }
|> InternalState.updateConsoleWidth rui.width

let entries =
Expand Down Expand Up @@ -386,7 +393,8 @@ module ``Buff writeScreen`` =
InternalState.QueryState.Query = ""
InternalState.QueryState.Cursor = 0
InternalState.QueryCondition.CaseSensitive = false
Prompt = "prompt" }
Prompt = "prompt>"
PromptLength = 7 }
|> InternalState.updateConsoleWidth rui.width

let entries =
Expand Down Expand Up @@ -418,7 +426,7 @@ module ``Buff writeScreen`` =
{ Query = query
Cursor = cursor
WindowBeginningCursor = beginning
WindowWidth = 50 - (state.Prompt |> String.length) - 1
WindowWidth = 50 - (state.Prompt |> String.length)
InputMode = InputMode.Input }
InternalState.QueryCondition.CaseSensitive = false
ConsoleWidth = rui.width }
Expand Down Expand Up @@ -554,7 +562,7 @@ module ``Buff writeScreen`` =
{ Query = query
Cursor = cursor
WindowBeginningCursor = beginning
WindowWidth = 50 - (state.Prompt |> String.length) - 1
WindowWidth = 50 - (state.Prompt |> String.length)
InputMode = inputMode }
InternalState.QueryCondition.CaseSensitive = false
ConsoleWidth = rui.width }
Expand Down
73 changes: 44 additions & 29 deletions src/pocof/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ module Data =
Properties: Generic.IReadOnlyCollection<string>
PropertyMap: Generic.IReadOnlyDictionary<string, string>
Prompt: string
PromptLength: int
WordDelimiters: string
ConsoleWidth: int
Refresh: Refresh }
Expand All @@ -449,14 +450,11 @@ module Data =
[<Literal>]
let private anchor = ">"

// TODO: it should be a property.
let prompt (state: InternalState) = $"%s{state.Prompt}%s{anchor}"

let queryInfo (state: InternalState) (count: int) =
$" %O{state.QueryCondition} [%d{count}]"

let getX (state: InternalState) =
(prompt state |> String.length) + state.QueryState.Cursor
state.PromptLength + state.QueryState.Cursor
- state.QueryState.WindowBeginningCursor

let updateQueryState (qs: QueryState) (state: InternalState) =
Expand Down Expand Up @@ -501,7 +499,34 @@ module Data =
let updateConsoleWidth (consoleWidth: int) (state: InternalState) =
{ state with
ConsoleWidth = consoleWidth
InternalState.QueryState.WindowWidth = prompt state |> String.length |> (+) 1 |> (-) consoleWidth }
InternalState.QueryState.WindowWidth = state.PromptLength |> (+) 1 |> (-) consoleWidth }

let create
(queryState: QueryState)
(queryCondition: QueryCondition)
(suppressProperties: bool)
(properties: Generic.IReadOnlyCollection<string>)
(propertyMap: Generic.IReadOnlyDictionary<string, string>)
(prompt: string)
(wordDelimiters: string)
(consoleWidth: int)
=

let prompt = prompt + anchor

{ QueryState = queryState
QueryCondition = queryCondition
PropertySearch = QueryState.getCurrentProperty queryState
Notification = None
SuppressProperties = suppressProperties
Properties = properties
PropertyMap = propertyMap
Prompt = prompt
PromptLength = prompt |> String.length
WordDelimiters = wordDelimiters
ConsoleWidth = 0 // NOTE: adjust later.
Refresh = Refresh.Required }
|> updateConsoleWidth consoleWidth

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

Expand All @@ -525,33 +550,23 @@ module Data =
ConsoleHeight: int }

let initConfig (p: IncomingParameters) =
let qs =
{ Layout = Layout.fromString p.Layout
Keymaps = p.Keymaps
NotInteractive = p.NotInteractive },
InternalState.create
{ Query = p.Query
Cursor = String.length p.Query
WindowBeginningCursor = 0 // NOTE: adjust later.
WindowWidth = 0 // NOTE: adjust later.
InputMode = InputMode.Input }

let s =
{ QueryState = qs
QueryCondition =
{ Matcher = Matcher.fromString p.Matcher
Operator = Operator.fromString p.Operator
CaseSensitive = p.CaseSensitive
Invert = p.InvertQuery }
PropertySearch = QueryState.getCurrentProperty qs
Notification = None
SuppressProperties = p.SuppressProperties
Properties = p.Properties
PropertyMap = p.PropertiesMap
Prompt = p.Prompt
WordDelimiters = p.WordDelimiters
ConsoleWidth = 0 // NOTE: adjust later.
Refresh = Refresh.Required }
|> InternalState.updateConsoleWidth p.ConsoleWidth

{ Layout = Layout.fromString p.Layout
Keymaps = p.Keymaps
NotInteractive = p.NotInteractive },
s,
{ Matcher = Matcher.fromString p.Matcher
Operator = Operator.fromString p.Operator
CaseSensitive = p.CaseSensitive
Invert = p.InvertQuery }
p.SuppressProperties
p.Properties
p.PropertiesMap
p.Prompt
p.WordDelimiters
p.ConsoleWidth,
{ Y = 0; Height = p.ConsoleHeight }
2 changes: 1 addition & 1 deletion src/pocof/Screen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ module Screen =
getQuery state.QueryState.WindowWidth q <| String.length q
|> selectRange state.QueryState

Data.InternalState.prompt state + q
state.Prompt + q

let getInformationString (state: Data.InternalState) (props: Result<string list, string>) (count: int) =
match state.Notification, props with
Expand Down

0 comments on commit eada57f

Please sign in to comment.