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

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/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.4",
4
+ "version": "2.0.0-rc.6",
5
5
  "author": "paradoxuum",
6
6
  "license": "MIT",
7
7
  "main": "src/ts/init.luau",
@@ -51,7 +51,7 @@ local function myers_32(a: string, b: string, a_len: number, b_len: number): num
51
51
 
52
52
  -- Set the bits for characters in string a
53
53
  for j, a_byte in a_bytes do
54
- PEQ[a_byte] = OR(PEQ[a_byte], LSHIFT(1, j))
54
+ PEQ[a_byte] = OR(PEQ[a_byte], LSHIFT(1, j - 1))
55
55
  end
56
56
 
57
57
  for _, byte in b_bytes do
@@ -1,6 +1,44 @@
1
1
  local leventine = require("./leventine")
2
2
 
3
3
  local MIN_SCORE = -math.huge
4
+ local SUBSEQUENCE_BONUS = 100
5
+
6
+ local function word_start(candidate: string, pos: number): boolean
7
+ return pos == 1 or string.find(string.sub(candidate, pos - 1, pos - 1), "[%s_%-]") ~= nil
8
+ end
9
+
10
+ -- rewards the query's characters appearing in order in the candidate, by how
11
+ -- tightly they're packed and whether they start a word. a flat bonus would
12
+ -- rank loose matches like "rsp" in "forcespawn" above close typos like "esp"
13
+ local function subsequence_bonus(query: string, candidate: string): number
14
+ local first = string.sub(query, 1, 1)
15
+ local best = 0
16
+
17
+ local start = string.find(candidate, first, 1, true)
18
+ while start do
19
+ local pos = start
20
+ for i = 2, #query do
21
+ local found = string.find(candidate, string.sub(query, i, i), pos + 1, true)
22
+ if not found then
23
+ -- later starts can't match either
24
+ return best
25
+ end
26
+ pos = found
27
+ end
28
+
29
+ local tightness = #query / (pos - start + 1)
30
+ -- abbreviations skip a lot, e.g. "tp" for "teleport", so don't punish gaps too hard
31
+ local bonus = SUBSEQUENCE_BONUS * tightness ^ 1.5
32
+ if not word_start(candidate, start) then
33
+ bonus /= 2
34
+ end
35
+ best = math.max(best, bonus)
36
+
37
+ start = string.find(candidate, first, start + 1, true)
38
+ end
39
+
40
+ return best
41
+ end
4
42
 
5
43
  -- returns the best matches, and how many there were before being limited
6
44
  local function suggest(input: string, candidates: { string }, max_suggestions: number?): ({ string }, number)
@@ -21,9 +59,7 @@ local function suggest(input: string, candidates: { string }, max_suggestions: n
21
59
  else
22
60
  score = leventine.score(normalized, candidate, false)
23
61
  score = (1 - score / math.max(#normalized, #candidate)) * 100
24
- if leventine.has_match(candidate, normalized, false) then
25
- score += 100
26
- end
62
+ score += subsequence_bonus(normalized, candidate:lower())
27
63
  end
28
64
 
29
65
  if score > MIN_SCORE then
@@ -19,6 +19,7 @@ local source = vide.source
19
19
  type Header = {
20
20
  read order: number?,
21
21
  read on_clear: () -> (),
22
+ read on_close: () -> (),
22
23
  }
23
24
 
24
25
  -- KeyCode names that aren't what's printed on the key
@@ -41,7 +42,7 @@ local function key_label(key: Enum.KeyCode): string
41
42
  return key_labels[key.Name] or key.Name
42
43
  end
43
44
 
44
- local function clear_button(props: { order: number, on_clear: () -> () })
45
+ local function text_button(props: { order: number, label: string, on_activated: () -> () })
45
46
  local hovered = source(false)
46
47
 
47
48
  return create "TextButton" {
@@ -51,7 +52,7 @@ local function clear_button(props: { order: number, on_clear: () -> () })
51
52
  LayoutOrder = props.order,
52
53
  Text = "",
53
54
 
54
- Activated = props.on_clear,
55
+ Activated = props.on_activated,
55
56
  MouseEnter = function()
56
57
  hovered(true)
57
58
  end,
@@ -61,7 +62,7 @@ local function clear_button(props: { order: number, on_clear: () -> () })
61
62
 
62
63
  text {
63
64
  auto = "XY",
64
- text = "Clear",
65
+ text = props.label,
65
66
  text_size = px(state.subtext_size),
66
67
  text_style = function()
67
68
  return if hovered() then "highlight" else "subtext"
@@ -85,6 +86,11 @@ local function header(props: Header)
85
86
  return input() == Enum.PreferredInput.KeyboardAndMouse and #state.activation_keys() > 0
86
87
  end
87
88
 
89
+ -- touch devices have no key to close with, so give them a button instead
90
+ local function show_close()
91
+ return input() == Enum.PreferredInput.Touch
92
+ end
93
+
88
94
  return container {
89
95
  size = px(UDim2.new(1, 0, 0, 23)),
90
96
  order = props.order,
@@ -99,7 +105,7 @@ local function header(props: Header)
99
105
  flex():column():vertical("center"):gap(5),
100
106
 
101
107
  image {
102
- image = state.icon,
108
+ image = state.header_icon,
103
109
  size = px(function()
104
110
  return UDim2.fromOffset(state.subtext_size(), state.subtext_size())
105
111
  end),
@@ -107,7 +113,7 @@ local function header(props: Header)
107
113
  return state.theme().highlight
108
114
  end,
109
115
  visible = function()
110
- return state.icon() ~= ""
116
+ return state.header_icon() ~= ""
111
117
  end,
112
118
  order = 1,
113
119
  },
@@ -115,7 +121,7 @@ local function header(props: Header)
115
121
  text {
116
122
  auto = "XY",
117
123
  order = 2,
118
- text = state.title,
124
+ text = state.header_title,
119
125
  text_size = px(state.subtext_size),
120
126
  text_style = "subtext",
121
127
  weight = Enum.FontWeight.Bold,
@@ -130,12 +136,12 @@ local function header(props: Header)
130
136
 
131
137
  -- only shown while there's something to clear
132
138
  show(show_clear, function()
133
- return clear_button { order = 1, on_clear = props.on_clear }
139
+ return text_button { order = 1, label = "Clear", on_activated = props.on_clear }
134
140
  end),
135
141
 
136
- -- vertical rule between the clear button and the close hint
142
+ -- vertical rule between the clear button and the close hint or button
137
143
  show(function()
138
- return show_clear() and show_hint()
144
+ return show_clear() and (show_hint() or show_close())
139
145
  end, function()
140
146
  return background {
141
147
  order = 2,
@@ -188,6 +194,10 @@ local function header(props: Header)
188
194
  },
189
195
  }
190
196
  end),
197
+
198
+ show(show_close, function()
199
+ return text_button { order = 4, label = "Close", on_activated = props.on_close }
200
+ end),
191
201
  },
192
202
  }
193
203
  end
@@ -23,8 +23,12 @@ local spring = vide.spring
23
23
 
24
24
  type History = {
25
25
  read max_lines: number | () -> number,
26
+ -- the most room the history can take up, in pixels, so it fits on screen
27
+ read max_height: (() -> number)?,
26
28
  read on_select: (command: string) -> (),
27
29
  read order: number?,
30
+
31
+ [number]: unknown,
28
32
  }
29
33
 
30
34
  local line_height = 23
@@ -197,7 +201,11 @@ local function history(props: History)
197
201
  if height == 0 then
198
202
  return UDim2.new()
199
203
  end
200
- return UDim2.fromOffset(0, math.min(height, max_height()) + px.convert(top_padding))
204
+ height = math.min(height, max_height()) + px.convert(top_padding)
205
+ if props.max_height then
206
+ height = math.min(height, math.max(props.max_height(), 0))
207
+ end
208
+ return UDim2.fromOffset(0, height)
201
209
  end, 0.1),
202
210
 
203
211
  ScrollBarThickness = px(3),
@@ -257,6 +265,8 @@ local function history(props: History)
257
265
  indexes(state.logs, function(value: () -> types.Log, i: number)
258
266
  return entry(value, i, props.on_select)
259
267
  end),
268
+
269
+ unpack(props),
260
270
  }
261
271
  end
262
272
 
package/src/app/init.luau CHANGED
@@ -1,4 +1,5 @@
1
1
  local ContextActionService = game:GetService("ContextActionService")
2
+ local GuiService = game:GetService("GuiService")
2
3
  local Players = game:GetService("Players")
3
4
  local RunService = game:GetService("RunService")
4
5
  local UserInputService = game:GetService("UserInputService")
@@ -79,6 +80,22 @@ local function app()
79
80
  keyboard_visible(UserInputService.OnScreenKeyboardVisible)
80
81
  end))
81
82
 
83
+ -- top edge of the on-screen keyboard, in the same space as the console, or
84
+ -- nil when there isn't one taking up the screen
85
+ local function read_keyboard_top(): number?
86
+ if UserInputService.OnScreenKeyboardSize.Y <= 0 then
87
+ return nil
88
+ end
89
+ -- the keyboard is placed on the whole screen, while the console is placed below the topbar
90
+ return UserInputService.OnScreenKeyboardPosition.Y - GuiService:GetGuiInset().Y
91
+ end
92
+ local keyboard_top = source(read_keyboard_top())
93
+ local function update_keyboard_top()
94
+ keyboard_top(read_keyboard_top())
95
+ end
96
+ cleanup(UserInputService:GetPropertyChangedSignal("OnScreenKeyboardPosition"):Connect(update_keyboard_top))
97
+ cleanup(UserInputService:GetPropertyChangedSignal("OnScreenKeyboardSize"):Connect(update_keyboard_top))
98
+
82
99
  local next_cursor_pos = false
83
100
  local refocus_deadline = 0
84
101
  local cmd_history = {}
@@ -457,6 +474,76 @@ local function app()
457
474
  return if has_popover() then 1 else 0
458
475
  end, 0.12)
459
476
 
477
+ -- on short screens the history gives up room first, then the suggestions,
478
+ -- so that neither runs off the bottom of the screen
479
+ local bounds_bottom = source(math.huge)
480
+ local console_top = source(0)
481
+ -- height of the console without its history
482
+ local console_fixed = source(0)
483
+ local popover_height = source(0)
484
+ local frames: { [string]: GuiObject } = {}
485
+
486
+ local function measure()
487
+ local console, history_frame, popover = frames.console, frames.history, frames.popover
488
+ if console and history_frame then
489
+ console_top(console.AbsolutePosition.Y)
490
+ -- read together so both sizes are from the same layout pass
491
+ console_fixed(console.AbsoluteSize.Y - history_frame.AbsoluteSize.Y)
492
+ end
493
+ if popover then
494
+ popover_height(popover.AbsoluteSize.Y)
495
+ end
496
+ end
497
+
498
+ local function measured(name: string)
499
+ return action(function(instance: GuiObject)
500
+ frames[name] = instance
501
+ cleanup(instance:GetPropertyChangedSignal("AbsoluteSize"):Connect(measure))
502
+ cleanup(instance:GetPropertyChangedSignal("AbsolutePosition"):Connect(measure))
503
+ end)
504
+ end
505
+
506
+ -- room for the history and suggestions, between the input and the bottom of the screen
507
+ local function free_space()
508
+ local bottom = bounds_bottom()
509
+ -- shrink out of the way of the on-screen keyboard while it's open. consoles
510
+ -- are left alone, since their keyboard covers the whole screen anyway
511
+ local top_of_keyboard = keyboard_top()
512
+ if touch() and keyboard_visible() and top_of_keyboard then
513
+ bottom = math.min(bottom, top_of_keyboard)
514
+ end
515
+ return bottom - px.convert(8) - console_top() - console_fixed()
516
+ end
517
+
518
+ -- the bottom of whatever the console is drawn in, usually the ScreenGui
519
+ local track_bounds = action(function(instance: GuiObject)
520
+ local connection: RBXScriptConnection? = nil
521
+
522
+ local function update_parent()
523
+ if connection then
524
+ connection:Disconnect()
525
+ connection = nil
526
+ end
527
+
528
+ local parent = instance.Parent
529
+ if parent and parent:IsA("GuiBase2d") then
530
+ local function update()
531
+ bounds_bottom(parent.AbsolutePosition.Y + parent.AbsoluteSize.Y)
532
+ end
533
+ connection = parent:GetPropertyChangedSignal("AbsoluteSize"):Connect(update)
534
+ update()
535
+ end
536
+ end
537
+
538
+ cleanup(instance:GetPropertyChangedSignal("Parent"):Connect(update_parent))
539
+ cleanup(function()
540
+ if connection then
541
+ connection:Disconnect()
542
+ end
543
+ end)
544
+ update_parent()
545
+ end)
546
+
460
547
  -- created inside the console so that it fades with it
461
548
  local function input_field()
462
549
  return background {
@@ -621,13 +708,17 @@ local function app()
621
708
  local offset = px.convert((reveal() - 1) * 8)
622
709
  return px.get(state.position) + UDim2.fromOffset(0, offset)
623
710
  end,
624
- size = px(function()
711
+ size = function()
625
712
  local w = state.width()
626
- return UDim2.new(w.Scale, 1000, 0, 0)
627
- end),
713
+ local viewport = px.viewport().X
714
+ -- never wider than the screen, leaving a margin on either side
715
+ local max_offset = viewport * (1 - w.Scale) - px.convert(16) * 2
716
+ return UDim2.new(w.Scale, math.min(px.convert(w.Offset), max_offset), 0, 0)
717
+ end,
628
718
 
629
719
  padding(px(8)),
630
720
  flex():row():gap(1),
721
+ track_bounds,
631
722
 
632
723
  {
633
724
  Visible = function()
@@ -653,6 +744,7 @@ local function app()
653
744
  },
654
745
  flex():row():fill("horizontal"),
655
746
  catch_hover(),
747
+ measured("console"),
656
748
 
657
749
  header {
658
750
  order = 1,
@@ -660,12 +752,20 @@ local function app()
660
752
  state.logs({})
661
753
  refocus()
662
754
  end,
755
+ on_close = function()
756
+ state.opened(false)
757
+ state.focused(false)
758
+ end,
663
759
  },
664
760
  divider { order = 2 },
665
761
 
666
762
  history {
667
763
  order = 3,
668
764
  max_lines = max_history_lines,
765
+ -- the suggestions take priority, since they're what's being typed into
766
+ max_height = function()
767
+ return free_space() - (if has_popover() then popover_height() else 0)
768
+ end,
669
769
  on_select = function(command)
670
770
  cmd_history_index = 0
671
771
  raw_text(command)
@@ -673,6 +773,8 @@ local function app()
673
773
  -- after the textbox has recaptured focus, move the cursor to the end
674
774
  task.defer(cursor_pos, #command + 1)
675
775
  end,
776
+
777
+ measured("history"),
676
778
  },
677
779
 
678
780
  container {
@@ -706,10 +808,14 @@ local function app()
706
808
  return pop() > 0.01
707
809
  end,
708
810
  },
811
+ measured("popover"),
709
812
 
710
813
  suggestions {
711
814
  analysis = display_analysis,
712
815
  selected_suggestion = selected_suggestion,
816
+ max_height = function()
817
+ return free_space() - px.convert(4)
818
+ end,
713
819
  on_select = function(suggestion: types.AnalysisSuggestion)
714
820
  raw_text(autofill(raw_text(), suggestion, true))
715
821
  if not gamepad() then
@@ -33,6 +33,8 @@ export type Suggestions = {
33
33
  read analysis: () -> types.AnalysisResult,
34
34
  read selected_suggestion: () -> number,
35
35
  read on_select: (suggestion: types.AnalysisSuggestion) -> (),
36
+ -- the most room the popover can take up, in pixels, so it fits on screen
37
+ read max_height: (() -> number)?,
36
38
  read order: number?,
37
39
 
38
40
  [number]: unknown,
@@ -252,6 +254,19 @@ local function suggestions(props: Suggestions)
252
254
  -- set by the list, which is also at the bottom when it's too short to scroll
253
255
  local scrolled_to_bottom = source(true)
254
256
 
257
+ -- height of everything in the popover besides the list, which is what the
258
+ -- list has to leave room for when it's limited by max_height
259
+ local root: GuiObject? = nil
260
+ local list: GuiObject? = nil
261
+ local chrome_height = source(0)
262
+
263
+ local function measure()
264
+ if root and list then
265
+ -- read together so both sizes are from the same layout pass
266
+ chrome_height(root.AbsoluteSize.Y - list.AbsoluteSize.Y)
267
+ end
268
+ end
269
+
255
270
  return background {
256
271
  auto = "XY",
257
272
  order = props.order,
@@ -272,6 +287,10 @@ local function suggestions(props: Suggestions)
272
287
  end,
273
288
  },
274
289
  flex():row():fill("horizontal"),
290
+ action(function(instance: Frame)
291
+ root = instance
292
+ cleanup(instance:GetPropertyChangedSignal("AbsoluteSize"):Connect(measure))
293
+ end),
275
294
 
276
295
  -- why the input can't be run as it is
277
296
  show(failed, function()
@@ -406,7 +425,15 @@ local function suggestions(props: Suggestions)
406
425
  end
407
426
 
408
427
  local function visible_height()
409
- return math.min(content_height(), row_height() * state.visible_suggestions())
428
+ local height = math.min(content_height(), row_height() * state.visible_suggestions())
429
+ local row = row_height()
430
+ if props.max_height == nil or row == 0 then
431
+ return height
432
+ end
433
+
434
+ -- only whole rows, around the list's padding, but always at least one
435
+ local room = props.max_height() - chrome_height() - px.convert(6)
436
+ return math.min(height, math.max(math.floor(room / row), 1) * row)
410
437
  end
411
438
 
412
439
  effect(function()
@@ -480,6 +507,13 @@ local function suggestions(props: Suggestions)
480
507
  changed("CanvasPosition", function(position: Vector2)
481
508
  canvas_y(position.Y)
482
509
  end),
510
+ action(function(instance: ScrollingFrame)
511
+ list = instance
512
+ cleanup(instance:GetPropertyChangedSignal("AbsoluteSize"):Connect(measure))
513
+ cleanup(function()
514
+ list = nil
515
+ end)
516
+ end),
483
517
 
484
518
  padding(px(3)),
485
519
 
package/src/init.luau CHANGED
@@ -42,7 +42,6 @@ local function configure(options: types.OptionsPartial)
42
42
  set_opt(state.hide_on_lost_focus, options.hide_on_lost_focus)
43
43
  set_opt(state.activation_keys, options.activation_keys)
44
44
  set_opt(state.theme, options.theme)
45
- set_opt(state.icon, options.icon)
46
45
  set_opt(state.font, options.font)
47
46
  set_opt(state.transparency, options.transparency)
48
47
 
@@ -56,8 +55,11 @@ local function configure(options: types.OptionsPartial)
56
55
 
57
56
  set_opt(state.visible_suggestions, options.visible_suggestions)
58
57
  set_opt(state.max_suggestions, options.max_suggestions)
59
- set_opt(state.clear_button, options.clear_button)
60
58
  set_opt(state.format_date, options.format_date)
59
+
60
+ set_opt(state.header_title, options.header_title)
61
+ set_opt(state.header_icon, options.header_icon)
62
+ set_opt(state.clear_button, options.clear_button)
61
63
  end
62
64
 
63
65
  local function clear()
package/src/px.luau CHANGED
@@ -12,6 +12,7 @@ const MIN_SCALE = 0.5
12
12
  local DOMINANT_AXIS = 0.5
13
13
 
14
14
  local scale = source(1)
15
+ local viewport_size = source(Vector2.new(0, 0))
15
16
 
16
17
  local function modify_scale(scale_value: number): number
17
18
  return math.max(scale_value, MIN_SCALE)
@@ -52,8 +53,6 @@ local function px<T>(value: T | () -> T, mode: Mode? | () -> Mode?): () -> T
52
53
  end
53
54
 
54
55
  local function mount()
55
- local viewport_size = source(Vector2.new(0, 0))
56
-
57
56
  effect(function()
58
57
  local viewport = viewport_size()
59
58
  local scaleX = math.log(viewport.X / BASE_RESOLUTION.X, 2)
@@ -69,7 +68,7 @@ local function mount()
69
68
  viewport_size(workspace.CurrentCamera.ViewportSize)
70
69
  end
71
70
 
72
- return setmetatable({ mount = mount, convert = convert, get = px_raw }, {
71
+ return setmetatable({ mount = mount, convert = convert, get = px_raw, viewport = viewport_size }, {
73
72
  __call = function<T>(_, value: T | () -> T, mode: Mode? | () -> Mode?): () -> T
74
73
  return px(value, mode)
75
74
  end,
package/src/state.luau CHANGED
@@ -14,9 +14,7 @@ return {
14
14
  hide_on_lost_focus = source(true),
15
15
  theme = source(theme.themes.mocha),
16
16
  font = source(Font.fromId(16658246179)),
17
- title = source("Centurion"),
18
- icon = source("rbxassetid://115292290730914"),
19
- transparency = source(0.2),
17
+ transparency = source(0.1),
20
18
  format_date = source(function(log: types.Log): string
21
19
  local dateTime = DateTime.fromUnixTimestamp(log.response.timestamp):FormatLocalTime("LT", "en-us")
22
20
  local dateParts = dateTime:split(" ")
@@ -24,6 +22,9 @@ return {
24
22
  end),
25
23
  visible_suggestions = source(5),
26
24
  max_suggestions = source(50),
25
+
26
+ header_title = source("Centurion"),
27
+ header_icon = source("rbxassetid://115292290730914"),
27
28
  clear_button = source(false),
28
29
 
29
30
  text_size = source(19),
@@ -31,6 +32,6 @@ return {
31
32
 
32
33
  anchor = source(Vector2.new(0.5, 0)),
33
34
  position = source(UDim2.new(0.5, 0, 0, 0)),
34
- width = source(UDim.new(0, 1200)),
35
+ width = source(UDim.new(0, 1000)),
35
36
  display_order = source(1000),
36
37
  }
package/src/ts/index.d.ts CHANGED
@@ -5,11 +5,13 @@ export interface Options {
5
5
  activation_keys: Enum.KeyCode[];
6
6
  theme: Theme;
7
7
  font: Font;
8
- icon: string;
9
8
  transparency: number;
10
9
  format_date: (log: Log) => string;
11
10
  visible_suggestions: number;
12
11
  max_suggestions: number;
12
+
13
+ header_title: string;
14
+ header_icon: string;
13
15
  clear_button: boolean;
14
16
 
15
17
  text_size: number;
package/src/types.luau CHANGED
@@ -10,11 +10,13 @@ export type Options = {
10
10
  activation_keys: { Enum.KeyCode },
11
11
  theme: Theme,
12
12
  font: Font,
13
- icon: string,
14
13
  transparency: number,
15
14
  format_date: (log: Log) -> string,
16
15
  visible_suggestions: number,
17
16
  max_suggestions: number,
17
+
18
+ header_title: string,
19
+ header_icon: string,
18
20
  clear_button: boolean,
19
21
 
20
22
  text_size: number,
@@ -31,11 +33,13 @@ export type OptionsPartial = {
31
33
  activation_keys: { Enum.KeyCode }?,
32
34
  theme: Theme?,
33
35
  font: Font?,
34
- icon: string?,
35
36
  transparency: number?,
36
37
  format_date: ((log: Log) -> string)?,
37
38
  visible_suggestions: number?,
38
39
  max_suggestions: number?,
40
+
41
+ header_title: string?,
42
+ header_icon: string?,
39
43
  clear_button: boolean?,
40
44
 
41
45
  text_size: number?,