yap-rawline 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,106 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/rawline/history_buffer.rb"
4
+
5
+ describe RawLine::HistoryBuffer do
6
+
7
+ before :each do
8
+ @history = RawLine::HistoryBuffer.new(5)
9
+ end
10
+
11
+ it "instantiates an empty array when created" do
12
+ expect(@history.length).to eq(0)
13
+ end
14
+
15
+ it "allows items to be added to the history" do
16
+ @history.duplicates = false
17
+ @history << "line #1"
18
+ @history << "line #2"
19
+ @history << "line #3"
20
+ @history << "line #2"
21
+ expect(@history).to eq(["line #1", "line #3", "line #2"])
22
+ @history.duplicates = true
23
+ @history << "line #3"
24
+ expect(@history).to eq(["line #1", "line #3", "line #2", "line #3"])
25
+ @history.exclude = lambda { |i| i.match(/line #[456]/) }
26
+ @history << "line #4"
27
+ @history << "line #5"
28
+ @history << "line #6"
29
+ expect(@history).to eq(["line #1", "line #3", "line #2", "line #3"])
30
+ end
31
+
32
+ it "does not overflow" do
33
+ @history << "line #1"
34
+ @history << "line #2"
35
+ @history << "line #3"
36
+ @history << "line #4"
37
+ @history << "line #5"
38
+ @history << "line #6"
39
+ expect(@history.length).to eq(5)
40
+ end
41
+
42
+ it "allows navigation back and forward" do
43
+ @history.back
44
+ @history.forward
45
+ expect(@history.position).to eq(nil)
46
+ @history << "line #1"
47
+ @history << "line #2"
48
+ @history << "line #3"
49
+ @history << "line #4"
50
+ @history << "line #5"
51
+ @history.back
52
+ @history.back
53
+ @history.back
54
+ @history.back
55
+ @history.back
56
+ expect(@history.position).to eq(0)
57
+ @history.back
58
+ expect(@history.position).to eq(0)
59
+ @history.forward
60
+ expect(@history.position).to eq(1)
61
+ @history.forward
62
+ @history.forward
63
+ @history.forward
64
+ @history.forward
65
+ expect(@history.position).to eq(4)
66
+ @history.forward
67
+ expect(@history.position).to eq(4)
68
+ @history.cycle = true
69
+ @history.forward
70
+ @history.forward
71
+ expect(@history.position).to eq(1)
72
+ end
73
+
74
+ it "can retrieve the last element or the element at @position via 'get'" do
75
+ expect(@history.get).to eq(nil)
76
+ @history << "line #1"
77
+ @history << "line #2"
78
+ @history << "line #3"
79
+ @history << "line #4"
80
+ @history << "line #5"
81
+ expect(@history.get).to eq("line #5")
82
+ @history.back
83
+ expect(@history.get).to eq("line #4")
84
+ @history.forward
85
+ expect(@history.get).to eq("line #5")
86
+ end
87
+
88
+ it "can be cleared and resized" do
89
+ @history << "line #1"
90
+ @history << "line #2"
91
+ @history << "line #3"
92
+ @history << "line #4"
93
+ @history << "line #5"
94
+ @history.back
95
+ @history.back
96
+ expect(@history.get).to eq("line #4")
97
+ @history.resize(6)
98
+ expect(@history.position).to eq(nil)
99
+ @history << "line #6"
100
+ expect(@history.get).to eq("line #6")
101
+ @history.empty
102
+ expect(@history).to eq([])
103
+ expect(@history.size).to eq(6)
104
+ expect(@history.position).to eq(nil)
105
+ end
106
+ end
@@ -0,0 +1,96 @@
1
+ require "spec_helper"
2
+
3
+ require_relative "../lib/rawline/keycode_parser.rb"
4
+
5
+ describe RawLine::KeycodeParser do
6
+ subject(:keycode_parser) { described_class.new(keymap) }
7
+ let(:keymap) { {} }
8
+
9
+ describe "#parse_bytes" do
10
+ def parse_bytes(bytes)
11
+ keycode_parser.parse_bytes(bytes)
12
+ end
13
+
14
+ context "given a char that isn't in the keymap" do
15
+ it "returns the byte as a keycode" do
16
+ expect(parse_bytes(["a"])).to eq ["a".ord]
17
+ end
18
+ end
19
+
20
+ context "given a char that isn't in the keymap" do
21
+ it "returns the byte as a keycode" do
22
+ expect(parse_bytes([97])).to eq ["a".ord]
23
+ end
24
+ end
25
+
26
+ context "given multiple characters that aren't in the keymap" do
27
+ it "returns the bytes as individual keycodes" do
28
+ expect(parse_bytes(["a", "b", "C"])).to eq ["a", "b", "C"].map(&:ord)
29
+ end
30
+ end
31
+
32
+ context "given multiple characters that aren't in the keymap" do
33
+ it "returns the bytes as individual keycodes" do
34
+ expect(parse_bytes([97, 98, 67])).to eq ["a", "b", "C"].map(&:ord)
35
+ end
36
+ end
37
+
38
+ context "given a byte in the keymap" do
39
+ let(:keymap) do
40
+ { backspace: [?\C-?.ord] }
41
+ end
42
+
43
+ it "returns the bytes as a single key code" do
44
+ expect(parse_bytes([127])).to eq [[127]]
45
+ end
46
+ end
47
+
48
+ context "given multiple bytes – some in the keymap, some not" do
49
+ let(:keymap) do
50
+ { backspace: [?\C-?.ord] }
51
+ end
52
+
53
+ it "returns the key codes appropriately" do
54
+ expect(parse_bytes([97, 127, 67])).to eq [97, [127], 67]
55
+ end
56
+ end
57
+
58
+ context "given multiple bytes which make up a single keycode in the keymap" do
59
+ let(:keymap) do
60
+ { left_arrow: [?\e.ord, ?[.ord, ?D.ord] }
61
+ end
62
+
63
+ it "returns a single key code" do
64
+ expect(parse_bytes([?\e.ord, ?[.ord, ?D.ord])).to eq [[?\e.ord, ?[.ord, ?D.ord]]
65
+ end
66
+ end
67
+
68
+ context "given multiple bytes where some bytes make up a multi-byte keycode and others don't" do
69
+ let(:keymap) do
70
+ { left_arrow: [?\e.ord, ?[.ord, ?D.ord] }
71
+ end
72
+
73
+ it "returns a keycodes appropriately" do
74
+ expect(parse_bytes([97, ?\e.ord, ?[.ord, ?D.ord, 67])).to eq [97, [?\e.ord, ?[.ord, ?D.ord], 67]
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+
83
+ __END__
84
+ @escape_codes = [?\e.ord]
85
+ @keys.merge!(
86
+ {
87
+ :up_arrow => [?\e.ord, ?[.ord, ?A.ord],
88
+ :down_arrow => [?\e.ord, ?[.ord, ?B.ord],
89
+ :right_arrow => [?\e.ord, ?[.ord, ?C.ord],
90
+ :left_arrow => [?\e.ord, ?[.ord, ?D.ord],
91
+ :insert => [?\e.ord, ?[, ?2.ord, ?~.ord],
92
+ :delete => [?\e.ord, ?[, ?3.ord, ?~.ord],
93
+ :backspace => [?\C-?.ord],
94
+ :enter => (HighLine::SystemExtensions::CHARACTER_MODE == 'termios' ? [?\n.ord] : [?\r.ord]),
95
+
96
+ :ctrl_alt_a => [?\e.ord, ?\C-a.ord],
data/spec/line_spec.rb ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'highline'
5
+
6
+ require_relative "../lib/rawline/history_buffer.rb"
7
+ require_relative "../lib/rawline/line.rb"
8
+
9
+ describe RawLine::Line do
10
+
11
+ before :each do
12
+ @line = RawLine::Line.new(5) {|l| l.prompt = "=>" }
13
+ end
14
+
15
+ it "allows characters to be added to @text via the '<<' operator" do
16
+ @line.text = "test #1"
17
+ @line << 'a'[0]
18
+ expect(@line.text).to eq("test #1a")
19
+ end
20
+
21
+ it "allows characters to be retrieved and substituted via '[]' and '[]=' operators" do
22
+ @line.text = 'test #2'
23
+ @line[0] = 't'
24
+ expect(@line[0..3]).to eq('test')
25
+ @line[4..6] = ''
26
+ @line[0] = "This is a t"
27
+ expect(@line.text).to eq("This is a test")
28
+ end
29
+
30
+ it "updates @position via 'left' and 'right'" do
31
+ @line.text = "test #3"
32
+ @line.right 2
33
+ expect(@line.position).to eq(2)
34
+ @line.left
35
+ expect(@line.position).to eq(1)
36
+ @line.left 4
37
+ expect(@line.position).to eq(0)
38
+ @line.right HighLine::SystemExtensions.terminal_size[0]+10
39
+
40
+ expect(@line.position).to eq(HighLine::SystemExtensions.terminal_size[0]+10)
41
+ expect(@line.eol).to eq(6)
42
+ end
43
+
44
+ it "is aware of the word in which the cursor is" do
45
+ @line.text = "This is another test"
46
+ expect(@line.word).to eq({:start => 0, :end => 3, :text => "This"})
47
+ @line.right 2
48
+ expect(@line[2]).to eq('i'[0])
49
+ expect(@line.word).to eq({:start => 0, :end => 3, :text => "This"})
50
+ @line.right
51
+ expect(@line.word).to eq({:start => 0, :end => 3, :text => "This"})
52
+ @line.right
53
+ expect(@line.word).to eq({:start => 0, :end => 3, :text => "This"})
54
+ @line.right
55
+ expect(@line.word).to eq({:start => 5, :end => 6, :text => "is"})
56
+ end
57
+
58
+ end
@@ -0,0 +1,8 @@
1
+ describe RawLine::Prompt do
2
+ describe "#length" do
3
+ it "returns the length of prompt ignoring ANSI escape sequences" do
4
+ prompt = RawLine::Prompt.new("\e[01;31mhello\e[00m")
5
+ expect(prompt.length).to eq("hello".length)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,96 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Allows RSpec to persist some state between runs in order to support
54
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
55
+ # you configure your source control system to ignore this file.
56
+ config.example_status_persistence_file_path = "spec/examples.txt"
57
+
58
+ # Limits the available syntax to the non-monkey patched syntax that is
59
+ # recommended. For more details, see:
60
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
61
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
62
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
63
+ config.disable_monkey_patching!
64
+
65
+ # This setting enables warnings. It's recommended, but in some cases may
66
+ # be too noisy due to issues in dependencies.
67
+ config.warnings = true
68
+
69
+ # Many RSpec users commonly either run the entire suite or an individual
70
+ # file, and it's useful to allow more verbose output when running an
71
+ # individual spec file.
72
+ if config.files_to_run.one?
73
+ # Use the documentation formatter for detailed output,
74
+ # unless a formatter has already been configured
75
+ # (e.g. via a command-line flag).
76
+ config.default_formatter = 'doc'
77
+ end
78
+
79
+ # Print the 10 slowest examples and example groups at the
80
+ # end of the spec run, to help surface which specs are running
81
+ # particularly slow.
82
+ config.profile_examples = 10
83
+
84
+ # Run specs in random order to surface order dependencies. If you find an
85
+ # order dependency and want to debug it, you can fix the order by providing
86
+ # the seed, which is printed after each run.
87
+ # --seed 1234
88
+ config.order = :random
89
+
90
+ # Seed global randomization in this process using the `--seed` CLI option.
91
+ # Setting this allows you to use `--seed` to deterministically reproduce
92
+ # test failures related to randomization by passing the same `--seed` value
93
+ # as the one that triggered the failure.
94
+ Kernel.srand config.seed
95
+ =end
96
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yap-rawline
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fabio Cevasco
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: highline
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.7.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.7'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.7.2
33
+ - !ruby/object:Gem::Dependency
34
+ name: terminal-layout
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.1.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.1.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.0'
61
+ description: 'RawLine can be used to define custom key bindings, perform common line
62
+ editing operations, manage command history and define custom command completion
63
+ rules. '
64
+ email: h3rald@h3rald.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - CHANGELOG.rdoc
70
+ - LICENSE
71
+ - README.rdoc
72
+ - examples/key_tester.rb
73
+ - examples/rawline_irb.rb
74
+ - examples/rawline_rush.rb
75
+ - examples/rawline_shell.rb
76
+ - examples/readline_emulation.rb
77
+ - lib/rawline.rb
78
+ - lib/rawline/completer.rb
79
+ - lib/rawline/editor.rb
80
+ - lib/rawline/event_loop.rb
81
+ - lib/rawline/event_registry.rb
82
+ - lib/rawline/history_buffer.rb
83
+ - lib/rawline/keycode_parser.rb
84
+ - lib/rawline/line.rb
85
+ - lib/rawline/prompt.rb
86
+ - lib/rawline/terminal.rb
87
+ - lib/rawline/terminal/vt220_terminal.rb
88
+ - lib/rawline/terminal/windows_terminal.rb
89
+ - spec/editor_spec.rb
90
+ - spec/history_buffer_spec.rb
91
+ - spec/keycode_parser_spec.rb
92
+ - spec/line_spec.rb
93
+ - spec/prompt_spec.rb
94
+ - spec/spec_helper.rb
95
+ homepage: http://rubyforge.org/projects/rawline
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.4.6
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: A library for defining custom key bindings and perform line editing operations
119
+ test_files: []