Class: Coopy::Coopy

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/coopy/coopy.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = nil) ⇒ Coopy

Returns a new instance of Coopy.



7
8
9
10
# File 'lib/lib/coopy/coopy.rb', line 7

def initialize(io = nil)
  self.init
  @io = io
end

Class Attribute Details

.versionObject

Returns the value of attribute version.



880
881
882
# File 'lib/lib/coopy/coopy.rb', line 880

def version
  @version
end

Instance Attribute Details

#cache_txtObject

Returns the value of attribute cache_txt.



28
29
30
# File 'lib/lib/coopy/coopy.rb', line 28

def cache_txt
  @cache_txt
end

#css_outputObject

Returns the value of attribute css_output.



25
26
27
# File 'lib/lib/coopy/coopy.rb', line 25

def css_output
  @css_output
end

#csv_eol_preferenceObject

Returns the value of attribute csv_eol_preference.



16
17
18
# File 'lib/lib/coopy/coopy.rb', line 16

def csv_eol_preference
  @csv_eol_preference
end

#daff_cmdObject

Returns the value of attribute daff_cmd.



344
345
346
# File 'lib/lib/coopy/coopy.rb', line 344

def daff_cmd
  @daff_cmd
end

#delim_preferenceObject

Returns the value of attribute delim_preference.



15
16
17
# File 'lib/lib/coopy/coopy.rb', line 15

def delim_preference
  @delim_preference
end

#extern_preferenceObject

Returns the value of attribute extern_preference.



17
18
19
# File 'lib/lib/coopy/coopy.rb', line 17

def extern_preference
  @extern_preference
end

#flagsObject

Returns the value of attribute flags.



27
28
29
# File 'lib/lib/coopy/coopy.rb', line 27

def flags
  @flags
end

#format_preferenceObject

protected - in ruby this doesn’t play well with static/inline methods



14
15
16
# File 'lib/lib/coopy/coopy.rb', line 14

def format_preference
  @format_preference
end

#fragmentObject

Returns the value of attribute fragment.



26
27
28
# File 'lib/lib/coopy/coopy.rb', line 26

def fragment
  @fragment
end

#ioObject

Returns the value of attribute io.



23
24
25
# File 'lib/lib/coopy/coopy.rb', line 23

def io
  @io
end

#mvObject

Returns the value of attribute mv.



29
30
31
# File 'lib/lib/coopy/coopy.rb', line 29

def mv
  @mv
end

#nested_outputObject

Returns the value of attribute nested_output.



20
21
22
# File 'lib/lib/coopy/coopy.rb', line 20

def nested_output
  @nested_output
end

#order_preferenceObject

Returns the value of attribute order_preference.



22
23
24
# File 'lib/lib/coopy/coopy.rb', line 22

def order_preference
  @order_preference
end

#order_setObject

Returns the value of attribute order_set.



21
22
23
# File 'lib/lib/coopy/coopy.rb', line 21

def order_set
  @order_set
end

#output_formatObject

Returns the value of attribute output_format.



18
19
20
# File 'lib/lib/coopy/coopy.rb', line 18

def output_format
  @output_format
end

#output_format_setObject

Returns the value of attribute output_format_set.



19
20
21
# File 'lib/lib/coopy/coopy.rb', line 19

def output_format_set
  @output_format_set
end

#statusObject

protected - in ruby this doesn’t play well with static/inline methods



343
344
345
# File 'lib/lib/coopy/coopy.rb', line 343

def status
  @status
end

#strategyObject

Returns the value of attribute strategy.



24
25
26
# File 'lib/lib/coopy/coopy.rb', line 24

def strategy
  @strategy
end

Class Method Details

.align(local, remote, flags, comp) ⇒ Object



927
928
929
930
931
932
933
934
935
936
# File 'lib/lib/coopy/coopy.rb', line 927

def Coopy.align(local,remote,flags,comp)
  comp.a = ::Coopy::Coopy.tablify(local)
  comp.b = ::Coopy::Coopy.tablify(remote)
  flags = ::Coopy::CompareFlags.new if flags == nil
  comp.compare_flags = flags
  ct = ::Coopy::CompareTable.new(comp)
  align = ct.align
  td = ::Coopy::TableDiff.new(align,flags)
  td
end

.cell_for(x) ⇒ Object



983
984
985
# File 'lib/lib/coopy/coopy.rb', line 983

def Coopy.cell_for(x)
  x
end

.compare_tables(local, remote, flags = nil) ⇒ Object



945
946
947
948
949
950
951
952
# File 'lib/lib/coopy/coopy.rb', line 945

def Coopy.compare_tables(local,remote,flags = nil)
  comp = ::Coopy::TableComparisonState.new
  comp.a = ::Coopy::Coopy.tablify(local)
  comp.b = ::Coopy::Coopy.tablify(remote)
  comp.compare_flags = flags
  ct = ::Coopy::CompareTable.new(comp)
  ct
end

.compare_tables3(parent, local, remote, flags = nil) ⇒ Object



954
955
956
957
958
959
960
961
962
# File 'lib/lib/coopy/coopy.rb', line 954

def Coopy.compare_tables3(parent,local,remote,flags = nil)
  comp = ::Coopy::TableComparisonState.new
  comp.p = ::Coopy::Coopy.tablify(parent)
  comp.a = ::Coopy::Coopy.tablify(local)
  comp.b = ::Coopy::Coopy.tablify(remote)
  comp.compare_flags = flags
  ct = ::Coopy::CompareTable.new(comp)
  ct
end

.diff(local, remote, flags = nil) ⇒ Object



906
907
908
909
910
911
912
913
914
915
# File 'lib/lib/coopy/coopy.rb', line 906

def Coopy.diff(local,remote,flags = nil)
  comp = ::Coopy::TableComparisonState.new
  td = ::Coopy::Coopy.align(local,remote,flags,comp)
  o = ::Coopy::Coopy.get_blank_table(td,comp)
  o = comp.a.create if comp.a != nil
  o = comp.b.create if o == nil && comp.b != nil
  o = ::Coopy::SimpleTable.new(0,0) if o == nil
  td.hilite(o)
  o
end

.diff_as_ansi(local, remote, flags = nil) ⇒ Object



897
898
899
900
901
902
903
904
# File 'lib/lib/coopy/coopy.rb', line 897

def Coopy.diff_as_ansi(local,remote,flags = nil)
  tool = ::Coopy::Coopy.new(::Coopy::TableIO.new)
  tool.cache_txt = ""
  flags = ::Coopy::CompareFlags.new if flags == nil
  tool.output_format = "csv"
  tool.run_diff(flags.parent,local,remote,flags,nil)
  tool.cache_txt
end

.diff_as_html(local, remote, flags = nil) ⇒ Object



884
885
886
887
888
889
890
891
892
893
894
895
# File 'lib/lib/coopy/coopy.rb', line 884

def Coopy.diff_as_html(local,remote,flags = nil)
  comp = ::Coopy::TableComparisonState.new
  td = ::Coopy::Coopy.align(local,remote,flags,comp)
  o = ::Coopy::Coopy.get_blank_table(td,comp)
  o = comp.a.create if comp.a != nil
  o = comp.b.create if o == nil && comp.b != nil
  o = ::Coopy::SimpleTable.new(0,0) if o == nil
  os = ::Coopy::Tables.new(o)
  td.hilite_with_nesting(os)
  render = ::Coopy::DiffRender.new
  render.render_tables(os).html
end

.get_blank_table(td, comp) ⇒ Object

protected - in ruby this doesn’t play well with static/inline methods



919
920
921
922
923
924
925
# File 'lib/lib/coopy/coopy.rb', line 919

def Coopy.get_blank_table(td,comp)
  o = nil
  o = comp.a.create if comp.a != nil
  o = comp.b.create if o == nil && comp.b != nil
  o = ::Coopy::SimpleTable.new(0,0) if o == nil
  o
end

.jsonify(t) ⇒ Object



1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
# File 'lib/lib/coopy/coopy.rb', line 1026

def Coopy.jsonify(t)
  workbook = {}
  sheet = Array.new
  w = t.get_width
  h = t.get_height
  txt = ""
  begin
    _g = 0
    while(_g < h) 
      y = _g
      _g+=1
      row = Array.new
      begin
        _g1 = 0
        while(_g1 < w) 
          x = _g1
          _g1+=1
          v = t.get_cell(x,y)
          row.push(v)
        end
      end
      sheet.push(row)
    end
  end
  workbook["sheet"] = sheet
  workbook
end

.keep_aroundObject

protected - in ruby this doesn’t play well with static/inline methods



966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
# File 'lib/lib/coopy/coopy.rb', line 966

def Coopy.keep_around 
  st = ::Coopy::SimpleTable.new(1,1)
  v = ::Coopy::Viterbi.new
  td = ::Coopy::TableDiff.new(nil,nil)
  cf = ::Coopy::CompareFlags.new
  idx = ::Coopy::Index.new(cf)
  dr = ::Coopy::DiffRender.new
  hp = ::Coopy::HighlightPatch.new(nil,nil)
  csv = ::Coopy::Csv.new
  tm = ::Coopy::TableModifier.new(nil)
  sc = ::Coopy::SqlCompare.new(nil,nil,nil,nil)
  sq = ::Coopy::SqliteHelper.new
  sm = ::Coopy::SimpleMeta.new(nil)
  ct = ::Coopy::CombinedTable.new(nil)
  0
end

.mainObject



989
990
991
992
993
994
995
# File 'lib/lib/coopy/coopy.rb', line 989

def Coopy.main 
  io = ::Coopy::TableIO.new
  coopy1 = ::Coopy::Coopy.new
  ret = coopy1.coopyhx(io)
  HxSys.exit(ret) if ret != 0
  ret
end

.patch(local, patch, flags = nil) ⇒ Object



940
941
942
943
# File 'lib/lib/coopy/coopy.rb', line 940

def Coopy.patch(local,patch,flags = nil)
  patcher = ::Coopy::HighlightPatch.new(::Coopy::Coopy.tablify(local),::Coopy::Coopy.tablify(patch))
  patcher.apply
end

.show(t) ⇒ Object

protected - in ruby this doesn’t play well with static/inline methods



999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'lib/lib/coopy/coopy.rb', line 999

def Coopy.show(t)
  w = t.get_width
  h = t.get_height
  txt = ""
  begin
    _g = 0
    while(_g < h) 
      y = _g
      _g+=1
      begin
        _g1 = 0
        while(_g1 < w) 
          x = _g1
          _g1+=1
          begin
            s = t.get_cell(x,y)
            txt += s.to_s
          end
          txt += " "
        end
      end
      txt += "\n"
    end
  end
  puts txt
end

.tablify(data) ⇒ Object



1056
1057
1058
1059
1060
# File 'lib/lib/coopy/coopy.rb', line 1056

def Coopy.tablify(data)
  return data if data == nil
  return data if data.respond_to? :get_cell_view
  ::Coopy::TableView.new(data)
end

Instance Method Details

#apply_renderer(name, renderer) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/lib/coopy/coopy.rb', line 106

def apply_renderer(name,renderer)
  renderer.complete_html if !@fragment
  if @format_preference == "www" 
    @io.send_to_browser(renderer.html)
  else 
    self.save_text(name,renderer.html)
  end
  self.save_text(@css_output,renderer.sample_css) if @css_output != nil
  true
end

#check_format(name) ⇒ Object



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
# File 'lib/lib/coopy/coopy.rb', line 48

def check_format(name)
  return @format_preference if @extern_preference
  ext = ""
  if name != nil 
    pt = name.rindex(".",nil || 0) || -1
    if pt >= 0 
      begin
        _this = name[pt + 1..-1]
        ext = _this.downcase
      end
      case(ext)
      when "json"
        @format_preference = "json"
      when "ndjson"
        @format_preference = "ndjson"
      when "csv"
        @format_preference = "csv"
        @delim_preference = ","
      when "tsv"
        @format_preference = "csv"
        @delim_preference = "\t"
      when "ssv"
        @format_preference = "csv"
        @delim_preference = ";"
        @format_preference = "csv"
      when "psv"
        @format_preference = "csv"
        @delim_preference = [128169].pack("U")
      when "sqlite3"
        @format_preference = "sqlite"
      when "sqlite"
        @format_preference = "sqlite"
      when "html","htm"
        @format_preference = "html"
      when "www"
        @format_preference = "www"
      else
        ext = ""
      end
    end
  end
  @nested_output = @format_preference == "json" || @format_preference == "ndjson"
  @order_preference = !@nested_output
  ext
end

#command(io, cmd, args) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/lib/coopy/coopy.rb', line 346

def command(io,cmd,args)
  r = 0
  r = io.command(cmd,args) if io.async
  if r != 999 
    io.write_stdout("$ " + _hx_str(cmd))
    begin
      _g = 0
      while(_g < args.length) 
        arg = args[_g]
        _g+=1
        io.write_stdout(" ")
        spaced = (arg.index(" ",nil || 0) || -1) >= 0
        io.write_stdout("\"") if spaced
        io.write_stdout(arg)
        io.write_stdout("\"") if spaced
      end
    end
    io.write_stdout("\n")
  end
  r = io.command(cmd,args) if !io.async
  r
end

#coopyhx(io) ⇒ Object



872
873
874
875
876
# File 'lib/lib/coopy/coopy.rb', line 872

def coopyhx(io)
  args = io.args
  return ::Coopy::Coopy.keep_around if args[0] == "--keep"
  self.run(args,io)
end

#encode_table(name, t, render = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/lib/coopy/coopy.rb', line 135

def encode_table(name,t,render = nil)
  self.set_format(@output_format) if @output_format != "copy"
  txt = ""
  self.check_format(name)
  @format_preference = "csv" if @format_preference == "sqlite" && !@extern_preference
  if render == nil 
    if @format_preference == "csv" 
      csv = ::Coopy::Csv.new(@delim_preference,@csv_eol_preference)
      txt = csv.render_table(t)
    elsif @format_preference == "ndjson" 
      txt = ::Coopy::Ndjson.new(t).render
    elsif @format_preference == "html" || @format_preference == "www" 
      self.render_table(name,t)
      return nil
    elsif @format_preference == "sqlite" 
      @io.write_stderr("! Cannot yet output to sqlite, aborting\n")
      return ""
    else 
      value = ::Coopy::Coopy.jsonify(t)
      txt = ::Haxe::Format::JsonPrinter._print(value,nil,"  ")
    end
  else 
    txt = render.render(t)
  end
  txt
end

#get_rendererObject



100
101
102
103
104
# File 'lib/lib/coopy/coopy.rb', line 100

def get_renderer 
  renderer = ::Coopy::DiffRender.new
  renderer.use_pretty_arrows(@flags.use_glyphs)
  renderer
end

#initObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lib/coopy/coopy.rb', line 31

def init 
  @extern_preference = false
  @format_preference = nil
  @delim_preference = nil
  @csv_eol_preference = nil
  @output_format = "copy"
  @output_format_set = false
  @nested_output = false
  @order_set = false
  @order_preference = false
  @strategy = nil
  @css_output = nil
  @fragment = false
  @flags = nil
  @cache_txt = nil
end

#install_git_driver(io, formats) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/lib/coopy/coopy.rb', line 369

def install_git_driver(io,formats)
  r = 0
  if @status == nil 
    @status = {}
    @daff_cmd = ""
  end
  key = "hello"
  if !@status.include?(key) 
    io.write_stdout("Setting up git to use daff on")
    begin
      _g = 0
      while(_g < formats.length) 
        format = formats[_g]
        _g+=1
        io.write_stdout(" *." + _hx_str(format))
      end
    end
    io.write_stdout(" files\n")
    @status[key] = r
  end
  key = "can_run_git"
  if !@status.include?(key) 
    r = self.command(io,"git",["--version"])
    return r if r == 999
    @status[key] = r
    if r != 0 
      io.write_stderr("! Cannot run git, aborting\n")
      return 1
    end
    io.write_stdout("- Can run git\n")
  end
  daffs = ["daff","daff.rb","daff.py"]
  if @daff_cmd == "" 
    begin
      _g1 = 0
      while(_g1 < daffs.length) 
        daff = daffs[_g1]
        _g1+=1
        key1 = "can_run_" + _hx_str(daff)
        if !@status.include?(key1) 
          r = self.command(io,daff,["version"])
          return r if r == 999
          @status[key1] = r
          if r == 0 
            @daff_cmd = daff
            io.write_stdout("- Can run " + _hx_str(daff) + " as \"" + _hx_str(daff) + "\"\n")
            break
          end
        end
      end
    end
    if @daff_cmd == "" 
      io.write_stderr("! Cannot find daff, is it in your path?\n")
      return 1
    end
  end
  begin
    _g2 = 0
    while(_g2 < formats.length) 
      format1 = formats[_g2]
      _g2+=1
      key = "have_diff_driver_" + _hx_str(format1)
      if !@status.include?(key) 
        r = self.command(io,"git",["config","--global","--get","diff.daff-" + _hx_str(format1) + ".command"])
        return r if r == 999
        @status[key] = r
      end
      have_diff_driver = @status[key] == 0
      key = "add_diff_driver_" + _hx_str(format1)
      if !@status.include?(key) 
        r = self.command(io,"git",["config","--global","diff.daff-" + _hx_str(format1) + ".command",_hx_str(@daff_cmd) + " diff --git"])
        return r if r == 999
        io.write_stdout("- Cleared existing daff diff driver for " + _hx_str(format1) + "\n") if have_diff_driver
        io.write_stdout("- Added diff driver for " + _hx_str(format1) + "\n")
        @status[key] = r
      end
      key = "have_merge_driver_" + _hx_str(format1)
      if !@status.include?(key) 
        r = self.command(io,"git",["config","--global","--get","merge.daff-" + _hx_str(format1) + ".driver"])
        return r if r == 999
        @status[key] = r
      end
      have_merge_driver = @status[key] == 0
      key = "name_merge_driver_" + _hx_str(format1)
      if !@status.include?(key) 
        if !have_merge_driver 
          r = self.command(io,"git",["config","--global","merge.daff-" + _hx_str(format1) + ".name","daff tabular " + _hx_str(format1) + " merge"])
          return r if r == 999
        else 
          r = 0
        end
        @status[key] = r
      end
      key = "add_merge_driver_" + _hx_str(format1)
      if !@status.include?(key) 
        r = self.command(io,"git",["config","--global","merge.daff-" + _hx_str(format1) + ".driver",_hx_str(@daff_cmd) + " merge --output %A %O %A %B"])
        return r if r == 999
        io.write_stdout("- Cleared existing daff merge driver for " + _hx_str(format1) + "\n") if have_merge_driver
        io.write_stdout("- Added merge driver for " + _hx_str(format1) + "\n")
        @status[key] = r
      end
    end
  end
  if !io.exists(".git/config") 
    io.write_stderr("! This next part needs to happen in a git repository.\n")
    io.write_stderr("! Please run again from the root of a git repository.\n")
    return 1
  end
  attr = ".gitattributes"
  txt = ""
  post = ""
  if !io.exists(attr) 
    io.write_stdout("- No .gitattributes file\n")
  else 
    io.write_stdout("- You have a .gitattributes file\n")
    txt = io.get_content(attr)
  end
  need_update = false
  begin
    _g3 = 0
    while(_g3 < formats.length) 
      format2 = formats[_g3]
      _g3+=1
      if (txt.index("*." + _hx_str(format2),nil || 0) || -1) >= 0 
        io.write_stderr("- Your .gitattributes file already mentions *." + _hx_str(format2) + "\n")
      else 
        post += "*." + _hx_str(format2) + " diff=daff-" + _hx_str(format2) + "\n"
        post += "*." + _hx_str(format2) + " merge=daff-" + _hx_str(format2) + "\n"
        io.write_stdout("- Placing the following lines in .gitattributes:\n")
        io.write_stdout(post)
        txt += "\n" if txt != "" && !need_update
        txt += post
        need_update = true
      end
    end
  end
  io.save_content(attr,txt) if need_update
  io.write_stdout("- Done!\n")
  0
end

#json_to_table(json) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/lib/coopy/coopy.rb', line 222

def json_to_table(json)
  output = nil
  begin
    _g = 0
    _g1 = Reflect.fields(json)
    while(_g < _g1.length) 
      name = _g1[_g]
      _g+=1
      t = Reflect.field(json,name)
      columns = Reflect.field(t,"columns")
      next if columns == nil
      rows = Reflect.field(t,"rows")
      next if rows == nil
      output = ::Coopy::SimpleTable.new(columns.length,rows.length)
      has_hash = false
      has_hash_known = false
      begin
        _g3 = 0
        _g2 = rows.length
        while(_g3 < _g2) 
          i = _g3
          _g3+=1
          row = rows[i]
          if !has_hash_known 
            has_hash = true if Reflect.fields(row).length == columns.length
            has_hash_known = true
          end
          if !has_hash 
            lst = row
            begin
              _g5 = 0
              _g4 = columns.length
              while(_g5 < _g4) 
                j = _g5
                _g5+=1
                val = lst[j]
                output.set_cell(j,i,::Coopy::Coopy.cell_for(val))
              end
            end
          else 
            _g51 = 0
            _g41 = columns.length
            while(_g51 < _g41) 
              j1 = _g51
              _g51+=1
              val1 = Reflect.field(row,columns[j1])
              output.set_cell(j1,i,::Coopy::Coopy.cell_for(val1))
            end
          end
        end
      end
    end
  end
  output.trim_blank if output != nil
  output
end

#json_to_tables(json) ⇒ Object



216
217
218
219
220
# File 'lib/lib/coopy/coopy.rb', line 216

def json_to_tables(json)
  tables = Reflect.field(json,"tables")
  return self.json_to_table(json) if tables == nil
  ::Coopy::JsonTables.new(json,@flags)
end

#load_table(name) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/lib/coopy/coopy.rb', line 304

def load_table(name)
  ext = self.check_format(name)
  if ext == "sqlite" 
    sql = @io.open_sqlite_database(name)
    if sql == nil 
      @io.write_stderr("! Cannot open database, aborting\n")
      return nil
    end
    tab = ::Coopy::SqlTables.new(sql,@flags)
    return tab
  end
  txt = @io.get_content(name)
  if ext == "ndjson" 
    t = ::Coopy::SimpleTable.new(0,0)
    ndjson = ::Coopy::Ndjson.new(t)
    ndjson.parse(txt)
    return t
  end
  begin
    json = ::Haxe::Format::JsonParser.new(txt).parse_rec
    @format_preference = "json"
    t1 = self.json_to_tables(json)
    raise hx_raise("JSON failed") if t1 == nil
    return t1
  rescue => e
    e = hx_rescued(e)
    raise hx_raise(e) if ext == "json"
  end if ext == "json" || ext == ""
  @format_preference = "csv"
  csv = ::Coopy::Csv.new(@delim_preference)
  output = ::Coopy::SimpleTable.new(0,0)
  csv.parse_table(txt,output)
  @csv_eol_preference = csv.get_discovered_eol if @csv_eol_preference == nil
  output.trim_blank if output != nil
  output
end

#render_table(name, t) ⇒ Object



117
118
119
120
121
# File 'lib/lib/coopy/coopy.rb', line 117

def render_table(name,t)
  renderer = self.get_renderer
  renderer.render(t)
  self.apply_renderer(name,renderer)
end

#render_tables(name, t) ⇒ Object



123
124
125
126
127
# File 'lib/lib/coopy/coopy.rb', line 123

def render_tables(name,t)
  renderer = self.get_renderer
  renderer.render_tables(t)
  self.apply_renderer(name,renderer)
end

#run(args, io = nil) ⇒ Object



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
# File 'lib/lib/coopy/coopy.rb', line 512

def run(args,io = nil)
  io = ::Coopy::TableIO.new if io == nil
  if io == nil 
    puts "No system interface available"
    return 1
  end
  self.init
  @io = io
  more = true
  output = nil
  inplace = false
  git = false
  @flags = ::Coopy::CompareFlags.new
  @flags.always_show_header = true
  while(more) 
    more = false
    begin
      _g1 = 0
      _g = args.length
      while(_g1 < _g) 
        i = _g1
        _g1+=1
        tag = args[i]
        if tag == "--output" 
          more = true
          output = args[i + 1]
          args.slice!(i,2)
          break
        elsif tag == "--css" 
          more = true
          @fragment = true
          @css_output = args[i + 1]
          args.slice!(i,2)
          break
        elsif tag == "--fragment" 
          more = true
          @fragment = true
          args.slice!(i,1)
          break
        elsif tag == "--plain" 
          more = true
          @flags.use_glyphs = false
          args.slice!(i,1)
          break
        elsif tag == "--all" 
          more = true
          @flags.show_unchanged = true
          @flags.show_unchanged_columns = true
          args.slice!(i,1)
          break
        elsif tag == "--all-rows" 
          more = true
          @flags.show_unchanged = true
          args.slice!(i,1)
          break
        elsif tag == "--all-columns" 
          more = true
          @flags.show_unchanged_columns = true
          args.slice!(i,1)
          break
        elsif tag == "--act" 
          more = true
          @flags.acts = {} if @flags.acts == nil
          begin
            @flags.acts[args[i + 1]] = true
            true
          end
          args.slice!(i,2)
          break
        elsif tag == "--context" 
          more = true
          context = args[i + 1].to_i
          @flags.unchanged_context = context if context >= 0
          args.slice!(i,2)
          break
        elsif tag == "--inplace" 
          more = true
          inplace = true
          args.slice!(i,1)
          break
        elsif tag == "--git" 
          more = true
          git = true
          args.slice!(i,1)
          break
        elsif tag == "--unordered" 
          more = true
          @flags.ordered = false
          @flags.unchanged_context = 0
          @order_set = true
          args.slice!(i,1)
          break
        elsif tag == "--ordered" 
          more = true
          @flags.ordered = true
          @order_set = true
          args.slice!(i,1)
          break
        elsif tag == "--color" 
          more = true
          @flags.terminal_format = "ansi"
          args.slice!(i,1)
          break
        elsif tag == "--no-color" 
          more = true
          @flags.terminal_format = "plain"
          args.slice!(i,1)
          break
        elsif tag == "--input-format" 
          more = true
          self.set_format(args[i + 1])
          args.slice!(i,2)
          break
        elsif tag == "--output-format" 
          more = true
          @output_format = args[i + 1]
          @output_format_set = true
          args.slice!(i,2)
          break
        elsif tag == "--id" 
          more = true
          @flags.ids = Array.new if @flags.ids == nil
          @flags.ids.push(args[i + 1])
          args.slice!(i,2)
          break
        elsif tag == "--ignore" 
          more = true
          @flags.ignore_column(args[i + 1])
          args.slice!(i,2)
          break
        elsif tag == "--index" 
          more = true
          @flags.always_show_order = true
          @flags.never_show_order = false
          args.slice!(i,1)
          break
        elsif tag == "--www" 
          more = true
          @output_format = "www"
          @output_format_set = true
          args.slice!(i,1)
        elsif tag == "--table" 
          more = true
          @flags.add_table(args[i + 1])
          args.slice!(i,2)
          break
        elsif tag == "-w" || tag == "--ignore-whitespace" 
          more = true
          @flags.ignore_whitespace = true
          args.slice!(i,1)
          break
        elsif tag == "-i" || tag == "--ignore-case" 
          more = true
          @flags.ignore_case = true
          args.slice!(i,1)
          break
        elsif tag == "--padding" 
          more = true
          @flags.padding_strategy = args[i + 1]
          args.slice!(i,2)
          break
        elsif tag == "-e" || tag == "--eol" 
          more = true
          ending = args[i + 1]
          if ending == "crlf" 
            ending = "\r\n"
          elsif ending == "lf" 
            ending = "\n"
          elsif ending == "cr" 
            ending = "\r"
          elsif ending == "auto" 
            ending = nil
          else 
            io.write_stderr("Expected line ending of either 'crlf' or 'lf' but got " + _hx_str(ending) + "\n")
            return 1
          end
          @csv_eol_preference = ending
          args.slice!(i,2)
          break
        end
      end
    end
  end
  cmd = args[0]
  if args.length < 2 
    if cmd == "version" 
      io.write_stdout(_hx_str(::Coopy::Coopy.version) + "\n")
      return 0
    end
    if cmd == "git" 
      io.write_stdout("You can use daff to improve git's handling of csv files, by using it as a\ndiff driver (for showing what has changed) and as a merge driver (for merging\nchanges between multiple versions).\n")
      io.write_stdout("\n")
      io.write_stdout("Automatic setup\n")
      io.write_stdout("---------------\n\n")
      io.write_stdout("Run:\n")
      io.write_stdout("  daff git csv\n")
      io.write_stdout("\n")
      io.write_stdout("Manual setup\n")
      io.write_stdout("------------\n\n")
      io.write_stdout("Create and add a file called .gitattributes in the root directory of your\nrepository, containing:\n\n")
      io.write_stdout("  *.csv diff=daff-csv\n")
      io.write_stdout("  *.csv merge=daff-csv\n")
      io.write_stdout("\nCreate a file called .gitconfig in your home directory (or alternatively\nopen .git/config for a particular repository) and add:\n\n")
      io.write_stdout("  [diff \"daff-csv\"]\n")
      io.write_stdout("  command = daff diff --git\n")
      io.write_stderr("\n")
      io.write_stdout("  [merge \"daff-csv\"]\n")
      io.write_stdout("  name = daff tabular merge\n")
      io.write_stdout("  driver = daff merge --output %A %O %A %B\n\n")
      io.write_stderr("Make sure you can run daff from the command-line as just \"daff\" - if not,\nreplace \"daff\" in the driver and command lines above with the correct way\nto call it. Add --no-color if your terminal does not support ANSI colors.")
      io.write_stderr("\n")
      return 0
    end
    if args.length < 1 
      io.write_stderr("daff can produce and apply tabular diffs.\n")
      io.write_stderr("Call as:\n")
      io.write_stderr("  daff [--color] [--no-color] [--output OUTPUT.csv] a.csv b.csv\n")
      io.write_stderr("  daff [--output OUTPUT.html] a.csv b.csv\n")
      io.write_stderr("  daff [--output OUTPUT.csv] parent.csv a.csv b.csv\n")
      io.write_stderr("  daff [--output OUTPUT.ndjson] a.ndjson b.ndjson\n")
      io.write_stderr("  daff [--www] a.csv b.csv\n")
      io.write_stderr("  daff patch [--inplace] [--output OUTPUT.csv] a.csv patch.csv\n")
      io.write_stderr("  daff merge [--inplace] [--output OUTPUT.csv] parent.csv a.csv b.csv\n")
      io.write_stderr("  daff trim [--output OUTPUT.csv] source.csv\n")
      io.write_stderr("  daff render [--output OUTPUT.html] diff.csv\n")
      io.write_stderr("  daff copy in.csv out.tsv\n")
      io.write_stderr("  daff in.csv\n")
      io.write_stderr("  daff git\n")
      io.write_stderr("  daff version\n")
      io.write_stderr("\n")
      io.write_stderr("The --inplace option to patch and merge will result in modification of a.csv.\n")
      io.write_stderr("\n")
      io.write_stderr("If you need more control, here is the full list of flags:\n")
      io.write_stderr("  daff diff [--output OUTPUT.csv] [--context NUM] [--all] [--act ACT] a.csv b.csv\n")
      io.write_stderr("     --act ACT:     show only a certain kind of change (update, insert, delete)\n")
      io.write_stderr("     --all:         do not prune unchanged rows or columns\n")
      io.write_stderr("     --all-rows:    do not prune unchanged rows\n")
      io.write_stderr("     --all-columns: do not prune unchanged columns\n")
      io.write_stderr("     --color:       highlight changes with terminal colors (default in terminals)\n")
      io.write_stderr("     --context NUM: show NUM rows of context\n")
      io.write_stderr("     --id:          specify column to use as primary key (repeat for multi-column key)\n")
      io.write_stderr("     --ignore:      specify column to ignore completely (can repeat)\n")
      io.write_stderr("     --index:       include row/columns numbers from original tables\n")
      io.write_stderr("     --input-format [csv|tsv|ssv|psv|json]: set format to expect for input\n")
      io.write_stderr("     --eol [crlf|lf|cr|auto]: separator between rows of csv output.\n")
      io.write_stderr("     --no-color:    make sure terminal colors are not used\n")
      io.write_stderr("     --ordered:     assume row order is meaningful (default for CSV)\n")
      io.write_stderr("     --output-format [csv|tsv|ssv|psv|json|copy|html]: set format for output\n")
      io.write_stderr("     --padding [dense|sparse|smart]: set padding method for aligning columns\n")
      io.write_stderr("     --table NAME:  compare the named table, used with SQL sources\n")
      io.write_stderr("     --unordered:   assume row order is meaningless (default for json formats)\n")
      io.write_stderr("     -w / --ignore-whitespace: ignore changes in leading/trailing whitespace\n")
      io.write_stderr("     -i / --ignore-case: ignore differences in case\n")
      io.write_stderr("\n")
      io.write_stderr("  daff render [--output OUTPUT.html] [--css CSS.css] [--fragment] [--plain] diff.csv\n")
      io.write_stderr("     --css CSS.css: generate a suitable css file to go with the html\n")
      io.write_stderr("     --fragment:    generate just a html fragment rather than a page\n")
      io.write_stderr("     --plain:       do not use fancy utf8 characters to make arrows prettier\n")
      io.write_stderr("     --www:         send output to a browser\n")
      return 1
    end
  end
  cmd1 = args[0]
  offset = 1
  if !Lambda.has(["diff","patch","merge","trim","render","git","version","copy"],cmd1) 
    if (cmd1.index("--",nil || 0) || -1) == 0 
      cmd1 = "diff"
      offset = 0
    elsif (cmd1.index(".",nil || 0) || -1) != -1 
      if args.length == 2 
        cmd1 = "diff"
        offset = 0
      elsif args.length == 1 
        cmd1 = "copy"
        offset = 0
      end
    end
  end
  if cmd1 == "git" 
    types = args.slice!(offset,args.length - offset)
    return self.install_git_driver(io,types)
  end
  if git 
    ct = args.length - offset
    if ct != 7 && ct != 9 
      io.write_stderr("Expected 7 or 9 parameters from git, but got " + _hx_str(ct) + "\n")
      return 1
    end
    git_args = args.slice!(offset,ct)
    args.slice!(0,args.length)
    offset = 0
    old_display_path = git_args[0]
    new_display_path = git_args[0]
    old_file = git_args[1]
    new_file = git_args[4]
    if ct == 9 
      io.write_stdout(git_args[8])
      new_display_path = git_args[7]
    end
    io.write_stdout("--- a/" + _hx_str(old_display_path) + "\n")
    io.write_stdout("+++ b/" + _hx_str(new_display_path) + "\n")
    args.push(old_file)
    args.push(new_file)
  end
  parent = nil
  if args.length - offset >= 3 
    parent = self.load_table(args[offset])
    offset+=1
  end
  aname = args[offset]
  a = self.load_table(aname)
  b = nil
  if args.length - offset >= 2 
    if cmd1 != "copy" 
      b = self.load_table(args[1 + offset])
    else 
      output = args[1 + offset]
    end
  end
  @flags.diff_strategy = @strategy
  if inplace 
    io.write_stderr("Please do not use --inplace when specifying an output.\n") if output != nil
    output = aname
    return 1
  end
  output = "-" if output == nil
  ok = true
  if cmd1 == "diff" 
    if !@order_set 
      @flags.ordered = @order_preference
      @flags.unchanged_context = 0 if !@flags.ordered
    end
    @flags.allow_nested_cells = @nested_output
    self.run_diff(parent,a,b,@flags,output)
  elsif cmd1 == "patch" 
    patcher = ::Coopy::HighlightPatch.new(a,b)
    patcher.apply
    self.save_table(output,a)
  elsif cmd1 == "merge" 
    merger = ::Coopy::Merger.new(parent,a,b,@flags)
    conflicts = merger.apply
    ok = conflicts == 0
    io.write_stderr(_hx_str(conflicts) + " conflict" + _hx_str((((conflicts > 1) ? "s" : ""))) + "\n") if conflicts > 0
    self.save_table(output,a)
  elsif cmd1 == "trim" 
    self.save_table(output,a)
  elsif cmd1 == "render" 
    self.render_table(output,a)
  elsif cmd1 == "copy" 
    os = ::Coopy::Tables.new(a)
    os.add("untitled")
    self.save_tables(output,os,self.use_color(@flags,output),false)
  end
  if ok 
    return 0
  else 
    return 1
  end
end

#run_diff(parent, a, b, flags, output) ⇒ Object



291
292
293
294
295
296
297
298
299
300
# File 'lib/lib/coopy/coopy.rb', line 291

def run_diff(parent,a,b,flags,output)
  ct = ::Coopy::Coopy.compare_tables3(parent,a,b,flags)
  align = ct.align
  td = ::Coopy::TableDiff.new(align,flags)
  o = ::Coopy::SimpleTable.new(0,0)
  os = ::Coopy::Tables.new(o)
  td.hilite_with_nesting(os)
  use_color = self.use_color(flags,output)
  self.save_tables(output,os,use_color,true)
end

#save_table(name, t, render = nil) ⇒ Object



129
130
131
132
133
# File 'lib/lib/coopy/coopy.rb', line 129

def save_table(name,t,render = nil)
  txt = self.encode_table(name,t,render)
  return true if txt == nil
  self.save_text(name,txt)
end

#save_tables(name, os, use_color, is_diff) ⇒ Object



162
163
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
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/lib/coopy/coopy.rb', line 162

def save_tables(name,os,use_color,is_diff)
  self.set_format(@output_format) if @output_format != "copy"
  txt = ""
  self.check_format(name)
  render = nil
  render = ::Coopy::TerminalDiffRender.new(@flags,@delim_preference,is_diff) if use_color
  order = os.get_order
  return self.save_table(name,os.one,render) if order.length == 1
  return self.render_tables(name,os) if @format_preference == "html" || @format_preference == "www"
  need_blank = false
  if order.length == 0 || os.has_ins_del 
    txt += self.encode_table(name,os.one,render)
    need_blank = true
  end
  if order.length > 1 
    _g1 = 1
    _g = order.length
    while(_g1 < _g) 
      i = _g1
      _g1+=1
      t = os.get(order[i])
      if t != nil 
        txt += "\n" if need_blank
        need_blank = true
        txt += _hx_str(order[i]) + "\n"
        line = ""
        begin
          _g3 = 0
          _g2 = order[i].length
          while(_g3 < _g2) 
            i1 = _g3
            _g3+=1
            line += "="
          end
        end
        txt += _hx_str(line) + "\n"
        txt += self.encode_table(name,os.get(order[i]),render)
      end
    end
  end
  self.save_text(name,txt)
end

#save_text(name, txt) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'lib/lib/coopy/coopy.rb', line 205

def save_text(name,txt)
  if name == nil 
    @cache_txt += txt
  elsif name != "-" 
    @io.save_content(name,txt)
  else 
    @io.write_stdout(txt)
  end
  true
end

#set_format(name) ⇒ Object



94
95
96
97
98
# File 'lib/lib/coopy/coopy.rb', line 94

def set_format(name)
  @extern_preference = false
  self.check_format("." + _hx_str(name))
  @extern_preference = true
end

#use_color(flags, output) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
# File 'lib/lib/coopy/coopy.rb', line 279

def use_color(flags,output)
  use_color = flags.terminal_format == "ansi"
  if flags.terminal_format == nil 
    if (output == nil || output == "-") && (@output_format == "copy" || @output_format == "csv" || @output_format == "psv") 
      if @io != nil 
        use_color = @io.is_tty if @io.is_tty_known
      end
    end
  end
  use_color
end