dev-ui 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 209b2569ba461e867bbce33d15b701106477886a
4
- data.tar.gz: 02d3b565537c45640763f25f30728df1fee6466e
3
+ metadata.gz: '0088aa409f65b6130d130f404bb71662a06a6f3d'
4
+ data.tar.gz: a1a7e7da5c2b8534882f67b22953d4fe886f4d4b
5
5
  SHA512:
6
- metadata.gz: a23cb0cb24241e40f466889c16ddd8fe26fe7191e7d93151a51a9a696f7e6e26592a8f37a8eb2f9b6ca169d844ab9bd5b39116e14ae997c6afad5b328e48b76f
7
- data.tar.gz: ecb5fb7a4a7e45b8d6648edab5c7f38d60b2c53730f145ab4002081d9396efd9cbff941d54fb02ea58a74ffab3834b9a5c95ee8df648df3b3a8f7776dfae663d
6
+ metadata.gz: d8dc9c83e8145df6ba606ad805b9c2fd1444c372ad00cbd34e534878ba1a3f0180d00488371cac76706f22bd4d8335e1dc51a3b144fc00e08496ecd07eac471e
7
+ data.tar.gz: b89f0a81a0840ed0ffde9b9f118ae4d9552b0ac4fde6505158a63ecd2bddcb4fdc8a1a74db78ffd9a7a0a27acc970cc68321905a1cfa89135299d1ad2883e2f0
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  ```ruby
2
2
  require 'dev/ui'
3
3
 
4
+ Dev::UI::StdoutRouter.enable
5
+
4
6
  Dev::UI::Frame.open('{{*}} {{bold:a}}', color: :green) do
5
7
  Dev::UI::Frame.open('{{i}} b', color: :magenta) do
6
8
  Dev::UI::Frame.open('{{?}} c', color: :cyan) do
@@ -67,9 +67,10 @@ module Dev
67
67
  Dev::UI::StdoutRouter.duplicate_output_to = File.open(path, 'w')
68
68
  yield
69
69
  ensure
70
- f = Dev::UI::StdoutRouter.duplicate_output_to
71
- f.close
72
- Dev::UI::StdoutRouter.duplicate_output_to = nil
70
+ if file_descriptor = Dev::UI::StdoutRouter.duplicate_output_to
71
+ file_descriptor.close
72
+ Dev::UI::StdoutRouter.duplicate_output_to = nil
73
+ end
73
74
  end
74
75
 
75
76
  def self.raw
@@ -7,8 +7,24 @@ module Dev
7
7
 
8
8
  # ANSI escape sequences (like \x1b[31m) have zero width.
9
9
  # when calculating the padding width, we must exclude them.
10
+ # This also implements a *reaaaallly* shitty version of utf8 character
11
+ # width calculation like we could get for real from something like
12
+ # utf8proc.
10
13
  def self.printing_width(str)
11
- strip_codes(str).size
14
+ zwj = false
15
+ strip_codes(str).codepoints.reduce(0) do |acc, cp|
16
+ if zwj
17
+ zwj = false
18
+ next acc
19
+ end
20
+ case cp
21
+ when 0x200d # zero-width joiner
22
+ zwj = true
23
+ acc
24
+ else
25
+ acc + 1
26
+ end
27
+ end
12
28
  end
13
29
 
14
30
  def self.strip_codes(str)
@@ -46,6 +62,10 @@ module Dev
46
62
  control(n.to_s, 'D')
47
63
  end
48
64
 
65
+ def self.cursor_horizontal_absolute(n = 1)
66
+ control(n.to_s, 'G')
67
+ end
68
+
49
69
  # Cursor Visibility
50
70
 
51
71
  def self.show_cursor
@@ -8,14 +8,16 @@ module Dev
8
8
  class Formatter
9
9
  SGR_MAP = {
10
10
  # presentational
11
- 'red' => '31',
12
- 'green' => '32',
13
- 'yellow' => '33',
14
- 'blue' => '34',
15
- 'magenta' => '35',
16
- 'cyan' => '36',
17
- 'bold' => '1',
18
- 'reset' => '0',
11
+ 'red' => '31',
12
+ 'green' => '32',
13
+ 'yellow' => '33',
14
+ 'blue' => '34',
15
+ 'magenta' => '35',
16
+ 'cyan' => '36',
17
+ 'bold' => '1',
18
+ 'italic' => '3',
19
+ 'underline' => '4',
20
+ 'reset' => '0',
19
21
 
20
22
  # semantic
21
23
  'error' => '31', # red
@@ -130,24 +130,40 @@ module Dev
130
130
  prefix << ' ' << text << ' '
131
131
  end
132
132
 
133
+ termwidth = Dev::UI::Terminal.width
134
+
133
135
  suffix = String.new
134
136
  if right_text
135
- suffix << ' ' << right_text << ' ' << color.code << (Dev::UI::Box::Heavy::HORZ * 2)
137
+ suffix << ' ' << right_text << ' '
136
138
  end
137
139
 
138
- textwidth = Dev::UI::ANSI.printing_width(prefix + suffix)
139
- termwidth = Dev::UI::Terminal.width
140
- termwidth = 30 if termwidth < 30
140
+ suffix_width = Dev::UI::ANSI.printing_width(suffix)
141
+ suffix_end = termwidth - 2
142
+ suffix_start = suffix_end - suffix_width
141
143
 
142
- if textwidth > termwidth
144
+ prefix_width = Dev::UI::ANSI.printing_width(prefix)
145
+ prefix_start = 0
146
+ prefix_end = prefix_start + prefix_width
147
+
148
+ if prefix_end > suffix_start
143
149
  suffix = ''
144
- prefix = prefix[0...termwidth]
145
- textwidth = termwidth
150
+ # if prefix_end > termwidth
151
+ # we *could* truncate it, but let's just let it overflow to the
152
+ # next line and call it poor usage of this API.
146
153
  end
147
- padwidth = termwidth - textwidth
148
- pad = Dev::UI::Box::Heavy::HORZ * padwidth
149
154
 
150
- prefix + color.code + pad + suffix + Dev::UI::Color::RESET.code + "\n"
155
+ o = String.new
156
+
157
+ o << color.code
158
+ o << Dev::UI::Box::Heavy::HORZ * termwidth # draw a full line
159
+ o << Dev::UI::ANSI.cursor_horizontal_absolute(1 + prefix_start)
160
+ o << prefix
161
+ o << Dev::UI::ANSI.cursor_horizontal_absolute(1 + suffix_start)
162
+ o << suffix
163
+ o << Dev::UI::Color::RESET.code
164
+ o << "\n"
165
+
166
+ o
151
167
  end
152
168
 
153
169
  module FrameStack
@@ -85,17 +85,10 @@ module Dev
85
85
  end
86
86
 
87
87
  def raw_tty!
88
- begin
89
- $stdin.raw! unless ENV['TEST']
90
- rescue Errno::ENOTTY
91
- ''
92
- end
93
- yield
94
- ensure
95
- begin
96
- $stdin.cooked!
97
- rescue Errno::ENOTTY
98
- ''
88
+ if ENV['TEST'] || !$stdin.tty?
89
+ yield
90
+ else
91
+ $stdin.raw { yield }
99
92
  end
100
93
  end
101
94
 
@@ -151,11 +151,11 @@ module Dev
151
151
  end
152
152
 
153
153
  Dev::UI::Frame.divider('STDOUT')
154
- out = "(empty)" if out.strip.empty?
154
+ out = "(empty)" if out.nil? || out.strip.empty?
155
155
  puts out
156
156
 
157
157
  Dev::UI::Frame.divider('STDERR')
158
- err = "(empty)" if err.strip.empty?
158
+ err = "(empty)" if err.nil? || err.strip.empty?
159
159
  puts err
160
160
  end
161
161
  end
@@ -1,5 +1,5 @@
1
1
  module Dev
2
2
  module UI
3
- VERSION = "0.0.2"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dev-ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Burke Libbey
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-05-31 00:00:00.000000000 Z
12
+ date: 2017-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler