cli-ui 1.5.1 → 2.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ # typed: true
2
+
1
3
  require('cli/ui')
2
4
 
3
5
  module CLI
@@ -16,57 +18,74 @@ module CLI
16
18
  # CLI::UI::Widgets.register('my-widget') { MyWidget }
17
19
  # puts(CLI::UI.fmt("{{@widget/my-widget:args}}"))
18
20
  module Widgets
21
+ extend T::Sig
22
+
19
23
  MAP = {}
20
24
 
21
25
  autoload(:Base, 'cli/ui/widgets/base')
26
+ autoload(:Status, 'cli/ui/widgets/status')
22
27
 
23
- def self.register(name, &cb)
24
- MAP[name] = cb
25
- end
28
+ class << self
29
+ extend T::Sig
26
30
 
27
- autoload(:Status, 'cli/ui/widgets/status')
28
- register('status') { Widgets::Status }
31
+ sig { params(name: String, cb: T.proc.returns(T.class_of(Widgets::Base))).void }
32
+ def register(name, &cb)
33
+ MAP[name] = cb
34
+ end
29
35
 
30
- # Looks up a widget by handle
31
- #
32
- # ==== Raises
33
- # Raises InvalidWidgetHandle if the widget is not available.
34
- #
35
- # ==== Returns
36
- # A callable widget, to be invoked like `.call(argstring)`
37
- #
38
- def self.lookup(handle)
39
- MAP.fetch(handle.to_s).call
40
- rescue KeyError, NameError
41
- raise(InvalidWidgetHandle, handle)
42
- end
36
+ # Looks up a widget by handle
37
+ #
38
+ # ==== Raises
39
+ # Raises InvalidWidgetHandle if the widget is not available.
40
+ #
41
+ # ==== Returns
42
+ # A callable widget, to be invoked like `.call(argstring)`
43
+ #
44
+ sig { params(handle: String).returns(T.class_of(Widgets::Base)) }
45
+ def lookup(handle)
46
+ MAP.fetch(handle).call
47
+ rescue KeyError, NameError
48
+ raise(InvalidWidgetHandle, handle)
49
+ end
43
50
 
44
- # All available widgets by name
45
- #
46
- def self.available
47
- MAP.keys
51
+ # All available widgets by name
52
+ #
53
+ sig { returns(T::Array[String]) }
54
+ def available
55
+ MAP.keys
56
+ end
48
57
  end
49
58
 
59
+ register('status') { Widgets::Status }
60
+
50
61
  class InvalidWidgetHandle < ArgumentError
62
+ extend T::Sig
63
+
64
+ sig { params(handle: String).void }
51
65
  def initialize(handle)
52
66
  super
53
67
  @handle = handle
54
68
  end
55
69
 
70
+ sig { returns(String) }
56
71
  def message
57
- keys = Widget.available.join(',')
72
+ keys = Widgets.available.join(',')
58
73
  "invalid widget handle: #{@handle} " \
59
74
  "-- must be one of CLI::UI::Widgets.available (#{keys})"
60
75
  end
61
76
  end
62
77
 
63
78
  class InvalidWidgetArguments < ArgumentError
79
+ extend T::Sig
80
+
81
+ sig { params(argstring: String, pattern: Regexp).void }
64
82
  def initialize(argstring, pattern)
65
83
  super
66
84
  @argstring = argstring
67
85
  @pattern = pattern
68
86
  end
69
87
 
88
+ sig { returns(String) }
70
89
  def message
71
90
  "invalid widget arguments: #{@argstring} " \
72
91
  "-- must match pattern: #{@pattern.inspect}"
data/lib/cli/ui/wrap.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  # coding: utf-8
2
+
3
+ # typed: true
4
+
2
5
  require 'cli/ui'
3
6
  require 'cli/ui/frame/frame_stack'
4
7
  require 'cli/ui/frame/frame_style'
@@ -6,13 +9,17 @@ require 'cli/ui/frame/frame_style'
6
9
  module CLI
7
10
  module UI
8
11
  class Wrap
12
+ extend T::Sig
13
+
14
+ sig { params(input: String).void }
9
15
  def initialize(input)
10
16
  @input = input
11
17
  end
12
18
 
19
+ sig { returns(String) }
13
20
  def wrap
14
21
  max_width = Terminal.width - Frame.prefix_width
15
- width = 0
22
+ width = T.let(0, Integer)
16
23
  final = []
17
24
  # Create an alternation of format codes of parameter lengths 1-20, since + and {1,n} not allowed in lookbehind
18
25
  format_codes = (1..20).map { |n| /\x1b\[[\d;]{#{n}}m/ }.join('|')