gm-notepad 0.0.5 → 0.0.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf576a76ad9f5f2c4676b9a14d9f3557eeddddf74e2050ebc17a97e3d9f65f65
4
- data.tar.gz: 81b1342b09f2321b1efa7dbd3b53acedc21303d27ce8bdb2b3e56888805b896c
3
+ metadata.gz: 6fad1534243d237bace667966a495af3e787a0bc67ef40283e96a2d7ca49942f
4
+ data.tar.gz: b37925618ac94a01fdceadba354c641f704756ce55788c839a518e4e3a474b24
5
5
  SHA512:
6
- metadata.gz: 78d7865d2e222c20e8acb490637b0442426bad4fb9d739cfb32bbb1f4c283ac92ca5037e96ec80b58bb8bdab55ad48b7dd3d699ac5b28f1817bdc86e4b6cb696
7
- data.tar.gz: 00262af2d6920303a49c22946cf7224f8e09e6b0bfe8da2275ae283ce097079256e61574e3bb00f19c17dd48fe0d13aa4a4fd06d85ad0028806cf3661924edd2
6
+ metadata.gz: 80173f33eff4c3f0aa01f4ec706f2759d8f91b698b0c45872c299b94e0ac327f5deb1d9b2bf85fa468eaf28b9744f08aa7af67ccb20dd201fe8ce7159ba15e11
7
+ data.tar.gz: 49ef7f14a885164ff70dc9e46bb2d05867fe53a9345cb5223efd2624fad4cad54e3f27d85f4aca4df0233cf973899f73786f1d824f958d9f09a8ceda837e7f3b
data/README.md CHANGED
@@ -207,7 +207,9 @@ entry has a 1 in 3 chance of being randomly chosen.
207
207
  - [X] Colorize puts to `interactive` buffer
208
208
  - [X] Disable colors as a configuration option
209
209
  - [ ] Write expected interface document
210
- - [ ] Allow `{critical[2d6+1]}` to roll the dice then lookup the value in the critical table
210
+ - [X] Handle `{critical[5]}`
211
+ - [X] Allow `{critical[{2d6+1}]}` to roll the dice then lookup the value in the critical table
212
+ - [ ] For `{critical[{2d6+1}]}`, how to handle out of bounds
211
213
  - [X] Skip table lines that begin with `#`
212
214
  - [X] Skip processing input lines that begin with `#`
213
215
  - [X] Allow configuration to specify table delimiter
@@ -222,7 +224,6 @@ entry has a 1 in 3 chance of being randomly chosen.
222
224
  - [ ] Add config that expands dice results while including the requested roll
223
225
  - [X] Add index name when rendering table entries
224
226
  - [ ] Gracefully handle loading a malformed data file (maybe?)
225
- - [ ] Add force write results to `output`
226
227
  - [X] Add concept of history
227
228
  - [ ] When expanding tables account for line expansion (via \n and \t)
228
229
  - [ ] Separate the InputHandler into pre-amble (e.g. allow overrides to where we are writing, determine what command we are writing)
@@ -243,3 +244,4 @@ entry has a 1 in 3 chance of being randomly chosen.
243
244
  - [ ] Add auto table expansion for "+"
244
245
  - [ ] Add auto index expansion for "["
245
246
  - [ ] Determine feasibility of adding dice to the `{}` expansion syntax (instead of the `[]` syntax)
247
+ - [ ] Add force write results to `output`
@@ -1,9 +1,21 @@
1
1
  module Gm
2
2
  module Notepad
3
+ class RuntimeError < ::RuntimeError
4
+ end
3
5
  class DuplicateKeyError < RuntimeError
4
6
  def initialize(key:, object:)
5
7
  super("Duplicate key for #{key.inspect} in #{object}")
6
8
  end
7
9
  end
10
+ class MissingTableError < RuntimeError
11
+ def initialize(name:)
12
+ super(%(Missing table "#{name}"))
13
+ end
14
+ end
15
+ class MissingTableEntryError < RuntimeError
16
+ def initialize(table_name:, index:)
17
+ super(%(Missing index "#{index}" for table "#{table_name}"))
18
+ end
19
+ end
8
20
  end
9
21
  end
@@ -1,4 +1,7 @@
1
+ require 'forwardable'
2
+ require 'gm/notepad/exceptions'
1
3
  require "gm/notepad/input_handlers/default_handler"
4
+ require "gm/notepad/parameters/table_lookup"
2
5
  module Gm
3
6
  module Notepad
4
7
  module InputHandlers
@@ -14,11 +17,6 @@ module Gm
14
17
  true
15
18
  end
16
19
 
17
- WITH_GREP_REGEXP = %r{(?<declaration>\/(?<grep>[^\/]+)/)}
18
- WITH_INDEX_REGEXP = %r{(?<declaration>\[(?<index>[^\]]+)\])}
19
- WITH_EMPTY_INDEX_REGEX = %r{(?<declaration>\[\])}
20
- WITH_EMPTY_GREP_REGEX = %r{(?<declaration>\/\/)}
21
- NON_EXPANDING_CHARATER = '!'.freeze
22
20
  def after_initialize!
23
21
  self.expand_line = false
24
22
  self.to_output = false
@@ -26,30 +24,16 @@ module Gm
26
24
 
27
25
  line = input[1..-1].to_s
28
26
  self.expand_line = false
29
- if match = WITH_EMPTY_INDEX_REGEX.match(line)
30
- line = line.sub(match[:declaration], '')
31
- elsif match = WITH_INDEX_REGEXP.match(line)
32
- line = line.sub(match[:declaration], '')
33
- self.index = match[:index]
34
- elsif match = WITH_EMPTY_GREP_REGEX.match(line)
35
- line = line.sub(match[:declaration], '')
36
- elsif match = WITH_GREP_REGEXP.match(line)
37
- line = line.sub(match[:declaration], '')
38
- grep = match[:grep]
39
- self.grep = grep
40
- end
41
- if line[-1] == NON_EXPANDING_CHARATER
42
- line = line[0..-2]
43
- end
44
- self.table_name = line.downcase
27
+ @table_lookup_parameters = Parameters::TableLookup.new(text: line)
45
28
  end
46
29
 
47
- attr_accessor :index, :grep, :table_name
30
+ extend Forwardable
31
+ def_delegators :@table_lookup_parameters, :index, :grep, :table_name
48
32
 
49
33
  def lines
50
34
  begin
51
35
  table = table_registry.fetch_table(name: table_name)
52
- rescue KeyError
36
+ rescue MissingTableError
53
37
  message = "Unknown table #{table_name.inspect}. Did you mean: "
54
38
  message += table_registry.table_names.grep(/\A#{table_name}/).map(&:inspect).join(", ")
55
39
  return [message]
@@ -57,7 +41,7 @@ module Gm
57
41
  if index
58
42
  begin
59
43
  [table.lookup(index: index)]
60
- rescue KeyError
44
+ rescue MissingTableEntryError
61
45
  [%(Entry with index "#{index}" not found in "#{table_name}" table)]
62
46
  end
63
47
  elsif grep
@@ -1,27 +1,49 @@
1
1
  require 'dice'
2
+ require 'gm/notepad/parameters/table_lookup'
2
3
  module Gm
3
4
  module Notepad
4
5
  # Responsible for recording entries and then dumping them accordingly.
5
6
  class LineEvaluator
7
+ def initialize(table_registry:)
8
+ @table_registry = table_registry
9
+ end
10
+ attr_reader :table_registry
11
+
6
12
  TABLE_NAME_REGEXP = %r{(?<table_name_container>\{(?<table_name>[^\{\}]+)\})}
7
- DICE_REGEXP = %r{(?<dice_container>\[(?<dice>[^\]]+)\])}
8
- def call(line:, table_lookup_function:, expand_line: true)
13
+ def call(line:, expand_line: true)
9
14
  return line unless expand_line
10
- match = line.match(TABLE_NAME_REGEXP)
11
- while match
12
- evaluated_table_name = table_lookup_function.call(table_name: match[:table_name].strip)
13
- line = line.sub(match[:table_name_container], evaluated_table_name)
14
- match = line.match(TABLE_NAME_REGEXP)
15
+ text = parse_table(text: line)
16
+ parse_dice(text: text)
17
+ end
18
+
19
+ private
20
+
21
+ def parse_table(text:)
22
+ while match = text.match(TABLE_NAME_REGEXP)
23
+ table_lookup = Parameters::TableLookup.new(text: match[:table_name].strip)
24
+ if table_lookup.index
25
+ if index = Dice.parse(table_lookup.index)
26
+ table_lookup.index = index.evaluate
27
+ end
28
+ entry = table_registry.lookup(index: table_lookup.index, table_name: table_lookup.table_name)
29
+ else
30
+ entry = table_registry.lookup(table_name: table_lookup.table_name)
31
+ end
32
+ text = text.sub(match[:table_name_container], entry)
15
33
  end
16
- while match = line.match(DICE_REGEXP)
34
+ text
35
+ end
36
+ DICE_REGEXP = %r{(?<dice_container>\[(?<dice>[^\]]+)\])}
37
+ def parse_dice(text:)
38
+ while match = text.match(DICE_REGEXP)
17
39
  if parsed_dice = Dice.parse(match[:dice])
18
40
  evaluated_dice = "#{parsed_dice.evaluate}"
19
41
  else
20
42
  evaluated_dice = "(#{match[:dice]})"
21
43
  end
22
- line = line.sub(match[:dice_container], evaluated_dice)
44
+ text = text.sub(match[:dice_container], evaluated_dice)
23
45
  end
24
- line
46
+ text
25
47
  end
26
48
  end
27
49
  end
@@ -0,0 +1,45 @@
1
+ module Gm
2
+ module Notepad
3
+ module Parameters
4
+ # Responsible for teasing apart the table logic
5
+ class TableLookup
6
+ WITH_GREP_REGEXP = %r{(?<declaration>\/(?<grep>[^\/]+)/)}
7
+ WITH_INDEX_REGEXP = %r{(?<declaration>\[(?<index>[^\]]+)\])}
8
+ WITH_EMPTY_INDEX_REGEX = %r{(?<declaration>\[\])}
9
+ WITH_EMPTY_GREP_REGEX = %r{(?<declaration>\/\/)}
10
+
11
+ def initialize(text:)
12
+ @text = text
13
+ extract_parameters!
14
+ end
15
+
16
+ attr_accessor :index, :grep, :table_name
17
+
18
+ def parameters
19
+ parameters = { table_name: table_name }
20
+ parameters[:grep] = grep if grep
21
+ parameters[:index] = index if index
22
+ parameters
23
+ end
24
+
25
+ private
26
+ def extract_parameters!
27
+ @parameters = {}
28
+ text = @text
29
+ if match = WITH_EMPTY_INDEX_REGEX.match(text)
30
+ text = text.sub(match[:declaration], '')
31
+ elsif match = WITH_INDEX_REGEXP.match(text)
32
+ text = text.sub(match[:declaration], '')
33
+ @index = match[:index]
34
+ elsif match = WITH_EMPTY_GREP_REGEX.match(text)
35
+ text = text.sub(match[:declaration], '')
36
+ elsif match = WITH_GREP_REGEXP.match(text)
37
+ text = text.sub(match[:declaration], '')
38
+ @grep = match[:grep]
39
+ end
40
+ @table_name = text.downcase
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -10,7 +10,11 @@ module Gm
10
10
 
11
11
  def lookup(index: false)
12
12
  if index
13
- @table.fetch(index)
13
+ begin
14
+ @table.fetch(index.to_s)
15
+ rescue KeyError
16
+ raise MissingTableEntryError.new(table_name: table_name, index: index.to_s)
17
+ end
14
18
  else
15
19
  @table.values[random_index]
16
20
  end
@@ -14,7 +14,7 @@ module Gm
14
14
 
15
15
  Configuration.init!(target: self, from_config: [:paths, :table_extension, :filesystem_directory]) do
16
16
  @registry = {}
17
- @line_evaluator = LineEvaluator.new
17
+ @line_evaluator = LineEvaluator.new(table_registry: self)
18
18
  end
19
19
 
20
20
  def load!
@@ -34,13 +34,15 @@ module Gm
34
34
 
35
35
  def fetch_table(name:)
36
36
  registry.fetch(name.downcase)
37
+ rescue KeyError
38
+ raise MissingTableError.new(name: name.downcase)
37
39
  end
38
40
 
39
41
  def append(table_name:, line:, write:)
40
42
  table = nil
41
43
  begin
42
44
  table = fetch_table(name: table_name)
43
- rescue KeyError
45
+ rescue MissingTableError
44
46
  filename = File.join(filesystem_directory, "#{table_name}#{table_extension}")
45
47
  table = register(table_name: table_name, lines: [], filename: filename)
46
48
  end
@@ -57,14 +59,19 @@ module Gm
57
59
  end
58
60
 
59
61
  def lookup(table_name:, **kwargs)
60
- table = fetch_table(name: table_name)
61
- table.lookup(**kwargs)
62
- rescue KeyError
63
- "(undefined #{table_name})"
62
+ # TODO: Push this onto the table, as it removes nosy neighbor syndrom
63
+ begin
64
+ table = fetch_table(name: table_name)
65
+ table.lookup(**kwargs)
66
+ rescue MissingTableError
67
+ "(undefined table_name: #{table_name.inspect})"
68
+ rescue KeyError
69
+ "(missing entry for #{kwargs.inspect})"
70
+ end
64
71
  end
65
72
 
66
73
  def evaluate(line:)
67
- line_evaluator.call(line: line, table_lookup_function: method(:lookup))
74
+ line_evaluator.call(line: line)
68
75
  end
69
76
 
70
77
  private
@@ -1,5 +1,5 @@
1
1
  module Gm
2
2
  module Notepad
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gm-notepad
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Friesen
@@ -159,6 +159,7 @@ files:
159
159
  - lib/gm/notepad/line_evaluator.rb
160
160
  - lib/gm/notepad/line_renderer.rb
161
161
  - lib/gm/notepad/pad.rb
162
+ - lib/gm/notepad/parameters/table_lookup.rb
162
163
  - lib/gm/notepad/readline.rb
163
164
  - lib/gm/notepad/table.rb
164
165
  - lib/gm/notepad/table_entry.rb