fir-repl 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5ce006c0c7b8576257f7ffa4304a09050a7a4e9b
4
+ data.tar.gz: c374f93b993ec41048fb8d2b7c8f5ae63b52bfbd
5
+ SHA512:
6
+ metadata.gz: 1da41645a9ebefa7b56e853f4d9c70757c55d9993834da384e824b2486e6882aedbbd371a2cba4dd29cbe2e5285b3a47c9537abd5eb526b39d3b7fe896fe21dd
7
+ data.tar.gz: b3374e86acb504681571d88f4926049ad9498447b925645da888dcd77465bf99f891eaae0638e32d2de2d822443d8f7dacc4b08ee030599a5d4bb609fcfd3946
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ License
2
+ -------
3
+
4
+ (The MIT License)
5
+
6
+ Copyright (c) 2017 Nassredean Nasseri (dnasseri)
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining
9
+ a copy of this software and associated documentation files (the
10
+ 'Software'), to deal in the Software without restriction, including
11
+ without limitation the rights to use, copy, modify, merge, publish,
12
+ distribute, sublicense, and/or sell copies of the Software, and to
13
+ permit persons to whom the Software is furnished to do so, subject to
14
+ the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be
17
+ included in all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
20
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ [![CircleCI](https://circleci.com/gh/dnasseri/fir.svg?style=svg&circle-token=547487bfcc46230ec60829366533cbbad14524ee)](https://circleci.com/gh/dnasseri/fir)
2
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
3
+
4
+ ## Description
5
+
6
+ Fir is a ruby repl that is an alternative to IRB. Fir aims to bring some of the friendly features that the [fish](https://github.com/fish-shell/fish-shell) project brought to shells to a ruby repl. Though I originally intended Fir to be a full blown replacement for pry, it is unlikely that you can use it in that capacity, since pry offers so many damn good features that this project does not replicate.
7
+
8
+ Fir does bring some features to the table that pry and other REPL's do not have. The key difference between pry, IRB, Ripl, etc, and Fir is that fir puts stdin into raw mode which allows us to provide features like autosuggestion a la fish, and automatically indenting/dedenting code as you type. Below is a gif of fir in action:
9
+
10
+ ![Fir in Action](fir-example.gif?raw=true "Fir in action")
11
+
12
+ As the video demonstrates, fir is able to indent your code as soon as it is typed, not just when you hit enter. It also can suggest lines from your history file as you type as well!
13
+
14
+ ## Install
15
+
16
+ ## Usage
17
+ ```
18
+ $ fir
19
+ (fir)> ...
20
+ ```
21
+
22
+ ### Key Commands
23
+ Fir aims to bring familiar bash keyboard shortcuts that we all know and love, however many editing commands remain unimplemented. Below is a list of keycommands, what they do, and whether they are implemented.
24
+
25
+ | Command | Alt | Description | Status |
26
+ | --- | --- | --- | --- |
27
+ | Ctrl + a | N/A | Move cursor to the beginning of the line | Implemented |
28
+ | Ctrl + e | N/A | Move cursor to the end of the line | Implemented |
29
+ | Ctrl + c | N/A | Clear current state, and step out of the block. | Implemented |
30
+ | Ctrl + d | N/A | Exit program. | Implemented |
31
+ | Ctrl + v | N/A | Paste from system clipboard. | Implemented |
32
+ | Ctrl + z | N/A | Put the running fir process in the background. | Implemented |
33
+ | Ctrl + p | Up Arrow | Previous command in history (i.e. walk back through the command history). | Implemented |
34
+ | Ctrl + n | Down Arrow | Next command in history (i.e. walk forward through the command history). | Implemented |
35
+ | Ctrl + b | Left Arrow | Backward one character. | Implemented |
36
+ | Ctrl + f | Right Arrow | Forward one character. | Implemented |
37
+ | Ctrl + u | N/A | Cut the line before the cursor position | Not implemented |
38
+ | Ctrl + d | N/A | Delete character under the cursor | Not implemented |
39
+ | Ctrl + h | N/A | Delete character before the cursor (backspace) | Not implemented |
40
+ | Ctrl + w | N/A | Cut the Word before the cursor to the clipboard | Not implemented |
41
+ | Ctrl + k | N/A | Cut the Line after the cursor to the clipboard | Not implemented |
42
+ | Ctrl + t | N/A | Swap the last two characters before the cursor (typo) | Not implemented |
43
+ | Ctrl + y | N/A | Paste the last thing to be cut (yank) | Not implemented |
44
+ | Сtrl + _ | N/A | Undo | Not implemented |
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+ ## Future Ideas
54
+ Below is a list of ideas/features that I would like to eventually add.
55
+
56
+ * Break points a la `binding.pry`
57
+ * Configurability via `.firrc` file
58
+ * Command line options
59
+ * -r: load module (same as ruby -r)
60
+ * -e, --exec: A line of code to execute in context before the session starts
61
+ * --no-history: Disable history loading
62
+ * --no-prompt: Disable prompt
63
+ * Suggesting methods, local variables, instance variables, and global variables that are in scope as you type
64
+ * Colorization
data/bin/fir ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ # encoding: UTF-8
4
+
5
+ require 'fir'
6
+ Fir.start($stdin, $stdout, $stderr)
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+ # encoding: UTF-8
3
+
4
+ require 'io/console'
5
+ require 'optparse'
6
+ require_relative 'fir/key'
7
+ require_relative 'fir/key_command/key_command'
8
+ require_relative 'fir/repl_state'
9
+ require_relative 'fir/screen'
10
+ require_relative 'fir/version'
11
+
12
+ class Fir
13
+ attr_reader :key
14
+ attr_reader :screen
15
+
16
+ def self.start(input, output, error)
17
+ parse_opts(output)
18
+ new(
19
+ Key.new(input),
20
+ Screen.new(output, error)
21
+ ).perform(ReplState.blank)
22
+ end
23
+
24
+ def self.parse_opts(output)
25
+ OptionParser.new do |cl_opts|
26
+ cl_opts.banner = 'Usage: fir [options]'
27
+ cl_opts.on('-v', '--version', 'Show version') do |v|
28
+ config[:version] = v
29
+ end
30
+ end.parse!
31
+ process_immediate_opts(config, output)
32
+ end
33
+
34
+ def self.config
35
+ @config ||= {
36
+ history: '~/.irb_history'
37
+ }
38
+ end
39
+
40
+ def self.process_immediate_opts(opts, output)
41
+ return unless opts[:version]
42
+ output.syswrite(Fir::VERSION)
43
+ exit(0)
44
+ end
45
+
46
+ def initialize(key, screen)
47
+ @key = key
48
+ @screen = screen
49
+ end
50
+
51
+ def perform(state)
52
+ state = yield(state) if block_given?
53
+ perform(state) do
54
+ state.transition(KeyCommand.for(key.get, state)) do |new_state|
55
+ screen.update(state, new_state)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+ # encoding: UTF-8
3
+
4
+ class Fir
5
+ class Cursor
6
+ attr_reader :x
7
+ attr_reader :y
8
+
9
+ def initialize(x, y)
10
+ @x = x
11
+ @y = y
12
+ end
13
+
14
+ def self.blank
15
+ new(0, 0)
16
+ end
17
+
18
+ def clone
19
+ self.class.new(x, y)
20
+ end
21
+
22
+ def up
23
+ self.class.new(x, y - 1)
24
+ end
25
+
26
+ def down
27
+ self.class.new(x, y + 1)
28
+ end
29
+
30
+ def left(n)
31
+ self.class.new(x - n, y)
32
+ end
33
+
34
+ def right(n)
35
+ self.class.new(x + n, y)
36
+ end
37
+
38
+ def ==(other)
39
+ other.x == x && other.y == y
40
+ end
41
+
42
+ def blank?
43
+ x.zero? && y.zero?
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ # encoding: UTF-8
3
+
4
+ require_relative 'screen_helper'
5
+
6
+ class Fir
7
+ class Eraser
8
+ include ScreenHelper
9
+
10
+ attr_reader :output
11
+
12
+ def initialize(output)
13
+ @output = output
14
+ end
15
+
16
+ def perform(state)
17
+ state.lines.length.times do |i|
18
+ output.syswrite("#{horizontal_absolute(1)}#{clear(0)}")
19
+ output.syswrite("#{previous_line(1)}#{clear(0)}") unless i.zero?
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+ # encoding: UTF-8
3
+
4
+ require_relative 'screen_helper'
5
+
6
+ class Fir
7
+ class Evaluater
8
+ include ScreenHelper
9
+
10
+ attr_reader :output
11
+ attr_reader :error
12
+
13
+ def initialize(output, error)
14
+ @output = output
15
+ @error = error
16
+ end
17
+
18
+ def perform(state)
19
+ return unless state.executable?
20
+ begin
21
+ write_result(eval(state.lines.join("\n"), state.repl_binding, 'fir'))
22
+ rescue Exception => e
23
+ write_error(e)
24
+ ensure
25
+ output.syswrite(line_prompt)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def write_result(result)
32
+ output.syswrite(result.inspect)
33
+ output.syswrite(next_line(1))
34
+ end
35
+
36
+ def write_error(result)
37
+ error.syswrite(exception_output(result))
38
+ output.syswrite(next_line(1))
39
+ end
40
+
41
+ def exception_output(exception)
42
+ "#{exception.class}: #{exception.message}\n #{backtrace(exception)}"
43
+ end
44
+
45
+ def backtrace(exception)
46
+ exception
47
+ .backtrace
48
+ .take_while { |line| line !~ %r{/fir/\S+\.rb} }
49
+ .join("\n ")
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+ # encoding: UTF-8
3
+
4
+ require_relative '../fir'
5
+
6
+ class Fir
7
+ class History
8
+ attr_reader :active_selection
9
+
10
+ def initialize
11
+ @active_selection = 0
12
+ end
13
+
14
+ def reset
15
+ @active_selection = 0
16
+ end
17
+
18
+ def up
19
+ @active_selection += 1
20
+ end
21
+
22
+ def down
23
+ @active_selection -= 1 if active_selection.positive?
24
+ end
25
+
26
+ def suggestion(line)
27
+ Fir::Suggestion.new(
28
+ line,
29
+ Fir::History.history
30
+ ).generate(active_selection)
31
+ end
32
+
33
+ class << self
34
+ def history_file
35
+ @history_file ||= Fir.config[:history] &&
36
+ File.expand_path(Fir.config[:history])
37
+ end
38
+
39
+ def history
40
+ if history_file && File.exist?(history_file)
41
+ IO.readlines(history_file).map(&:chomp)
42
+ else
43
+ []
44
+ end
45
+ end
46
+
47
+ def add_line_to_history_file(line)
48
+ return unless history_file
49
+ File.open(history_file, 'a') { |f| f.puts(line) }
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,228 @@
1
+ # frozen_string_literal: true
2
+ # encoding: UTF-8
3
+
4
+ require 'ripper'
5
+
6
+ class Fir
7
+ class Indent
8
+ attr_reader :lines
9
+ attr_reader :stack
10
+ attr_reader :delimiter_stack
11
+ attr_reader :array_stack
12
+ attr_reader :paren_stack
13
+ attr_reader :heredoc_stack
14
+
15
+ def initialize(lines)
16
+ @lines = lines
17
+ @stack = []
18
+ @delimiter_stack = []
19
+ @array_stack = []
20
+ @paren_stack = []
21
+ @heredoc_stack = []
22
+ end
23
+
24
+ OPEN_TOKENS = %w[if while for until unless def class module begin].freeze
25
+ OPTIONAL_DO_TOKENS = %w[for while until].freeze
26
+ WHEN_MIDWAY_TOKEN = %w[else when].freeze
27
+ IF_MIDWAY_TOKEN = %w[else elsif].freeze
28
+ BEGIN_MIDWAY_TOKEN = %w[rescue ensure else].freeze
29
+ BEGIN_IMPLICIT_TOKEN = %w[rescue ensure].freeze
30
+ OPEN_HEREDOC_TOKEN = %w[<<- <<~].freeze
31
+
32
+ def generate
33
+ indents = lines.each.with_index.with_object([]) do |(line, line_index), deltas|
34
+ delta = stack.length
35
+ delta += array_stack.length if in_array?
36
+ delta += paren_stack.length if in_paren?
37
+ delta += heredoc_stack.length if in_heredoc?
38
+ line.split(' ').each_with_index do |word, word_index|
39
+ token = construct_token(word, word_index, line_index)
40
+ if any_open?(token)
41
+ stack.push(token)
42
+ elsif any_midway?(token)
43
+ delta -= 1
44
+ elsif any_close?(token)
45
+ delta -= 1
46
+ stack.pop
47
+ elsif string_open_token?(token)
48
+ delimiter_stack.push(token)
49
+ elsif string_close_token?(token)
50
+ delimiter_stack.pop
51
+ elsif open_array_token?(token)
52
+ array_stack.push(token)
53
+ elsif close_array_token?(token)
54
+ delta -= 1 if array_stack.last.position.y != token.position.y
55
+ array_stack.pop
56
+ elsif open_paren_token?(token)
57
+ paren_stack.push(token)
58
+ elsif close_paren_token?(token)
59
+ delta -= 1 if paren_stack.last.position.y != token.position.y
60
+ paren_stack.pop
61
+ elsif open_heredoc_token?(token)
62
+ heredoc_stack.push(token)
63
+ elsif close_heredoc_token?(token)
64
+ delta -= 1 if heredoc_stack.last.position.y != token.position.y
65
+ heredoc_stack.pop
66
+ end
67
+ end
68
+ deltas << delta
69
+ end
70
+ IndentBlock.new(indents, executable?(indents, lines))
71
+ end
72
+
73
+ private
74
+
75
+ def construct_token(word, word_index, line_index)
76
+ position = Position.new(word_index, line_index)
77
+ Token.new(word, position)
78
+ end
79
+
80
+ def executable?(indents, lines)
81
+ indents[-1].zero? &&
82
+ lines[-1] == '' &&
83
+ !in_block? &&
84
+ !in_string? &&
85
+ !in_heredoc? &&
86
+ !in_paren? &&
87
+ !in_array? &&
88
+ !lines.all? { |line| line == '' }
89
+ end
90
+
91
+ def any_open?(token)
92
+ !in_string? &&
93
+ !in_heredoc? &&
94
+ open_token?(token) ||
95
+ when_open_token?(token) ||
96
+ unmatched_do_token?(token)
97
+ end
98
+
99
+ def any_midway?(token)
100
+ !in_string? &&
101
+ !in_heredoc? &&
102
+ if_midway_token?(token) ||
103
+ begin_midway_token?(token) ||
104
+ when_midway_token?(token)
105
+ end
106
+
107
+ def any_close?(token)
108
+ !in_string? &&
109
+ !in_heredoc? &&
110
+ closing_token?(token) ||
111
+ when_close_token?(token)
112
+ end
113
+
114
+ def string_open_token?(token)
115
+ (token.word[0] == '\'' || token.word[0] == '"') &&
116
+ !((token.word.length > 1) && token.word[-1] == token.word[0]) &&
117
+ !in_string?
118
+ end
119
+
120
+ def string_close_token?(token)
121
+ in_string? &&
122
+ ((token.word[-1] == delimiter_stack.last.word[0]) && (token.word[-2] != "\\"))
123
+ end
124
+
125
+ def open_heredoc_token?(token)
126
+ !in_string? &&
127
+ !in_heredoc? &&
128
+ token.word.length > 3 &&
129
+ OPEN_HEREDOC_TOKEN.include?(token.word[0..2]) &&
130
+ (token.word[3..-1] == token.word[3..-1].upcase)
131
+ end
132
+
133
+ def close_heredoc_token?(token)
134
+ !in_string? &&
135
+ in_heredoc? &&
136
+ heredoc_stack.last.word[3..-1] == token.word
137
+ end
138
+
139
+ def open_array_token?(token)
140
+ !in_string? &&
141
+ token.word[0] == '['
142
+ end
143
+
144
+ def close_array_token?(token)
145
+ !in_string? &&
146
+ in_array? &&
147
+ token.word[-1] == ']'
148
+ end
149
+
150
+ def open_paren_token?(token)
151
+ !in_string? &&
152
+ token.word[0] == '{'
153
+ end
154
+
155
+ def close_paren_token?(token)
156
+ !in_string? &&
157
+ in_paren? &&
158
+ token.word[-1] == '}'
159
+ end
160
+
161
+ def in_array?
162
+ array_stack.length.positive?
163
+ end
164
+
165
+ def in_string?
166
+ delimiter_stack.length.positive?
167
+ end
168
+
169
+ def in_paren?
170
+ paren_stack.length.positive?
171
+ end
172
+
173
+ def in_block?
174
+ stack.length.positive?
175
+ end
176
+
177
+ def in_heredoc?
178
+ heredoc_stack.length.positive?
179
+ end
180
+
181
+ def open_token?(token)
182
+ OPEN_TOKENS.include?(token.word) && token.position.x.zero?
183
+ end
184
+
185
+ def closing_token?(token)
186
+ token.word == 'end' && in_block? && token.position.x.zero?
187
+ end
188
+
189
+ def unmatched_do_token?(token)
190
+ return false unless token.word == 'do'
191
+ !(OPTIONAL_DO_TOKENS.include?(stack.last&.word) &&
192
+ (token.position.y == stack.last&.position&.y))
193
+ end
194
+
195
+ def when_open_token?(token)
196
+ return false unless token.word == 'when' && token.position.x.zero?
197
+ stack.last&.word != 'when'
198
+ end
199
+
200
+ def when_midway_token?(token)
201
+ return false unless WHEN_MIDWAY_TOKEN.include?(token.word) && token.position.x.zero?
202
+ return false unless stack.last&.word == 'when'
203
+ true
204
+ end
205
+
206
+ def when_close_token?(token)
207
+ return false unless token.word == 'end' && token.position.x.zero?
208
+ return false unless stack.last&.word == 'when'
209
+ true
210
+ end
211
+
212
+ def if_midway_token?(token)
213
+ return false unless IF_MIDWAY_TOKEN.include?(token.word) && token.position.x.zero?
214
+ return false unless stack.last&.word == 'if'
215
+ true
216
+ end
217
+
218
+ def begin_midway_token?(token)
219
+ return false unless token.position.x.zero? && stack.last
220
+ (BEGIN_MIDWAY_TOKEN.include?(token.word) && stack.last.word == 'begin') ||
221
+ BEGIN_IMPLICIT_TOKEN.include?(token.word)
222
+ end
223
+
224
+ Token = Struct.new(:word, :position)
225
+ Position = Struct.new(:x, :y)
226
+ IndentBlock = Struct.new(:indents, :executable?)
227
+ end
228
+ end