Module: Ned

Defined in:
lib/ned.rb,
lib/ned_old.rb,
lib/ned/command.rb,
lib/ned/strings.rb,
lib/ned/version.rb,
lib/ned/commands/cut.rb,
lib/ned/commands/eval.rb,
lib/ned/commands/grep.rb,
lib/ned/commands/head.rb,
lib/ned/commands/join.rb,
lib/ned/commands/read.rb,
lib/ned/commands/sort.rb,
lib/ned/commands/tail.rb,
lib/ned/commands/uniq.rb,
lib/ned/commands/index.rb,
lib/ned/commands/print.rb,
lib/ned/commands/quote.rb,
lib/ned/commands/append.rb,
lib/ned/command_registry.rb,
lib/ned/commands/prepend.rb,
lib/ned/commands/backward.rb

Overview

todo

- Add ability to check whether there are more lines.
- add replace
- add count or something like wc
- implement inline editing
- implement highlighting
- maybe add append and prepend
- figure out how to distrute as gem without bundle
- add support for a ~/.ned/ dir
- implement highlight
- add color
- add support for more escape sequences, see https://github.com/ruby/ruby/blob/master/doc/syntax/literals.rdoc#label-Strings
- add support for editing files in place
- add ljust
- maybe add ability to highlight columns, not just full lines
- write readme
- add support for to_set in uniq
- add support for overriding existing commands
- index shouldn't always be an all command. It should be based on the indices.
- add a way to operate on input files separately, one at a time.
- maybe add --all to eval to operate on all lines at once
- write more tests:
  - '\:'
  - duplicate names
  - eval
  - no short name or no long name
- maybe Command should provide an option to ensure the trailing newline is preserved rather than each command doing it alone.
- add delete to delete strings

Defined Under Namespace

Classes: Append, Backward, Command, CommandRegistry, Cut, Eval, Grep, Head, Index, Join, Main, Prepend, Print, Quote, Read, Sort, Strings, Tail, Uniq

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.failure(error_output, message) ⇒ Object



40
41
42
43
# File 'lib/ned_old.rb', line 40

def self.failure(error_output, message)
  error_output.puts "fatal: #{message}\n\n"
  help(error_output)
end

.help(error_output) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ned_old.rb', line 13

def self.help(error_output)
  error_output.puts "    A stream editor.\n\n    Usage: ned [file] [options ...]\n\n    Options:\n  EOF\n\n  lines = [\n    ['    --help',    'Print this message.'],\n    ['    --version', 'Print version.']\n  ]\n  max = lines[0][0].length\n  Ned::Config.all.each do |config|\n    next if config.hidden?\n\n    line = \"\#{config.short}, \#{config.long} \#{config.params}\"\n    lines << [line, config.description]\n    max = [max, line.length].max\n  end\n\n  lines.each do |line|\n    error_output.puts \"    \#{line[0].ljust(max)}  \#{line[1]}\"\n  end\nend\n"

.process_all(commands, lines, output) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ned_old.rb', line 150

def self.process_all(commands, lines, output)
  final_newline = lines[-1]&.[](-1) == "\n" ? "\n" : ''

  lines = lines.map { |line| line.chomp("\n") }
  context = Context.new(lines, 0)
  commands.each do |command|
    command.execute(context)
  end
  last = context.lines.length - 1
  context.lines.each_with_index do |line, index|
    output.print "#{line}#{index == last ? final_newline : "\n"}"
  end
end

.process_each(commands, inputs, output) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ned_old.rb', line 164

def self.process_each(commands, inputs, output)
  input = inputs[0]

  if input.is_a? String
    input = File.open(input)
  end

  current = input.gets(sep="\n")
  next_line = input.gets(sep="\n")
  index = 0

  while current
    final_newline = current[-1]&.[](-1) == "\n" ? "\n" : ''
    context = Context.new([current.chomp("\n")], index)
    commands.each do |command|
      command.execute(context)
    end
    index = context.start_index + context.lines.size
    last = context.lines.length - 1
    context.lines.each_with_index do |line, index|
      output.print "#{line}#{index == last && next_line.nil? ? final_newline : "\n"}"
    end

    current = next_line
    next_line = input.gets(sep="\n")
  end

  input.close
end

.run(input: $stdin, output: $stdout, error_output: $stderr, args: ARGV) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ned_old.rb', line 45

def self.run(input: $stdin, output: $stdout, error_output: $stderr, args: ARGV)
  args = args.map do |arg|
    Ned::Strings.unescape!(arg.dup)
  end

  if args.include?('--help')
    help(error_output)
    return 0
  end

  if args.include?('--version')
    output.puts("Ned version #{Ned::VERSION}")
    return 0
  end

  inputs = []
  while args.first && !args.first.match(/^-./)
    arg = args.shift

    if arg == '-'
      inputs << input
    else
      unless File.exist?(arg)
        failure(error_output, "no such file: #{arg}")
        return 1
      end
      inputs << arg
    end
  end

  if input.tty? && (inputs.empty? || (inputs.size == 1 && inputs[0] == input))
    failure(error_output, 'no input')
    return 1
  end

  inputs = inputs.unshift(input) if !input.tty? && !inputs.index(input)

  if inputs.count(input) > 1
    failure(error_output, "can't read from standard in twice, silly")
    return 1
  end

  if inputs.size > 1
    failure(error_output, "more than one input not yet supported")
    return 1
  end

  commands = []

  while arg = args.shift
    config = Ned::Config.all.find do |config|
      arg.start_with?(config.short) || arg == config.long || arg.start_with?("#{config.long}=")
    end

    unless config
      failure(error_output, "invalid option: #{arg}")
      return 1
    end

    params = []

    if arg.start_with?("#{config.long}=")
      params << arg["#{config.long}=".size..-1]
    elsif arg.start_with?(config.short) && arg != config.short
      params << arg[config.short.size..-1]
    else
      while !args.empty? && !args.first.start_with?('-')
        arg = args.shift
        if arg.start_with? '\\-'
          arg = arg[1..-1]
        end
        params << arg
      end
    end

    valid = config.validate(*params)
    if valid.nil?
      if config.params.nil?
        failure(error_output, "invalid param for #{config.name}. found: #{params}, expected none")
        return 1
      else
        failure(error_output, "invalid param for #{config.name}. found: #{params}, expected: #{config.params}")
        return 1
      end
    end

    commands << Ned::Command.new(config, valid)
  end

  if commands.any?(&:all?)
    lines = inputs.flat_map do |i|
      if i.is_a? String
        IO.readlines(i, sep="\n")
      else
        i.readlines(sep="\n")
      end
    end
    process_all(commands, lines, output)
  else
    process_each(commands, inputs, output)
  end

  0
end