terminal_rb 0.20.0 → 1.0.4

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.
data/lib/terminal.rb CHANGED
@@ -4,105 +4,109 @@ require_relative 'terminal/output'
4
4
  require_relative 'terminal/input'
5
5
  require_relative 'terminal/ansi'
6
6
 
7
+ # Terminal I/O with ANSI support, BBCode markup, Unicode display width,
8
+ # keyboard/mouse events, and text formatting.
7
9
  #
8
- # Terminal access with support for ANSI control codes and
9
- # [BBCode-like](https://en.wikipedia.org/wiki/BBCode) embedded text attribute
10
- # syntax (see {Ansi.bbcode}).
11
- # It automagically detects whether your terminal supports ANSI features, like
12
- # coloring (see {colors}) or the
13
- # [CSIu protocol](https://sw.kovidgoyal.net/kitty/keyboard-protocol) support
14
- # (see {read_key_event}, {on_key_event} and {input_mode}).
15
- # It calculates the display width for Unicode chars (see {Text.width}) and help
16
- # you to display text with word-wise line breaks (see {Text.each_line}).
10
+ # The appropriate output and input modes are selected automatically at
11
+ # load time based on terminal capabilities. Use the class-level methods
12
+ # for all interaction.
13
+ #
14
+ # @example Formatted output with BBCode
15
+ # Terminal.puts '[bold green]Success![/]'
16
+ #
17
+ # @example Read a key event
18
+ # event = Terminal.read_key_event
19
+ # puts "You pressed: #{event.name}"
20
+ #
21
+ # @example Execute a shell command
22
+ # status, out, err = Terminal.sh('ls', '-la')
17
23
  #
18
24
  module Terminal
19
25
  class << self
26
+ #
20
27
  # @!group Attributes
28
+ #
21
29
 
22
- # Terminal application identifier.
30
+ # Detect the terminal emulator application.
23
31
  #
24
- # The detection supports a wide range of terminal emulators but may return
25
- # `nil` if an unsupported terminal was found. These are the supported
26
- # application IDs:
27
- # `:alacritty`, `:amiga`, `:code_edit`, `:dg_unix`, `:docker`, `:fluent`,
28
- # `:hpterm`, `:hyper`, `:iterm`, `:macos`, `:mintty`, `:ms_terminal`,
29
- # `:ncr260`, `:nsterm`, `:terminator`, `:terminology`, `:termite`,
30
- # `:vt100`, `:warp`, `:wezterm`, `:wyse`, `:xnuppc`
32
+ # Known applications:
33
+ # +:alacritty+,
34
+ # +:amiga+,
35
+ # +:code_edit+,
36
+ # +:dg_unix+,
37
+ # +:docker+,
38
+ # +:fluent+,
39
+ # +:hpterm+,
40
+ # +:hyper+,
41
+ # +:iterm+,
42
+ # +:kitty+,
43
+ # +:macos+,
44
+ # +:mintty+,
45
+ # +:ms_terminal+,
46
+ # +:ncr260+,
47
+ # +:nsterm+,
48
+ # +:terminator+,
49
+ # +:terminology+,
50
+ # +:termite+,
51
+ # +:vscode+,
52
+ # +:vt100+,
53
+ # +:warp+,
54
+ # +:wyse+,
55
+ # +:xnuppc+
56
+ #
57
+ # Any unrecognized +TERM_PROGRAM+ value is converted to a sanitized
58
+ # symbol. Returns +nil+ when no terminal can be identified.
59
+ #
60
+ # @example
61
+ # Terminal.application # => :kitty
31
62
  #
32
63
  # @attribute [r] application
33
- # @return [Symbol, nil] the application identifier
64
+ # @return [Symbol, nil] the detected application
34
65
  def application = (@application ||= Detect.application)
35
66
 
36
- # @!endgroup
37
-
38
- # @!group Tool methods
39
-
40
- # Execute a command and report command output line by line
41
- # or capture the output.
42
67
  #
43
- # @example Execute a command wih arguments
44
- # ret, out, = Terminal.sh('curl', '--version')
45
- # raise('command not found - curl') unless ret
46
- # puts out
68
+ # @!endgroup
47
69
  #
48
- # @example Execute shell commands and print error
49
- # ret, _, err = Terminal.sh("mkdir '/test' && cd '/test'")
50
- # raise('unable to execute') unless ret
51
- # puts("error #{ret.exitstatus}: #{err.join}") unless ret.success?
70
+ # @!group Tool methods
52
71
  #
53
- # @example Copy a file content to clipboard
54
- # command = ENV.fetch('CLIP_COPY', 'pbcopy')
55
- # File.open(__FILE__) do |file|
56
- # ret, = Terminal.sh(*command, input: file)
57
- # raise("command not found - #{command}") unless ret
58
- # end
72
+
73
+ # Execute a shell command.
59
74
  #
60
75
  # @overload sh(*cmd, **options)
61
- # @return [[Process::Status, output, error]]
62
- # the status of the process successfully executed,
63
- # the captured output,
64
- # the captured error output,
65
- # @return [nil]
66
- # when the command was not executable
76
+ # Run a command and capture output.
77
+ # @param cmd [Array<String>] command and arguments
78
+ # @param options [Hash] options passed to the shell runner
79
+ # @return [Array<Process::Status, Array<String>, Array<String>>]
80
+ # +[status, stdout_lines, stderr_lines]+
81
+ # @example
82
+ # status, output, error = Terminal.sh('echo', 'hello')
83
+ # output # => ["hello\n"]
67
84
  #
68
- # @overload sh(*cmd, **options)
69
- # @yieldparam line [String]
70
- # next received line from the process
71
- # @yieldparam type [:output, :error]
72
- # whether the received line is standard output or error
73
- # @return [Process::Status]
74
- # the status of the process successfully executed
75
- # @return [nil]
76
- # when the command was not executable
85
+ # @overload sh(*cmd, **options, &block)
86
+ # Run a command and stream output to a block.
87
+ # @param cmd [Array<String>] command and arguments
88
+ # @param options [Hash] options passed to the shell runner
89
+ # @yield [line, type] called for each output line
90
+ # @yieldparam line [String] the output line
91
+ # @yieldparam type [Symbol] +:output+ or +:error+
92
+ # @return [Process::Status] the exit status
93
+ # @example
94
+ # Terminal.sh('make') do |line, type|
95
+ # Terminal.puts(type == :error ? "[red]#{line}[/]" : line)
96
+ # end
77
97
  #
78
- # @param cmd [Array<#to_s>]
79
- # command line or command with arguments
80
- # @param options [Hash]
81
- # options how to execute the new process;
82
- # see `Process.spawn` for default execution options provided by Ruby
83
- # @option options [Hash<String,String>] :env
84
- # key/value pairs added to the ENV of the process;
85
- # with option `:unsetenv_others` set to `true` it replaces the ENV
86
- # @option options [true, false] :shell
87
- # whether to create a seperate shell or not
88
- # @option options [#readpartial, #to_io, Array, #each, #to_a, #to_s] :input
89
- # piped to the command as input
90
- # @raise [ArgumentError] when command is missing
91
- def sh(*cmd, **options, &block)
98
+ # @raise [ArgumentError] if no command is given
99
+ def sh(*cmd, **, &)
92
100
  raise(ArgumentError, 'command expected') if cmd.empty?
93
- return Shell.exec(cmd, **options, &block) if block_given?
101
+ return Shell.run(*cmd, **, &) if block_given?
94
102
  out = []
95
103
  err = []
96
- [
97
- Shell.exec(cmd, **options) do |line, type|
98
- (type == :error ? err : out) << line
99
- end,
100
- out,
101
- err
102
- ]
104
+ [Shell.run(*cmd, **) { |l, t| (t == :error ? err : out) << l }, out, err]
103
105
  end
104
106
 
107
+ #
105
108
  # @!endgroup
109
+ #
106
110
  end
107
111
 
108
112
  dir = "#{__dir__}/terminal"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terminal_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.0
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
@@ -29,10 +29,12 @@ files:
29
29
  - examples/bbcode.rb
30
30
  - examples/info.rb
31
31
  - examples/key-codes.rb
32
+ - examples/screen_viewer.rb
32
33
  - examples/text.rb
33
34
  - lib/terminal.rb
34
35
  - lib/terminal/ansi.rb
35
36
  - lib/terminal/ansi/named_colors.rb
37
+ - lib/terminal/ansi/screen_viewer.rb
36
38
  - lib/terminal/detect.rb
37
39
  - lib/terminal/input.rb
38
40
  - lib/terminal/input/ansi.rb
@@ -45,19 +47,19 @@ files:
45
47
  - lib/terminal/shell.rb
46
48
  - lib/terminal/text.rb
47
49
  - lib/terminal/text/char_width.rb
50
+ - lib/terminal/text/formatter.rb
48
51
  - lib/terminal/version.rb
49
52
  - lib/terminal_rb.rb
50
- - terminal_rb.gemspec
51
53
  homepage: https://codeberg.org/mblumtritt/Terminal.rb
52
54
  licenses:
53
55
  - MIT
54
56
  - Ruby
55
57
  metadata:
56
- source_code_uri: https://codeberg.org/mblumtritt/Terminal.rb
57
- bug_tracker_uri: https://codeberg.org/mblumtritt/Terminal.rb/issues
58
- documentation_uri: https://rubydoc.info/gems/terminal_rb/0.20.0
59
58
  rubygems_mfa_required: 'true'
60
59
  yard.run: yard
60
+ source_code_uri: https://codeberg.org/mblumtritt/Terminal.rb
61
+ bug_tracker_uri: https://codeberg.org/mblumtritt/Terminal.rb/issues
62
+ documentation_uri: https://rubydoc.info/gems/terminal_rb/1.0.4
61
63
  rdoc_options: []
62
64
  require_paths:
63
65
  - lib
@@ -65,14 +67,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
67
  requirements:
66
68
  - - ">"
67
69
  - !ruby/object:Gem::Version
68
- version: '3.0'
70
+ version: '3.4'
69
71
  required_rubygems_version: !ruby/object:Gem::Requirement
70
72
  requirements:
71
73
  - - ">="
72
74
  - !ruby/object:Gem::Version
73
75
  version: '0'
74
76
  requirements: []
75
- rubygems_version: 4.0.5
77
+ rubygems_version: 4.0.11
76
78
  specification_version: 4
77
79
  summary: Fast terminal access with ANSI, CSIu, mouse events, BBCode, word-wise line
78
80
  break support and much more.
data/terminal_rb.gemspec DELETED
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'lib/terminal/version'
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = 'terminal_rb'
7
- spec.version = Terminal::VERSION
8
- spec.summary = <<~SUMMARY.tr("\n", ' ')
9
- Fast terminal access with ANSI, CSIu, mouse events, BBCode, word-wise
10
- line break support and much more.
11
- SUMMARY
12
- spec.description = <<~DESCRIPTION.tr("\n", ' ')
13
- Terminal.rb supports you with input and output on your terminal.
14
- Simple BBCode-like markup for attributes and coloring, word-wise line
15
- breaks, correct special key recognition and mouse event reporting enable
16
- you to implement your CLI app quickly and easily.
17
- DESCRIPTION
18
-
19
- spec.author = 'Mike Blumtritt'
20
- spec.licenses = %w[MIT Ruby]
21
- spec.homepage = 'https://codeberg.org/mblumtritt/Terminal.rb'
22
- spec.metadata['source_code_uri'] = spec.homepage
23
- spec.metadata['bug_tracker_uri'] = "#{spec.homepage}/issues"
24
- spec.metadata['documentation_uri'] = "https://rubydoc.info/gems/terminal_rb/#{
25
- Terminal::VERSION
26
- }"
27
- spec.metadata['rubygems_mfa_required'] = 'true'
28
- spec.metadata['yard.run'] = 'yard'
29
-
30
- spec.required_ruby_version = '> 3.0'
31
-
32
- spec.files = Dir['lib/**/*.rb'] + Dir['examples/*.rb']
33
- spec.files << 'terminal_rb.gemspec' << '.yardopts'
34
- spec.executables = %w[bbcode]
35
- spec.extra_rdoc_files = %w[README.md]
36
- end