Class: Coopy::TableDiff

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

Instance Method Summary collapse

Constructor Details

#initialize(align, flags) ⇒ TableDiff

Returns a new instance of TableDiff.



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

def initialize(align,flags)
  @align = align
  @flags = flags
end

Instance Method Details

#get_separator(t, t2, root) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# File 'lib/lib/coopy/table_diff.rb', line 21

def get_separator(t,t2,root)
  sep = root
  w = t.get_width
  h = t.get_height
  view = t.get_cell_view
  begin
    _g = 0
    while(_g < h) 
      y = _g
      _g+=1
      begin
        _g1 = 0
        while(_g1 < w) 
          x = _g1
          _g1+=1
          txt = view.to_s(t.get_cell(x,y))
          next if txt == nil
          while((txt.index(sep,nil || 0) || -1) >= 0) 
            sep = "-" + _hx_str(sep)
          end
        end
      end
    end
  end
  if t2 != nil 
    w = t2.get_width
    h = t2.get_height
    begin
      _g2 = 0
      while(_g2 < h) 
        y1 = _g2
        _g2+=1
        begin
          _g11 = 0
          while(_g11 < w) 
            x1 = _g11
            _g11+=1
            txt1 = view.to_s(t2.get_cell(x1,y1))
            next if txt1 == nil
            while((txt1.index(sep,nil || 0) || -1) >= 0) 
              sep = "-" + _hx_str(sep)
            end
          end
        end
      end
    end
  end
  return sep
end

#hilite(output) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
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
509
510
511
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
# File 'lib/lib/coopy/table_diff.rb', line 193

def hilite(output)
  return false if !output.is_resizable
  output.resize(0,0)
  output.clear
  row_map = {}
  col_map = {}
  order = @align.to_order_pruned(true)
  units = order.get_list
  has_parent = @align.reference != nil
  a = nil
  b = nil
  p = nil
  ra_header = 0
  rb_header = 0
  is_index_p = {}
  is_index_a = {}
  is_index_b = {}
  if has_parent 
    p = @align.get_source
    a = @align.reference.get_target
    b = @align.get_target
    ra_header = @align.reference.meta.get_target_header
    rb_header = @align.meta.get_target_header
    if @align.get_index_columns != nil 
      _g = 0
      _g1 = @align.get_index_columns
      while(_g < _g1.length) 
        p2b = _g1[_g]
        _g+=1
        is_index_p[p2b.l] = true if p2b.l >= 0
        is_index_b[p2b.r] = true if p2b.r >= 0
      end
    end
    if @align.reference.get_index_columns != nil 
      _g2 = 0
      _g11 = @align.reference.get_index_columns
      while(_g2 < _g11.length) 
        p2a = _g11[_g2]
        _g2+=1
        is_index_p[p2a.l] = true if p2a.l >= 0
        is_index_a[p2a.r] = true if p2a.r >= 0
      end
    end
  else 
    a = @align.get_source
    b = @align.get_target
    p = a
    ra_header = @align.meta.get_source_header
    rb_header = @align.meta.get_target_header
    if @align.get_index_columns != nil 
      _g3 = 0
      _g12 = @align.get_index_columns
      while(_g3 < _g12.length) 
        a2b = _g12[_g3]
        _g3+=1
        is_index_a[a2b.l] = true if a2b.l >= 0
        is_index_b[a2b.r] = true if a2b.r >= 0
      end
    end
  end
  column_order = @align.meta.to_order_pruned(false)
  column_units = column_order.get_list
  show_rc_numbers = false
  row_moves = nil
  col_moves = nil
  if @flags.ordered 
    row_moves = {}
    moves = ::Coopy::Mover.move_units(units)
    begin
      _g13 = 0
      _g4 = moves.length
      while(_g13 < _g4) 
        i = _g13
        _g13+=1
        begin
          row_moves[moves[i]] = i
          i
        end
      end
    end
    col_moves = {}
    moves = ::Coopy::Mover.move_units(column_units)
    begin
      _g14 = 0
      _g5 = moves.length
      while(_g14 < _g5) 
        i1 = _g14
        _g14+=1
        begin
          col_moves[moves[i1]] = i1
          i1
        end
      end
    end
  end
  active = Array.new
  active_column = nil
  if !@flags.show_unchanged 
    _g15 = 0
    _g6 = units.length
    while(_g15 < _g6) 
      i2 = _g15
      _g15+=1
      active[i2] = 0
    end
  end
  allow_insert = @flags.allow_insert
  allow_delete = @flags.allow_delete
  allow_update = @flags.allow_update
  if !@flags.show_unchanged_columns 
    active_column = Array.new
    begin
      _g16 = 0
      _g7 = column_units.length
      while(_g16 < _g7) 
        i3 = _g16
        _g16+=1
        v = 0
        unit = column_units[i3]
        v = 1 if unit.l >= 0 && is_index_a[unit.l]
        v = 1 if unit.r >= 0 && is_index_b[unit.r]
        v = 1 if unit.p >= 0 && is_index_p[unit.p]
        active_column[i3] = v
      end
    end
  end
  outer_reps_needed = nil
  if @flags.show_unchanged && @flags.show_unchanged_columns 
    outer_reps_needed = 1
  else 
    outer_reps_needed = 2
  end
  v1 = a.get_cell_view
  sep = ""
  conflict_sep = ""
  schema = Array.new
  have_schema = false
  begin
    _g17 = 0
    _g8 = column_units.length
    while(_g17 < _g8) 
      j = _g17
      _g17+=1
      cunit = column_units[j]
      reordered = false
      if @flags.ordered 
        reordered = true if col_moves.include?(j)
        show_rc_numbers = true if reordered
      end
      act = ""
      if cunit.r >= 0 && cunit.lp == -1 
        have_schema = true
        act = "+++"
        if active_column != nil 
          active_column[j] = 1 if allow_update
        end
      end
      if cunit.r < 0 && cunit.lp >= 0 
        have_schema = true
        act = "---"
        if active_column != nil 
          active_column[j] = 1 if allow_update
        end
      end
      if cunit.r >= 0 && cunit.lp >= 0 
        if a.get_height >= ra_header && b.get_height >= rb_header 
          aa = a.get_cell(cunit.lp,ra_header)
          bb = b.get_cell(cunit.r,rb_header)
          if !v1.equals(aa,bb) 
            have_schema = true
            act = "("
            act += v1.to_s(aa)
            act += ")"
            active_column[j] = 1 if active_column != nil
          end
        end
      end
      if reordered 
        act = ":" + _hx_str(act)
        have_schema = true
        active_column = nil if active_column != nil
      end
      schema.push(act)
    end
  end
  if have_schema 
    at = output.get_height
    output.resize(column_units.length + 1,at + 1)
    output.set_cell(0,at,v1.to_datum("!"))
    begin
      _g18 = 0
      _g9 = column_units.length
      while(_g18 < _g9) 
        j1 = _g18
        _g18+=1
        output.set_cell(j1 + 1,at,v1.to_datum(schema[j1]))
      end
    end
  end
  top_line_done = false
  if @flags.always_show_header 
    at1 = output.get_height
    output.resize(column_units.length + 1,at1 + 1)
    output.set_cell(0,at1,v1.to_datum("@@"))
    begin
      _g19 = 0
      _g10 = column_units.length
      while(_g19 < _g10) 
        j2 = _g19
        _g19+=1
        cunit1 = column_units[j2]
        if cunit1.r >= 0 
          output.set_cell(j2 + 1,at1,b.get_cell(cunit1.r,rb_header)) if b.get_height > 0
        elsif cunit1.lp >= 0 
          output.set_cell(j2 + 1,at1,a.get_cell(cunit1.lp,ra_header)) if a.get_height > 0
        end
        col_map[j2 + 1] = cunit1
      end
    end
    top_line_done = true
  end
  begin
    _g20 = 0
    while(_g20 < outer_reps_needed) 
      out = _g20
      _g20+=1
      if out == 1 
        self.spread_context(units,@flags.unchanged_context,active)
        self.spread_context(column_units,@flags.unchanged_column_context,active_column)
        if active_column != nil 
          _g21 = 0
          _g110 = column_units.length
          while(_g21 < _g110) 
            i4 = _g21
            _g21+=1
            active_column[i4] = 0 if active_column[i4] == 3
          end
        end
      end
      showed_dummy = false
      l = -1
      r = -1
      begin
        _g22 = 0
        _g111 = units.length
        while(_g22 < _g111) 
          i5 = _g22
          _g22+=1
          unit1 = units[i5]
          reordered1 = false
          if @flags.ordered 
            reordered1 = true if row_moves.include?(i5)
            show_rc_numbers = true if reordered1
          end
          next if unit1.r < 0 && unit1.l < 0
          next if unit1.r == 0 && unit1.lp == 0 && top_line_done
          act1 = ""
          act1 = ":" if reordered1
          publish = @flags.show_unchanged
          dummy = false
          if out == 1 
            publish = active[i5] > 0
            dummy = active[i5] == 3
            next if dummy && showed_dummy
            next if !publish
          end
          showed_dummy = false if !dummy
          at2 = output.get_height
          output.resize(column_units.length + 1,at2 + 1) if publish
          if dummy 
            begin
              _g41 = 0
              _g31 = column_units.length + 1
              while(_g41 < _g31) 
                j3 = _g41
                _g41+=1
                output.set_cell(j3,at2,v1.to_datum("..."))
                showed_dummy = true
              end
            end
            next
          end
          have_addition = false
          skip = false
          if unit1.p < 0 && unit1.l < 0 && unit1.r >= 0 
            skip = true if !allow_insert
            act1 = "+++"
          end
          if (unit1.p >= 0 || !has_parent) && unit1.l >= 0 && unit1.r < 0 
            skip = true if !allow_delete
            act1 = "---"
          end
          if skip 
            if !publish 
              active[i5] = -3 if active != nil
            end
            next
          end
          begin
            _g42 = 0
            _g32 = column_units.length
            while(_g42 < _g32) 
              j4 = _g42
              _g42+=1
              cunit2 = column_units[j4]
              pp = nil
              ll = nil
              rr = nil
              dd = nil
              dd_to = nil
              have_dd_to = false
              dd_to_alt = nil
              have_dd_to_alt = false
              have_pp = false
              have_ll = false
              have_rr = false
              if cunit2.p >= 0 && unit1.p >= 0 
                pp = p.get_cell(cunit2.p,unit1.p)
                have_pp = true
              end
              if cunit2.l >= 0 && unit1.l >= 0 
                ll = a.get_cell(cunit2.l,unit1.l)
                have_ll = true
              end
              if cunit2.r >= 0 && unit1.r >= 0 
                rr = b.get_cell(cunit2.r,unit1.r)
                have_rr = true
                if (((have_pp) ? cunit2.p : cunit2.l)) < 0 
                  if rr != nil 
                    if v1.to_s(rr) != "" 
                      have_addition = true if @flags.allow_update
                    end
                  end
                end
              end
              if have_pp 
                if !have_rr 
                  dd = pp
                elsif v1.equals(pp,rr) 
                  dd = pp
                else 
                  dd = pp
                  dd_to = rr
                  have_dd_to = true
                  if !v1.equals(pp,ll) 
                    if !v1.equals(pp,rr) 
                      dd_to_alt = ll
                      have_dd_to_alt = true
                    end
                  end
                end
              elsif have_ll 
                if !have_rr 
                  dd = ll
                elsif v1.equals(ll,rr) 
                  dd = ll
                else 
                  dd = ll
                  dd_to = rr
                  have_dd_to = true
                end
              else 
                dd = rr
              end
              txt = nil
              if have_dd_to && allow_update 
                active_column[j4] = 1 if active_column != nil
                txt = self.quote_for_diff(v1,dd)
                sep = self.get_separator(a,b,"->") if sep == ""
                is_conflict = false
                if have_dd_to_alt 
                  is_conflict = true if !v1.equals(dd_to,dd_to_alt)
                end
                if !is_conflict 
                  txt = _hx_str(txt) + _hx_str(sep) + _hx_str(self.quote_for_diff(v1,dd_to))
                  act1 = sep if sep.length > act1.length
                else 
                  conflict_sep = _hx_str(self.get_separator(p,a,"!")) + _hx_str(sep) if conflict_sep == ""
                  txt = _hx_str(txt) + _hx_str(conflict_sep) + _hx_str(self.quote_for_diff(v1,dd_to_alt)) + _hx_str(conflict_sep) + _hx_str(self.quote_for_diff(v1,dd_to))
                  act1 = conflict_sep
                end
              end
              act1 = "+" if act1 == "" && have_addition
              if act1 == "+++" 
                if have_rr 
                  active_column[j4] = 1 if active_column != nil
                end
              end
              if publish 
                if active_column == nil || active_column[j4] > 0 
                  if txt != nil 
                    output.set_cell(j4 + 1,at2,v1.to_datum(txt))
                  else 
                    output.set_cell(j4 + 1,at2,dd)
                  end
                end
              end
            end
          end
          if publish 
            output.set_cell(0,at2,v1.to_datum(act1))
            row_map[at2] = unit1
          end
          if act1 != "" 
            if !publish 
              active[i5] = 1 if active != nil
            end
          end
        end
      end
    end
  end
  if !show_rc_numbers 
    if @flags.always_show_order 
      show_rc_numbers = true
    elsif @flags.ordered 
      show_rc_numbers = self.is_reordered(row_map,output.get_height)
      show_rc_numbers = self.is_reordered(col_map,output.get_width) if !show_rc_numbers
    end
  end
  admin_w = 1
  if show_rc_numbers && !@flags.never_show_order 
    admin_w+=1
    target = Array.new
    begin
      _g112 = 0
      _g23 = output.get_width
      while(_g112 < _g23) 
        i6 = _g112
        _g112+=1
        target.push(i6 + 1)
      end
    end
    output.insert_or_delete_columns(target,output.get_width + 1)
    @l_prev = -1
    @r_prev = -1
    begin
      _g113 = 0
      _g24 = output.get_height
      while(_g113 < _g24) 
        i7 = _g113
        _g113+=1
        unit2 = row_map[i7]
        next if unit2 == nil
        output.set_cell(0,i7,self.report_unit(unit2))
      end
    end
    target = Array.new
    begin
      _g114 = 0
      _g25 = output.get_height
      while(_g114 < _g25) 
        i8 = _g114
        _g114+=1
        target.push(i8 + 1)
      end
    end
    output.insert_or_delete_rows(target,output.get_height + 1)
    @l_prev = -1
    @r_prev = -1
    begin
      _g115 = 1
      _g26 = output.get_width
      while(_g115 < _g26) 
        i9 = _g115
        _g115+=1
        unit3 = col_map[i9 - 1]
        next if unit3 == nil
        output.set_cell(i9,0,self.report_unit(unit3))
      end
    end
    output.set_cell(0,0,"@:@")
  end
  if active_column != nil 
    all_active = true
    begin
      _g116 = 0
      _g27 = active_column.length
      while(_g116 < _g27) 
        i10 = _g116
        _g116+=1
        if active_column[i10] == 0 
          all_active = false
          break
        end
      end
    end
    if !all_active 
      fate = Array.new
      begin
        _g28 = 0
        while(_g28 < admin_w) 
          i11 = _g28
          _g28+=1
          fate.push(i11)
        end
      end
      at3 = admin_w
      ct = 0
      dots = Array.new
      begin
        _g117 = 0
        _g29 = active_column.length
        while(_g117 < _g29) 
          i12 = _g117
          _g117+=1
          off = active_column[i12] == 0
          if off 
            ct = ct + 1
          else 
            ct = 0
          end
          if off && ct > 1 
            fate.push(-1)
          else 
            dots.push(at3) if off
            fate.push(at3)
            at3+=1
          end
        end
      end
      output.insert_or_delete_columns(fate,at3)
      begin
        _g30 = 0
        while(_g30 < dots.length) 
          d = dots[_g30]
          _g30+=1
          begin
            _g210 = 0
            _g118 = output.get_height
            while(_g210 < _g118) 
              j5 = _g210
              _g210+=1
              output.set_cell(d,j5,"...")
            end
          end
        end
      end
    end
  end
  return true
end

#quote_for_diff(v, d) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lib/coopy/table_diff.rb', line 71

def quote_for_diff(v,d)
  _nil = "NULL"
  return _nil if v.equals(d,nil)
  str = v.to_s(d)
  score = 0
  begin
    _g1 = 0
    _g = str.length
    while(_g1 < _g) 
      i = _g1
      _g1+=1
      break if (str[score].ord rescue nil) != 95
      score+=1
    end
  end
  str = "_" + _hx_str(str) if str[score..-1] == _nil
  return str
end