tmux-erb-parser 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1609219d3c43082d5419e05ce2b64430b0524f0b6b09acb1e447e1efd4a5f243
4
- data.tar.gz: 841d87ef8ccf1c9d618a301df1de3248cff2bac12d790f4ce8025c679fbcc626
3
+ metadata.gz: 7da9078d32cc8302a89b36462a0cd9bc9329679e205e8be50cfdc0e839b019e0
4
+ data.tar.gz: c3ef1b2b8c8105d7f4a32515853920aabb72f337a009bfae36a2c8840a04f624
5
5
  SHA512:
6
- metadata.gz: ab1579f7d6bd609b12cfc58423e612595dbd534af28335ac7574766234fd6faa40dcf1f00695fb5afcfd8ae00bdc6e7f28077d960f87e7f91172cf527be7632f
7
- data.tar.gz: e52c39ca365fc048fbdf0b4b2547ab76196b9c634c6de3b3b6f473512c11519fb9549ef4d52f3aee646f3f710a1f6e91ccdf13bfbedc66d070a1af4fbbab3d32
6
+ metadata.gz: 7150f31dfd4bef069c6799cc25cdcd28c529eff87746e774f5560eb4a611c69c3306c9c9a15120ffc5e534b1c672c87e96f65846624e452916ddf0703ecc0133
7
+ data.tar.gz: 300d0087b6e77ae478d31ddb57d441e68536c61b4f31dbeb2aa1fce560d9fbf55fce66e1da414b4b27db77d44f088fba71d3a7e9d572edfba74f4323efd07d9d
data/README.md CHANGED
@@ -20,7 +20,7 @@ A Tmux plugin to load tmux.conf.erb
20
20
  2. Install tmux-erb-parser and run tmux!
21
21
 
22
22
  ### Install with tpm (Tmux Plugin Manager)
23
- * Put this at the bottom of `~/.tmux.conf` (**Not your tmux.conf.erb !**):
23
+ * Put this at the bottom of `~/.tmux.conf` (**Not your tmux.conf.erb!**):
24
24
  ```tmux
25
25
  setenv -g TMUX_CONF_EXT_PATH "path/to/tmux.conf.erb" # set your tmux.conf.erb's path
26
26
  # Note: You can specify multiple files using glob expressions. This is parsed by bash.
@@ -35,16 +35,21 @@ A Tmux plugin to load tmux.conf.erb
35
35
 
36
36
  * Run tmux and press `Prefix + I` to install plugins!
37
37
 
38
- ### Install and configure with git
38
+ ### Install and configure with git/rubygems
39
39
  * Install:
40
- ```bash
41
- git clone https://github.com/epaew/tmux-erb-parser ~/.tmux/plugins/tmux-erb-parser
42
- ```
40
+ * with git:
41
+ ```bash
42
+ git clone https://github.com/epaew/tmux-erb-parser ~/.tmux/plugins/tmux-erb-parser
43
+ ```
44
+ * from rubygems:
45
+ ```bash
46
+ gem install tmux-erb-parser
47
+ ```
43
48
 
44
49
  * Configure:
45
- * Put this in `~/.tmux.conf`:
50
+ * Put this in `~/.tmux.conf` (**Not your tmux.conf.erb!**):
46
51
  ```tmux
47
- run '~/.tmux/plugins/tmux-erb-parser/bin/tmux-erb-parser --inline path/to/tmux.conf.erb'
52
+ run 'path/to/bin/tmux-erb-parser --inline path/to/tmux.conf.erb'
48
53
  ```
49
54
 
50
55
  ## License
data/bin/tmux-erb-parser CHANGED
@@ -1,8 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- # vim:set ft=ruby:
5
-
6
4
  $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
7
5
  require 'tmux-erb-parser'
8
6
 
@@ -34,22 +34,6 @@ module TmuxERBParser
34
34
  raise ArgumentError, msg if msg
35
35
  end
36
36
 
37
- def process
38
- check_args
39
-
40
- args = @args.dup
41
- args.each do |arg|
42
- @logger.info "open #{arg}."
43
- File.open(arg, 'r') do |input|
44
- p = Parser.new(input,
45
- @options[:output],
46
- File.extname(arg.downcase)[1..-1].to_sym,
47
- @options)
48
- p.parse
49
- end
50
- end
51
- end
52
-
53
37
  def command_opts(opts) # rubocop:disable Metrics/MethodLength
54
38
  opts.banner = "Usage: #{@command_name} INPUT_FILES [options]"
55
39
 
@@ -78,5 +62,41 @@ module TmuxERBParser
78
62
  exit
79
63
  end
80
64
  end
65
+
66
+ def exec(commands, output = nil)
67
+ if output
68
+ commands.each(&output.method(:puts))
69
+ else
70
+ commands.inject(+'', &method(:exec_tmux))
71
+ end
72
+ end
73
+
74
+ def exec_tmux(buf, line)
75
+ buf << line
76
+ return buf if buf.empty?
77
+
78
+ if buf.end_with?('\\')
79
+ buf.chop!
80
+ else
81
+ command = "tmux #{buf.gsub(%(\\;), %( '\\;'))}"
82
+ @logger.debug "exec: #{command}"
83
+
84
+ `#{command}` unless ENV['DEBUG']
85
+ +''
86
+ end
87
+ end
88
+
89
+ def process
90
+ check_args
91
+
92
+ @args.each do |arg|
93
+ @logger.info "open #{arg}."
94
+ File.open(arg, 'r') do |input|
95
+ p = Parser.new(input, File.extname(arg.downcase)[1..-1].to_sym)
96
+ commands = p.parse(@options[:output].nil?)
97
+ exec(commands, @options[:output])
98
+ end
99
+ end
100
+ end
81
101
  end
82
102
  end
@@ -10,54 +10,33 @@ module TmuxERBParser
10
10
  class ParseError < StandardError; end
11
11
 
12
12
  class Parser
13
- def initialize(input, output, type = :erb, options = {})
14
- @input = input
15
- @output = output
13
+ def initialize(input, type = :erb)
14
+ @input = case input
15
+ when IO then input.read
16
+ when Array then input.join('\n')
17
+ when String then input
18
+ else raise ArgumentError
19
+ end
16
20
  @type = type
17
- @options = options
18
- @logger = Logger.instance
19
21
  end
20
22
 
21
- def parse
22
- conf_lines = parse_file(@input, @type)
23
- converted_lines = conf_lines.map(&method(:replace_source_file))
24
- exec(converted_lines, @output)
25
- end
26
-
27
- private
28
-
29
- def exec(commands, output = nil)
30
- if output
31
- commands.each(&output.method(:puts))
32
- else
33
- commands.inject(+'', &method(:exec_tmux))
23
+ def parse(strip_comments = false)
24
+ parse_string(@input, @type).map do |line|
25
+ line = replace_source_file(line)
26
+ line = strip_comment(line) if strip_comments
27
+ line
34
28
  end
35
29
  end
36
30
 
37
- def exec_tmux(buf, line)
38
- buf = +buf if buf.frozen?
39
- buf << line
40
- buf = strip_comments(buf)
41
- return buf if buf.empty?
42
-
43
- if buf.end_with?('\\')
44
- buf.chop!
45
- else
46
- command = "tmux #{buf.gsub(%(\\;), %( '\\;'))}"
47
- @logger.debug "exec: #{command}"
48
-
49
- `#{command}` unless ENV['DEBUG']
50
- buf.clear
51
- end
52
- end
31
+ private
53
32
 
54
33
  def generate_conf(_structured)
55
34
  # TODO
56
35
  parse_result = []
57
36
  end
58
37
 
59
- def parse_file(input, type)
60
- erb_result = ERB.new(input.read).result
38
+ def parse_string(input, type)
39
+ erb_result = ERB.new(input).result
61
40
 
62
41
  case type
63
42
  when :json
@@ -88,32 +67,35 @@ module TmuxERBParser
88
67
  line
89
68
  end
90
69
 
91
- def strip_comments(str)
70
+ def strip_comment(str)
92
71
  return '' if str.empty? || str.lstrip.start_with?('#')
93
72
 
94
- strip_eol_comments(str)
73
+ strip_eol_comment(str)
95
74
  end
96
75
 
97
- # TODO: refactoring
98
- def strip_eol_comments(str)
76
+ def strip_eol_comment(str)
99
77
  flags = {}
100
78
  str = str.each_char.inject(+'') do |result, char|
101
- case char
102
- when '\''
103
- if !flags[:double] || (flags[:single] && result[-1] != '\\')
104
- flags[:single] = !flags[:single]
105
- end
106
- when '"'
107
- if !flags[:single] || (flags[:double] && result[-1] != '\\')
108
- flags[:double] = !flags[:double]
109
- end
110
- when '#'
111
- break result if flags.values.none?
112
- end
79
+ flags = update_flags(flags, char)
80
+ break result if char == '#' && flags.values.none?
113
81
 
114
82
  result << char
115
83
  end
116
84
  str.rstrip
117
85
  end
86
+
87
+ def update_flags(current_flags, char)
88
+ result = current_flags.dup
89
+ result.delete(:'\\')
90
+
91
+ case char
92
+ when '\\'
93
+ result[char.to_sym] = !current_flags[char.to_sym]
94
+ when '\'', '"'
95
+ result[char.to_sym] =
96
+ current_flags.delete(char.to_sym) ^ current_flags.values.none?
97
+ end
98
+ result
99
+ end
118
100
  end
119
101
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TmuxERBParser
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tmux-erb-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - epaew
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-15 00:00:00.000000000 Z
11
+ date: 2018-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler