console 0.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,67 @@
1
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'io/console'
22
+
23
+ module Console
24
+ # Styled terminal output.
25
+ module Terminal
26
+ class Text
27
+ def initialize(output)
28
+ @output = output
29
+ @styles = {}
30
+ end
31
+
32
+ def [] key
33
+ @styles[key]
34
+ end
35
+
36
+ def []= key, value
37
+ @styles[key] = value
38
+ end
39
+
40
+ def style(foreground, background = nil, *attributes)
41
+ end
42
+
43
+ def reset
44
+ end
45
+
46
+ def write(*args, style: nil)
47
+ if style and prefix = self[style]
48
+ @output.write(prefix)
49
+ @output.write(*args)
50
+ @output.write(self.reset)
51
+ else
52
+ @output.write(*args)
53
+ end
54
+ end
55
+
56
+ def puts(*args, style: nil)
57
+ if style and prefix = self[style]
58
+ @output.write(prefix)
59
+ @output.puts(*args)
60
+ @output.write(self.reset)
61
+ else
62
+ @output.puts(*args)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,80 @@
1
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'io/console'
22
+
23
+ require_relative 'text'
24
+
25
+ module Console
26
+ # Styled terminal output.
27
+ module Terminal
28
+ class XTerm < Text
29
+ COLORS = {
30
+ black: 0,
31
+ red: 1,
32
+ green: 2,
33
+ yellow: 3,
34
+ blue: 4,
35
+ magenta: 5,
36
+ cyan: 6,
37
+ white: 7,
38
+ default: 9,
39
+ }
40
+
41
+ ATTRIBUTES = {
42
+ normal: 0,
43
+ bold: 1,
44
+ bright: 1,
45
+ faint: 2,
46
+ italic: 3,
47
+ underline: 4,
48
+ blink: 5,
49
+ reverse: 7,
50
+ hidden: 8,
51
+ }
52
+
53
+ def size
54
+ @output.winsize
55
+ end
56
+
57
+ def style(foreground, background = nil, *attributes)
58
+ tokens = []
59
+
60
+ if foreground
61
+ tokens << 30 + COLORS.fetch(foreground)
62
+ end
63
+
64
+ if background
65
+ tokens << 40 + COLORS.fetch(background)
66
+ end
67
+
68
+ attributes.each do |attribute|
69
+ tokens << ATTRIBUTES.fetch(attribute){attribute.to_i}
70
+ end
71
+
72
+ return "\e[#{tokens.join(';')}m"
73
+ end
74
+
75
+ def reset
76
+ "\e[0m"
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,23 @@
1
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Console
22
+ VERSION = "1.0.0"
23
+ end
@@ -0,0 +1,84 @@
1
+ # Global Logger
2
+
3
+ Configure what logging looks like globally:
4
+
5
+ ```ruby
6
+ Console.logger
7
+ ```
8
+
9
+ Other logger instances would inherit their initial configuration from the global logger.
10
+
11
+ Rather than the output being configurable on the global logger, it would always route to `$stdout`. _Or perhaps no destinations should exist and you add one; there must be a way to attach a formatter to even the default destination._
12
+
13
+ # Adding Destinations
14
+
15
+ The global logger or any instance could log to multiple destinations:
16
+
17
+ ```ruby
18
+ Console.add(some_io, formatter: some_formatter)
19
+ ```
20
+
21
+ I _think_ that formatters would be defined on destinations. This would allow for writing in one format to `$stdout` and another to `syslog`, for example.
22
+
23
+ # Logger Instances
24
+
25
+ Named instances could be created with a given output:
26
+
27
+ ```ruby
28
+ connection_logger = Console.new(:connection, output: ...)
29
+ ```
30
+
31
+ Outputs would not reference an io object, but rather another named instance (defaulting to global). Again, Destinations + Formatters could be added to any instance. This would allow outputs to be chained.
32
+
33
+ ## Instances As Subjects
34
+
35
+ Perhaps an interesting idea is letting logger instance names be subjects, controllable from the global logger. In the example above, `:connection` would become a known subject that could be enabled/disabled from the global logger:
36
+
37
+ ```ruby
38
+ Console.disable(:connection)
39
+ ```
40
+
41
+ This lets us very easily disable the logging of events of a particular type.
42
+
43
+ ---
44
+
45
+ In [Pakyow's logger](https://gist.github.com/bryanp/0329d58c753f1fa6e99d970960ad006d#file-logger-rb), instances are created in these cases:
46
+
47
+ * Environment: Single logger for the environment, named `:pkyw`.
48
+ * Connection: Per-Request logger containing the connection id, named `:http`.
49
+ * WebSocket: Per-WebSocket logger containing the WebSocket id, named `:sock`.
50
+ * Async: The logger that async is configured with, named `:asnc`.
51
+
52
+ Instances could be initialized with metadata that is passed with the console to the formatter:
53
+
54
+ ```ruby
55
+ connection_logger = Console.new(:http, output: ..., connection_id: "123")
56
+ ```
57
+
58
+ # Formatters
59
+
60
+ Just a class with a `format` method that accepts an `console`, which is a hash or simple object containing the console data along with attached metadata (e.g. timestamp, metadata from the instance). Returns a string, and/or writes directly to the buffer.
61
+
62
+ # Custom Levels
63
+
64
+ Pakyow's logger uses the following levels:
65
+
66
+ * all
67
+ * verbose
68
+ * debug
69
+ * info
70
+ * warn
71
+ * error
72
+ * fatal
73
+ * unknown
74
+ * off
75
+
76
+ `all` and `off` aren't turned into logging methods in `Pakyow::Logger`, but rather the log level can be set to either one as an easy way to guarantee that all or no logs will be written, without knowing what the lowest and highest level of logging are in the system.
77
+
78
+ Projects often have different needs, so making this easily configurable on both the global logger and individual logger instances would be amazing:
79
+
80
+ ```ruby
81
+ Console.levels(
82
+ %i(all verbose debug info warn error fatal unknown off)
83
+ )
84
+ ```
metadata CHANGED
@@ -1,72 +1,121 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: console
3
- version: !ruby/object:Gem::Version
4
- hash: 1
5
- prerelease:
6
- segments:
7
- - 0
8
- - 5
9
- version: "0.5"
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
10
5
  platform: ruby
11
- authors:
12
- - William Morgan
6
+ authors:
7
+ - Samuel Williams
13
8
  autorequire:
14
9
  bindir: bin
15
10
  cert_chain: []
16
-
17
- date: 2011-06-27 20:12:30 -07:00
18
- default_executable:
19
- dependencies: []
20
-
21
- description: Console is a helper class for displaying super-ASCII strings on the console. Console is needed when you want to mix two-column (e.g. Chinese) and single-column (e.g. ASCII) characters and know how much horizontal realestate the result takes on the screen. This is generally necessary when you want to have internationalization support in a console-based program. Console currently provides utility methods for determining the display width of a string, and for taking a substring based on display position and display width.
22
- email: wmorgan-console@masanjin.net
11
+ date: 2019-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: covered
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - samuel.williams@oriontransfer.co.nz
23
72
  executables: []
24
-
25
- extensions:
26
- - ext/console/extconf.rb
27
- extra_rdoc_files:
28
- - README
29
- - ext/console/console.c
30
- files:
31
- - README
32
- - ext/console/extconf.rb
33
- - ext/console/console.c
34
- - test/console.rb
35
- - lib/console/string.rb
36
- has_rdoc: true
37
- homepage: http://masanjin.net/console
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - console.gemspec
83
+ - lib/console.rb
84
+ - lib/console/buffer.rb
85
+ - lib/console/capture.rb
86
+ - lib/console/error.rb
87
+ - lib/console/filter.rb
88
+ - lib/console/generic.rb
89
+ - lib/console/logger.rb
90
+ - lib/console/serialized/logger.rb
91
+ - lib/console/shell.rb
92
+ - lib/console/split.rb
93
+ - lib/console/terminal.rb
94
+ - lib/console/terminal/logger.rb
95
+ - lib/console/terminal/text.rb
96
+ - lib/console/terminal/xterm.rb
97
+ - lib/console/version.rb
98
+ - proposal.md
99
+ homepage: https://github.com/socketry/console
38
100
  licenses: []
39
-
101
+ metadata: {}
40
102
  post_install_message:
41
- rdoc_options:
42
- - -c
43
- - utf8
44
- require_paths:
103
+ rdoc_options: []
104
+ require_paths:
45
105
  - lib
46
- required_ruby_version: !ruby/object:Gem::Requirement
47
- none: false
48
- requirements:
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
49
108
  - - ">="
50
- - !ruby/object:Gem::Version
51
- hash: 3
52
- segments:
53
- - 0
54
- version: "0"
55
- required_rubygems_version: !ruby/object:Gem::Requirement
56
- none: false
57
- requirements:
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
58
113
  - - ">="
59
- - !ruby/object:Gem::Version
60
- hash: 3
61
- segments:
62
- - 0
63
- version: "0"
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
64
116
  requirements: []
65
-
66
- rubyforge_project:
67
- rubygems_version: 1.6.0
117
+ rubygems_version: 3.0.2
68
118
  signing_key:
69
- specification_version: 3
70
- summary: Console is a helper for properly handling super-ASCII strings on the console.
119
+ specification_version: 4
120
+ summary: Beautiful logging for Ruby.
71
121
  test_files: []
72
-
data/README DELETED
@@ -1,51 +0,0 @@
1
- Console is a helper class for displaying super-ASCII strings on the console.
2
-
3
- Console is necessary when you want to mix multi-column (e.g. Chinese, Japanese
4
- and Korean characters) and single-column (e.g. ASCII, Latin, Vietnamese)
5
- characters on screen in such a way that the display width matters. This is
6
- typically the case in curses programs with i18n support, but can be necessary
7
- in certain internationalized $stdout applications as well.
8
-
9
- Note that display width is different from a) the number of bytes in the string,
10
- and b) the number of characters in the string. When you move beyond ASCII
11
- strings, these three metrics can all have distinct values for a given string.
12
-
13
- The Console gem currently provides these methods:
14
-
15
- - Console.init_locale!: set the program's locale from the appropriate
16
- environment variables. (Ruby 1.8 programs must call this before calling any
17
- of the other methods. Ruby 1.9 programs can call it or skip it without
18
- effect.)
19
- - Console.display_width: calculates the display width of a string
20
- - Console.display_slice: returns a substring according to display offset
21
- and display width parameters.
22
-
23
- If you require 'console', you will get just those methods.
24
-
25
- If you require 'console/string', you will get String#display_width and
26
- String#display_slice methods directly on all strings.
27
-
28
- = EXAMPLE USAGE
29
-
30
- ## encoding: UTF-8 (this comment required for ruby 1.9)
31
- require 'rubygems' # this line required for ruby 1.8
32
- require 'console'
33
- require 'console/string'
34
-
35
- Console.init_locale!
36
-
37
- STRING = "我能吞下玻璃而不傷身體。Góa ē-tàng chia̍h po-lê, mā bē tio̍h-siong.私はガラスを食べられます。それは私を傷つけません。I can eat glass and it doesn't hurt me."
38
- COLS = 30
39
-
40
- rows = STRING.display_width / COLS
41
- (0 .. rows).each { |i| puts STRING.display_slice(i * COLS, COLS) }
42
-
43
- The result will be displayed in an even 30-column block on the console, even
44
- though some characters in the string require two columns to display and some
45
- characters require one column.
46
-
47
- = MORE
48
-
49
- The console homepage is <code>http://masanjin.net/console/</code>.
50
- The console git repo is <code>git://masanjin.net/console</code>.
51
-