@rbxts/centurion-ui 2.0.0-rc.6 → 2.0.0-rc.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/centurion.luau CHANGED
@@ -1,10 +1,13 @@
1
- local centurion = require(script.Parent.Parent.centurion.src)
1
+ local centurion = require("../centurion/src")
2
2
 
3
3
  export type Argument = centurion.Argument
4
4
  export type ArgumentRange = centurion.ArgumentRange
5
5
  export type Command<T...> = centurion.Command<T...>
6
6
  export type Response = centurion.Response
7
7
  export type Result<T = unknown> = centurion.Result<T>
8
+ export type Suggestion = centurion.Suggestion
9
+ export type SuggestionData = centurion.SuggestionData
10
+ export type SuggestionList = centurion.SuggestionList
8
11
  export type Token = centurion.Token
9
12
 
10
13
  return centurion
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rbxts/centurion-ui",
3
3
  "description": "Terminal UI for the Centurion command framework",
4
- "version": "2.0.0-rc.6",
4
+ "version": "2.0.0-rc.7",
5
5
  "author": "paradoxuum",
6
6
  "license": "MIT",
7
7
  "main": "src/ts/init.luau",
@@ -33,7 +33,7 @@
33
33
  "access": "public"
34
34
  },
35
35
  "dependencies": {
36
- "@rbxts/centurion": "^2.0.0-rc.3"
36
+ "@rbxts/centurion": "^2.0.0-rc.4"
37
37
  },
38
38
  "scripts": {}
39
39
  }
@@ -1,18 +1,17 @@
1
1
  --!strict
2
- local Players = game:GetService("Players")
3
-
4
2
  local centurion = require("../../centurion")
5
3
  local rich = require("../rich")
6
4
  local types = require("../types")
7
5
 
8
6
  type Kind = "command" | "argument" | "string" | "invalid"
9
7
 
10
- local function is_valid(raw: string, arg: centurion.Argument): boolean
11
- local ok, result = pcall(centurion.transform_arg, Players.LocalPlayer, arg, raw)
8
+ local function is_valid(executor: Player, raw: string, arg: centurion.Argument): boolean
9
+ -- this runs while vide updates, which breaks if a transform yields
10
+ local ok, result = pcall(centurion.check_arg, executor, arg, raw)
12
11
  return ok and result.success
13
12
  end
14
13
 
15
- local function classify(text: string): { { span: vector, kind: Kind } }
14
+ local function classify(executor: Player, text: string): { { span: vector, kind: Kind } }
16
15
  local resolution = centurion.resolve_command(text)
17
16
  local tokens = resolution.tokens
18
17
  local cmds = centurion.registry.commands
@@ -45,7 +44,7 @@ local function classify(text: string): { { span: vector, kind: Kind } }
45
44
  local token = tokens[cmd_index + j]
46
45
  local raw = text:sub(token.span.x, token.span.y)
47
46
 
48
- if not is_valid(raw, arg) then
47
+ if not is_valid(executor, raw, arg) then
49
48
  add(token, "invalid")
50
49
  elseif raw:match("^[\"']") then
51
50
  add(token, "string")
@@ -68,7 +67,7 @@ local function classify(text: string): { { span: vector, kind: Kind } }
68
67
  return highlights
69
68
  end
70
69
 
71
- local function highlight(text: string, theme: types.Theme): string
70
+ local function highlight(executor: Player, text: string, theme: types.Theme): string
72
71
  local colors: { [Kind]: Color3 } = {
73
72
  command = theme.highlight,
74
73
  argument = theme.text,
@@ -78,7 +77,7 @@ local function highlight(text: string, theme: types.Theme): string
78
77
 
79
78
  local out = {}
80
79
  local pos = 1
81
- for _, item in classify(text) do
80
+ for _, item in classify(executor, text) do
82
81
  table.insert(out, rich.escape(text:sub(pos, item.span.x - 1)))
83
82
  table.insert(out, rich.color(rich.escape(text:sub(item.span.x, item.span.y)), colors[item.kind]))
84
83
  pos = item.span.y + 1
@@ -1,13 +1,34 @@
1
1
  --!strict
2
- local Players = game:GetService("Players")
3
-
4
2
  local centurion = require("../centurion")
5
3
  local cursor = require("@self/cursor")
6
4
  local match = require("@self/match")
7
5
  local suggest = require("@self/suggest")
8
6
  local types = require("./types")
9
7
 
10
- local function analyse(text: string, cursor_pos: number, max_suggestions: number?): types.AnalysisResult
8
+ -- a string is shorthand for a suggestion that's only found by its text
9
+ local function normalize(suggestion: centurion.Suggestion): centurion.SuggestionData
10
+ if type(suggestion) == "string" then
11
+ return { text = suggestion }
12
+ end
13
+ return suggestion
14
+ end
15
+
16
+ local function suggestion_keys(suggestion: centurion.SuggestionData): { string }
17
+ local keys = { suggestion.text }
18
+ if suggestion.aliases then
19
+ for _, alias in suggestion.aliases do
20
+ table.insert(keys, alias)
21
+ end
22
+ end
23
+ return keys
24
+ end
25
+
26
+ local function analyse(
27
+ executor: Player,
28
+ text: string,
29
+ cursor_pos: number,
30
+ max_suggestions: number?
31
+ ): types.AnalysisResult
11
32
  local resolution = centurion.resolve_command(text)
12
33
  local tokens = resolution.tokens
13
34
 
@@ -62,12 +83,12 @@ local function analyse(text: string, cursor_pos: number, max_suggestions: number
62
83
  end
63
84
 
64
85
  return {
65
- result = centurion.transform_args(Players.LocalPlayer, arg_text, cmd_args),
86
+ result = centurion.check_args(executor, arg_text, cmd_args),
66
87
  suggestions = {},
67
88
  }
68
89
  end
69
90
 
70
- local transform_result = centurion.transform_args(Players.LocalPlayer, arg_text, cmd_args)
91
+ local transform_result = centurion.check_args(executor, arg_text, cmd_args)
71
92
 
72
93
  if not at_arg then
73
94
  return {
@@ -143,35 +164,37 @@ local function analyse(text: string, cursor_pos: number, max_suggestions: number
143
164
  end
144
165
  end
145
166
 
146
- local arg_suggestions: { string } = {}
147
- if arg_type.suggestions then
148
- arg_suggestions = table.clone(arg_type.suggestions(input_token.text, Players.LocalPlayer))
149
- end
167
+ -- the type's suggestions come before the argument's own, and the first
168
+ -- suggestion with a given text wins
169
+ local arg_suggestions: { centurion.SuggestionData } = {}
170
+ local seen: { [string]: true } = {}
171
+ local function add(list: centurion.SuggestionList?)
172
+ if not list then
173
+ return
174
+ end
150
175
 
151
- if arg.suggestions then
152
- for _, suggestion in arg.suggestions do
153
- table.insert(arg_suggestions, suggestion)
176
+ for _, item in list do
177
+ local suggestion = normalize(item)
178
+ if not seen[suggestion.text] then
179
+ seen[suggestion.text] = true
180
+ table.insert(arg_suggestions, suggestion)
181
+ end
154
182
  end
155
183
  end
156
184
 
157
- -- check for duplicates
158
- local seen = {}
159
- for i = #arg_suggestions, 1, -1 do
160
- local suggestion = arg_suggestions[i]
161
- if seen[suggestion] then
162
- table.remove(arg_suggestions, i)
163
- else
164
- seen[suggestion] = true
165
- end
185
+ if arg_type.suggestions then
186
+ add(arg_type.suggestions(input_token.text, executor, arg))
166
187
  end
188
+ add(arg.suggestions)
167
189
 
168
190
  local suggestions = {}
169
191
  local hidden = 0
170
192
  if #arg_suggestions > 0 then
171
- local matches, total = suggest(input_token.text, arg_suggestions, max_suggestions)
193
+ local matches, total = suggest(input_token.text, arg_suggestions, max_suggestions, suggestion_keys)
172
194
  hidden = total - #matches
173
195
 
174
- for _, suggestion_text in matches do
196
+ for _, item in matches do
197
+ local suggestion_text = item.text
175
198
  local replacement = ""
176
199
  local replacement_text = {}
177
200
  for _, replacement_token in centurion.parse_text(suggestion_text, " ").tokens do
@@ -187,6 +210,7 @@ local function analyse(text: string, cursor_pos: number, max_suggestions: number
187
210
  local suggestion = {
188
211
  kind = "argument",
189
212
  name = suggestion_text,
213
+ description = item.description,
190
214
  matches = match(input_token.text, suggestion_text),
191
215
 
192
216
  span = input_token.span,
@@ -209,7 +233,7 @@ local function analyse(text: string, cursor_pos: number, max_suggestions: number
209
233
  end
210
234
 
211
235
  local function process_command(): types.AnalysisResult
212
- local to_match = {}
236
+ local to_match: { string } = {}
213
237
  for k, _ in cmds do
214
238
  if k == cmd_name then
215
239
  continue
@@ -40,8 +40,28 @@ local function subsequence_bonus(query: string, candidate: string): number
40
40
  return best
41
41
  end
42
42
 
43
- -- returns the best matches, and how many there were before being limited
44
- local function suggest(input: string, candidates: { string }, max_suggestions: number?): ({ string }, number)
43
+ local function score(normalized: string, key: string): number
44
+ if key:sub(1, #normalized):lower() == normalized then
45
+ return 1000 - (#key - #normalized)
46
+ end
47
+
48
+ local distance = leventine.score(normalized, key, false)
49
+ return (1 - distance / math.max(#normalized, #key)) * 100 + subsequence_bonus(normalized, key:lower())
50
+ end
51
+
52
+ local function identity(candidate: any): { string }
53
+ return { candidate }
54
+ end
55
+
56
+ -- returns the best matches, and how many there were before being limited.
57
+ -- `keys` gives the strings a candidate can be found by, and its best scoring
58
+ -- key ranks it. by default a candidate is its own only key
59
+ local function suggest<T>(
60
+ input: string,
61
+ candidates: { T },
62
+ max_suggestions: number?,
63
+ keys: ((candidate: T) -> { string })?
64
+ ): ({ T }, number)
45
65
  if #input == 0 then
46
66
  if max_suggestions and #candidates > max_suggestions then
47
67
  return { unpack(candidates, 1, max_suggestions) }, #candidates
@@ -49,21 +69,17 @@ local function suggest(input: string, candidates: { string }, max_suggestions: n
49
69
  return candidates, #candidates
50
70
  end
51
71
 
72
+ local get_keys = keys or identity
52
73
  local normalized = input:lower()
53
74
  local scored = {}
54
75
  for _, candidate in candidates do
55
- local score = 0
56
-
57
- if candidate:sub(1, #normalized):lower() == normalized then
58
- score = 1000 - (#candidate - #normalized)
59
- else
60
- score = leventine.score(normalized, candidate, false)
61
- score = (1 - score / math.max(#normalized, #candidate)) * 100
62
- score += subsequence_bonus(normalized, candidate:lower())
76
+ local best = MIN_SCORE
77
+ for _, key in get_keys(candidate) do
78
+ best = math.max(best, score(normalized, key))
63
79
  end
64
80
 
65
- if score > MIN_SCORE then
66
- table.insert(scored, { text = candidate, score = score })
81
+ if best > MIN_SCORE then
82
+ table.insert(scored, { value = candidate, score = best })
67
83
  end
68
84
  end
69
85
 
@@ -73,7 +89,7 @@ local function suggest(input: string, candidates: { string }, max_suggestions: n
73
89
 
74
90
  local mapped = {}
75
91
  for i = 1, math.min(max_suggestions or #scored, #scored) do
76
- table.insert(mapped, scored[i].text)
92
+ table.insert(mapped, scored[i].value)
77
93
  end
78
94
  return mapped, #scored
79
95
  end
package/src/app/init.luau CHANGED
@@ -288,7 +288,7 @@ local function app()
288
288
  untrack(function()
289
289
  local input_text = raw_text()
290
290
  if #input_text > 0 then
291
- analysis(analyse(input_text, #input_text + 1, state.max_suggestions()))
291
+ analysis(analyse(Players.LocalPlayer, input_text, #input_text + 1, state.max_suggestions()))
292
292
  end
293
293
  end)
294
294
  end)
@@ -335,7 +335,7 @@ local function app()
335
335
  pos = #raw_text() + 1
336
336
  end
337
337
 
338
- analysis(analyse(raw_text(), pos, state.max_suggestions()))
338
+ analysis(analyse(Players.LocalPlayer, raw_text(), pos, state.max_suggestions()))
339
339
  end)
340
340
  end)
341
341
 
@@ -622,7 +622,7 @@ local function app()
622
622
  text {
623
623
  size = UDim2.fromScale(1, 1),
624
624
  text = function()
625
- return if touch() then "" else highlight(raw_text(), state.theme())
625
+ return if touch() then "" else highlight(Players.LocalPlayer, raw_text(), state.theme())
626
626
  end,
627
627
  rich_text = true,
628
628
  xalignment = Enum.TextXAlignment.Left,
@@ -659,8 +659,11 @@ local function app()
659
659
  end,
660
660
 
661
661
  focused = state.focused,
662
- update_focused = function(focused)
663
- if focused then
662
+ update_focused = function(focused, enter)
663
+ if focused or enter then
664
+ -- pressing enter right after selecting an entry shouldn't refocus
665
+ -- straight away, since capturing focus while enter is still being
666
+ -- handled leaves whitespace in the textbox
664
667
  refocus_deadline = 0
665
668
  elseif os.clock() < refocus_deadline then
666
669
  -- the tap that selected an entry released the textbox, so take it back
@@ -39,7 +39,7 @@ type TextBoxProps = {
39
39
  read bind_open_keyboard: ((open: () -> ()) -> ())?,
40
40
 
41
41
  read update_text: ((string) -> ())?,
42
- read update_focused: ((boolean) -> ())?,
42
+ read update_focused: ((focused: boolean, enter: boolean) -> ())?,
43
43
  read enter: (string) -> (),
44
44
 
45
45
  [number]: unknown,
@@ -96,7 +96,7 @@ return function(props: TextBoxProps)
96
96
  if not props.update_focused then
97
97
  return
98
98
  end
99
- props.update_focused(true)
99
+ props.update_focused(true, false)
100
100
  end,
101
101
 
102
102
  FocusLost = function(enter)
@@ -104,7 +104,7 @@ return function(props: TextBoxProps)
104
104
  return
105
105
  end
106
106
  if props.update_focused then
107
- props.update_focused(false)
107
+ props.update_focused(false, enter)
108
108
  end
109
109
  if not enter then
110
110
  return
package/src/init.luau CHANGED
@@ -16,7 +16,9 @@ local function mount(keys: { Enum.KeyCode }?)
16
16
  end
17
17
  mounted = true
18
18
 
19
- state.activation_keys(keys or { Enum.KeyCode.F2 })
19
+ if keys then
20
+ state.activation_keys(keys)
21
+ end
20
22
 
21
23
  vide.mount(function()
22
24
  px.mount()
package/src/state.luau CHANGED
@@ -10,7 +10,7 @@ return {
10
10
  logs = source({} :: { types.Log }),
11
11
 
12
12
  -- options
13
- activation_keys = source({}),
13
+ activation_keys = source({ Enum.KeyCode.F2 }),
14
14
  hide_on_lost_focus = source(true),
15
15
  theme = source(theme.themes.mocha),
16
16
  font = source(Font.fromId(16658246179)),
package/src/ts/index.d.ts CHANGED
@@ -20,6 +20,7 @@ export interface Options {
20
20
  anchor: Vector2;
21
21
  width: UDim;
22
22
  position: UDim2;
23
+ display_order: number;
23
24
  }
24
25
 
25
26
  export interface Theme {