Class: Rodo

Inherits:
Object
  • Object
show all
Includes:
Commands
Defined in:
lib/rodo.rb,
lib/rodo/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands

#initialize

Instance Attribute Details

#current_day_indexObject

Returns the value of attribute current_day_index.



39
40
41
# File 'lib/rodo.rb', line 39

def current_day_index
  @current_day_index
end

#cursor_lineObject

Returns the value of attribute cursor_line.



39
40
41
# File 'lib/rodo.rb', line 39

def cursor_line
  @cursor_line
end

#cursor_xObject

Returns the value of attribute cursor_x.



39
40
41
# File 'lib/rodo.rb', line 39

def cursor_x
  @cursor_x
end

#debugObject

Returns the value of attribute debug.



42
43
44
# File 'lib/rodo.rb', line 42

def debug
  @debug
end

#file_nameObject

Returns the value of attribute file_name.



37
38
39
# File 'lib/rodo.rb', line 37

def file_name
  @file_name
end

#journalObject

Returns the value of attribute journal.



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

def journal
  @journal
end

#modeObject

Returns the value of attribute mode.



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

def mode
  @mode
end

#newly_added_lineObject

Returns the value of attribute newly_added_line.



39
40
41
# File 'lib/rodo.rb', line 39

def newly_added_line
  @newly_added_line
end

#win1Object

Returns the value of attribute win1.



38
39
40
# File 'lib/rodo.rb', line 38

def win1
  @win1
end

#win1bObject

Returns the value of attribute win1b.



38
39
40
# File 'lib/rodo.rb', line 38

def win1b
  @win1b
end

Instance Method Details

#build_windowsObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rodo.rb', line 126

def build_windows

  @win1.close if @win1
  if Curses.debug_win
    Curses.debug_win.close
    Curses.debug_win = nil
  end

  # Building a static window
  @win1 = Curses::Window.new(Curses.lines, Curses.cols / (@debug ? 2 : 1), 0, 0)
  @win1.keypad = true
  @win1b = @win1.subwin(@win1.maxy - 2, @win1.maxx - 3, 1, 2)

  if @debug
    debug_win = Curses::Window.new(Curses.lines, Curses.cols / 2, 0, Curses.cols / 2)
    debug_win.box
    debug_win.caption "Debug information"
    debug_win.refresh
    Curses.debug_win = debug_win.inset
  end
end

#get_cmd(win, pos_y, pos_x, command_prototyp_list) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
# File 'lib/rodo.rb', line 148

def get_cmd(win, pos_y, pos_x, command_prototyp_list)

  size_y = 10
  size_x = [win.maxx - win.begx - 2 * pos_x - 2, 10].max

  cmd_win = Curses::Window.new(size_y + 2, size_x + 2, win.begy + pos_y, win.begx + pos_x)
  cmd_win.keypad = true
  cmd_win.box
  cmd_win.refresh
  inset = cmd_win.inset(1)
  inset.keypad = true
  result = inset.gets("> ".dup) { |s|

    if s =~ /^\s*\>\s*(.*)$/
      cmd = $1 # Remove prompt
      list = command_prototyp_list.dup
      if cmd.strip != ""
        list.select! { |c|
          if c.instance_of? Hash
            if c.has_key?(:regex)
              c[:regex] =~ cmd
            elsif c.has_key?(:description)
              c[:description].include?(cmd)
            else
              true
            end
          else
            c.include?(cmd)
          end
        }
      end
      #Curses.debug_win.puts cmd
      #Curses.debug_win.puts list.inspect
      #Curses.debug_win.refresh
      list.push "No matching commands" if list.empty?
      list.each { |c|
        if c.instance_of? Hash
          if c.has_key?(:description)
            inset.puts c[:description]
          end
        else
          inset.puts c
        end
      }
    end

  }

  # Curses.debug_win.puts "Result: #{result}"

  return result
end

#initObject



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
70
71
72
73
74
75
76
77
78
79
# File 'lib/rodo.rb', line 44

def init
  Curses.ESCDELAY = 50        # 50 milliseconds (ESC is always a key, never a sequence until automatic)
  Curses.raw
  Curses.noecho               # Don't print user input
  Curses.nonl
  Curses.stdscr.keypad = true # Control keys should be returned as keycodes
  Curses.init_screen
  Curses.curs_set(0)          # Invisible cursor
  Curses.bracketed_paste      # From CursesUtils: Enable Bracketed Paste Mode

  if !Curses.has_colors?
    Curses.abort "Curses doesn't have color support in this TTY."
  else
    Curses.start_color
    Curses.colors.times { |i|
      Curses.init_pair(i, i, 0)
    }
  end

  # If no file is given, assume the user wants to edit "~/plan.md"
  if ARGV.empty?
    @file_name = "plan.md"
    Dir.chdir Dir.home
  else
    @file_name = ARGV[0]
  end
  File.write(@file_name, "# #{Time.now.strftime("%Y-%m-%d")}\n") if !File.exist?(@file_name)

  @journal = Journal.from_s(File.read(@file_name))

  @cursor = Cursor.new(@journal)
  @cursor.day = @journal.most_recent_index
  @mode = :scroll
  @newly_added_line = nil
  @debug = $DEBUG || $VERBOSE # || ENV["BUNDLE_BIN_PATH"]
end

#main_loopObject



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

def main_loop

  status = init()
  return if status == :close

  begin

    build_windows()

    loop do
      render_windows()

      char = @win1::get_char3

      if char == Curses::KEY_RESIZE
        sleep(0.5)
        build_windows
        next
      end

      case char
      in paste:
        process_paste(paste)
      else
        status = process_input(char)
        return if status == :close
      end
    end

  ensure
    Curses.close_screen
  end
end

#postpone(lines, current_day, n) ⇒ Object



807
808
809
810
811
812
813
# File 'lib/rodo.rb', line 807

def postpone(lines, current_day, n)

  target_day = @journal.postpone_line(current_day, @cursor.line, n)

  # Adjust @cursor.day if a new day was created
  @cursor.day += 1 if current_day != @journal.days[@cursor.day]
end

#process_input(char) ⇒ Object



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

def process_input(char)

  current_day = @journal.days[@cursor.day]
  lines = current_day.lines

  case @mode

  when :journalling

    case char

    when CTRLC, CTRLC.chr
      return :close

    when Curses::KEY_UP
      @cursor.line -= 1 if @cursor.line > 0

    when Curses::KEY_DOWN
      @cursor.line += 1 if @cursor.line < lines.size - 1

    when "\u0001" # CTRL+A

      @cursor.x = 0

    when "\u0005" # CTRL+E

      @cursor.x = lines[@cursor.line].length

    when Curses::KEY_LEFT

      if @cursor.x == 0
        if @cursor.line > 0
          @cursor.line -= 1
          @cursor.x = lines[@cursor.line].length
        end
      else
       @cursor.x -= 1 if @cursor.x > 0
      end

    when Curses::KEY_RIGHT

      if @cursor.x >= lines[@cursor.line].length
        if @cursor.line < lines.size - 1
          @cursor.line += 1
          @cursor.x = 0
        end
      else
        @cursor.x += 1 if @cursor.x < lines[@cursor.line].length
      end

    when Curses::KEY_CTRL_LEFT

      if @cursor.x == 0
        if @cursor.line > 0
          @cursor.line -= 1
          @cursor.x = lines[@cursor.line].length
        end
      end

      left = lines[@cursor.line][0...@cursor.x]
      if /\b?((^|\S+)\s*)$/ =~ left
        @cursor.x -= $1.length
      end

    when Curses::KEY_CTRL_RIGHT

      if @cursor.x >= lines[@cursor.line].length - 1
        if @cursor.line < lines.size - 1
          @cursor.line += 1
          @cursor.x = 0
        end
      end
      right = lines[@cursor.line][@cursor.x..-1]
      if /^(\s*\S+\b?\s*)/ =~ right
        @cursor.x += $1.length
      end

    when Curses::KEY_DC, "\u0004" # DEL, CTRL+D

      if @cursor.x >= lines[@cursor.line].size
        if @cursor.line < lines.size - 1
          lines[@cursor.line] += lines[@cursor.line + 1]
          lines.delete_at(@cursor.line + 1)
        end
      else
        lines[@cursor.line].slice!(@cursor.x)
      end
      win1b.clear

    when Curses::KEY_BACKSPACE

      if @cursor.x == 0 && @cursor.line > 0
        @cursor.line -= 1
        @cursor.x = lines[@cursor.line].length
        lines[@cursor.line] += lines[@cursor.line + 1]
        lines.delete_at(@cursor.line + 1)
      elsif @cursor.x > 0
        lines[@cursor.line].slice!(@cursor.x - 1)
        @cursor.x -= 1
      end

    when "\v" # CTRL K
      if lines.size > 1
        lines.delete_at(@cursor.line)
        @cursor.line -= 1 if @cursor.line >= lines.size
      else
        lines[0] = "".dup
      end
      @win1b.clear

    when ENTER, ENTER.chr

      left  = lines[@cursor.line][0...@cursor.x]
      right = lines[@cursor.line][@cursor.x..-1]

      # If line to the left of cursor starts with "- [ ]" or with a star or dash
      if /^(?<lead>\s+[*-])(?<option>\s\[.\]\s?)?(?<rest>.*?)$/ =~ left && !(right =~ /^\s+[*-]/)

        if rest.strip.length == 0 and right.strip.length == 0
          # line is empty, except for */-/[ ]
          right = nil
          # unindent
          if /^\s\s(?<lead2>.*)$/ =~ lead
            lead = lead2
          else
            lead = ""
            option = ""
          end
          left = lead + option
        else
          if option =~ /^\s\[.\]/
            option = " [ ]"
          end
        end

        option = "" if !option
        lead = lead + option.rstrip + " "
        right = lead + right.lstrip if right
        @cursor.x = lead.length
      else
        @cursor.x = 0
      end

      lines[@cursor.line] = left
      if right != nil
        lines.insert(@cursor.line + 1, right)
        @cursor.line += 1
      end
    when ESC, ESC.chr

      @mode = :scroll
      @journal.days[@cursor.day] = TodoDay.new(lines) # Reparse day after edit

    when /[[:print:]]/

      lines[@cursor.line].insert(@cursor.x, char)
      @cursor.x += 1

    when "\t", "\t".ord
      if lines[@cursor.line] =~ /\s*[-+*]/
        lines[@cursor.line].sub!(/^/, "  ")
        @cursor.x += 2
      end

    when Curses::KEY_BTAB
      if lines[@cursor.line] =~ /(  |\t)\s*[-+*]/
        lines[@cursor.line].sub!(/^(  |\t)/, "")
        @cursor.x -= 2
        @cursor.x = 0 if @cursor.x < 0
      end

    else
      if Curses.debug_win
        Curses.debug_win.puts "Char not handled: " + Curses::char_info(char)
        Curses.debug_win.refresh
      end
    end

  when :edit

    case char

    when CTRLC, CTRLC.chr
      return :close

    when "\u0001" # CTRL+A

      @cursor.x = 0

    when "\u0005" # CTRL+E

      @cursor.x = lines[@cursor.line].length

    when Curses::KEY_LEFT

      @cursor.x -= 1 if @cursor.x > 0

    when Curses::KEY_RIGHT

      @cursor.x += 1 if @cursor.x < lines[@cursor.line].length

    when Curses::KEY_CTRL_LEFT

      left = lines[@cursor.line][0...@cursor.x]
      if /((^|\w+?|\W+?)\s*)$/ =~ left
        @cursor.x -= $1.length
      end

    when Curses::KEY_CTRL_RIGHT

      right = lines[@cursor.line][@cursor.x..-1]
      if /^(\s*|\S+\s*)/ =~ right
        @cursor.x += $1.length
      end

    when Curses::KEY_DC, "\u0004" # DEL, CTRL+D

      if @cursor.x < lines[@cursor.line].size
        lines[@cursor.line].slice!(@cursor.x)
      end

    when Curses::KEY_BACKSPACE
      if @cursor.x > 0
        lines[@cursor.line].slice!(@cursor.x - 1)
        @cursor.x -= 1
      end

    when ENTER, ENTER.chr
      @mode = :scroll
      @newly_added_line = nil
      @journal.days[@cursor.day] = TodoDay.new(lines) # Reparse day after edit

    when ESC, ESC.chr
      # When pressing ESC after an insert, which didn't change anything then undo the insertion
      if @newly_added_line && lines[@cursor.line] == @newly_added_line
        lines.delete_at(@cursor.line)
        @cursor.line -= 1 if @cursor.line >= lines.size
        @win1b.clear
      end
      # Debug:
      # Curses.debug "Lines[@cursor.line] == #{lines[@cursor.line].inspect }, @newly_added_line == #{@newly_added_line.inspect}"

      @mode = :scroll
      @newly_added_line = nil
      @journal.days[@cursor.day] = TodoDay.new(lines) # Reparse day after edit

    when /[[:print:]]/

      lines[@cursor.line].insert(@cursor.x, char)
      # Curses.setpos(@cursor.line + 2, lines[@cursor.line].length + 2)
      @cursor.x += 1

    when "\t", "\t".ord
      if lines[@cursor.line] =~ /\s*[-+*]/
        lines[@cursor.line].sub!(/^/, "  ")
        @cursor.x += 2
      end

    when Curses::KEY_BTAB
      if lines[@cursor.line] =~ /(  |\t)\s*[-+*]/
        lines[@cursor.line].sub!(/^(  |\t)/, "")
        @cursor.x -= 2
        @cursor.x = 0 if @cursor.x < 0
      end

    else
      if Curses.debug_win
        Curses.debug_win.puts "Char not handled: " + Curses::char_info(char)
        Curses.debug_win.refresh
      end
    end

  when :scroll

    case char
      when 'q'
        return self.save

      when CTRLC, CTRLC.chr
        return :close

      when '~'
        @debug = !@debug
        build_windows

      when Curses::KEY_UP
        @cursor.line -= 1 if @cursor.line > 0

      when Curses::KEY_DOWN
        @cursor.line += 1 if @cursor.line < lines.size - 1

      when Curses::KEY_RIGHT then
        @cursor.day -= 1 if @cursor.day > 0
        @win1b.clear

      when Curses::KEY_LEFT then
        @cursor.day += 1 if @cursor.day < @journal.days.size - 1
        @win1b.clear

      when "\t", "\t".ord
        if lines[@cursor.line] =~ /\s*[-+*]/
          lines[@cursor.line].sub!(/^/, "  ")
        end

      when Curses::KEY_F1

        cmd = get_cmd(@win1b, 1, 1, @command_prototyp_list)
        if cmd == :close
          # do nothing, because user closed window
        elsif cmd =~ /^\s*>\s*(.*)$/

          cmd = $1 # Remove prompt

          # Search list of available commands for a match and run the cmd
          @command_prototyp_list.find { |command_prototype|
            case command_prototype
            in regex: r, do_cmd: c
              if r =~ cmd
                c.call(cmd, lines, current_day)
                next true
              else
                next false
              end

            in description: d
              if d.start_with? cmd.strip
                Curses.unget_char(cmd.strip)
                next true
              end
              next false

            in String
              if command_prototype.start_with? cmd.strip
                Curses.unget_char(cmd.strip)
                next true
              end
              next false
            else
              if Curses.debug_win
                Curses.debug_win.puts "Unknown command enter from F1: #{cmd}"
                Curses.debug_win.refresh
              end
            end
          }
        end

      when Curses::KEY_BTAB
        if lines[@cursor.line] =~ /(  |\t)\s*[-+*]/
          lines[@cursor.line].sub!(/^(  |\t)/, "")
        end

        #when Curses::KEY_BACKSPACE then
      #  buffer.remove_char_before_cursor
      #when ENTER then buffer.new_line
      when '.', 'x'
        if lines[@cursor.line] =~ /\[\s\]/
          lines[@cursor.line].gsub!(/\[\s\]/, "[x]")
        elsif lines[@cursor.line] =~ /\[[xX]\]/
          lines[@cursor.line].gsub!(/\[[xX]\]/, "[ ]")
        end
      #when /[[:print:]]/ then buffer.add_char(char)

      when '2'
        # ★

      when 'e' # Edit
        @mode = :journalling
        @cursor.x = lines[@cursor.line].length

      when 'm' # Move
        @mode = :move

      when 'i' # Insert
        @cursor.line = 1 if @cursor.line == 0
        @newly_added_line = current_day.line_prototype(@cursor.line)
        lines.insert(@cursor.line, @newly_added_line.dup)
        @mode = :edit
        @cursor.x = lines[@cursor.line].size

      when ENTER, ENTER.chr
        @mode = :edit
        @cursor.x = lines[@cursor.line].size

      when 'a' # append

        @newly_added_line = current_day.line_prototype(@cursor.line)
        @cursor.line += 1
        lines.insert(@cursor.line, @newly_added_line.dup)
        @mode = :edit
        @cursor.x = lines[@cursor.line].size

      when 't' # t(oday)

        @cursor.day = @journal.close(current_day)
        @current_day = @journal.days[@cursor.day]
        @win1b.clear

      when 'k' # kill
        if lines.size > 1
          lines.delete_at(@cursor.line)
          @cursor.line -= 1 if @cursor.line >= lines.size
        else
          lines[0] = "".dup
        end
        @win1b.clear

      when 'w' # waiting

        if lines[@cursor.line] =~ /\[\s\]/

          line_to_wait_for = lines[@cursor.line].dup

          if !(line_to_wait_for =~ / - ⌛ since \d\d\d\d-\d\d-\d\d$/) && current_day.date
            line_to_wait_for = line_to_wait_for.rstrip + " - ⌛ since #{current_day.date_name}"
          end

          # Get target day (and create if it doesn't exist) and add there
          postpone_day = @journal.postpone_day(current_day, 7)
          postpone_day.lines.insert(1, line_to_wait_for)

          # Adjust @cursor.day if a new day was created
          @cursor.day += 1 if current_day != @journal.days[@cursor.day]

          # Add hourclass here
          lines[@cursor.line].gsub!(/\[\s\]/, "[⌛]")

        end

      when 'p' # postpone

        postpone(lines, current_day, 1)

      else
        if Curses.debug_win
          Curses.debug_win.puts "Char not handled: " + Curses::char_info(char)
          Curses.debug_win.refresh
        end
    end

  when :move

    case char
      when 'q'
        return self.save

      when CTRLC, CTRLC.chr
        return :close

      when Curses::KEY_UP

        if @cursor.line > 0
          lines[@cursor.line], lines[@cursor.line - 1] = lines[@cursor.line - 1], lines[@cursor.line]
          @cursor.line -= 1
        end

      when Curses::KEY_DOWN

        if @cursor.line < lines.size - 1
          lines[@cursor.line], lines[@cursor.line + 1] = lines[@cursor.line + 1], lines[@cursor.line]
          @cursor.line += 1
        end

      when "\t", "\t".ord, Curses::KEY_RIGHT
        if lines[@cursor.line] =~ /\s*[-+*]/
          lines[@cursor.line].sub!(/^/, "  ")
        end

      when Curses::KEY_BTAB, Curses::KEY_LEFT
        if lines[@cursor.line] =~ /(  |\t)\s*[-+*]/
          lines[@cursor.line].sub!(/^(  |\t)/, "")
        end

      when ENTER, ENTER.chr, ESC, ESC.chr
        @mode = :scroll

      else
        Curses.debug "Char not handled: " + Curses::char_info(char)
    end
  else
    Curses.abort "Mode #{@mode} not handled"
  end

  return nil
end

#process_paste(pasted) ⇒ Object



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

def process_paste(pasted)

  pasted.gsub! /\r\n?/, "\n"
  # Curses.debug "Pasted: #{pasted.inspect}"

  current_day = @journal.days[@cursor.day]
  lines = current_day.lines

  # Todo Clean-up Pasted Special characters (bullets)
  pasted.gsub! /^\t/, " " # Replace initial tabs with 1 space
  pasted.gsub! /\t/, "  " # Replace other tabs with 2 spaces
  pasted.gsub! /^(\s*)[•□○®◊§] /, "\\1- "

  case @mode
  when :journalling, :edit

    # When in journalling or edit mode, then pasting will split the current line

    pasted_lines = pasted.lines

    left  = lines[@cursor.line][0...@cursor.x]
    right = lines[@cursor.line][@cursor.x..-1]

    pasted_lines.each_with_index { |line, i|
      # On the first line, append to text left of cursor
      if i == 0
        lines[@cursor.line] = left + line
      else
        lines.insert(@cursor.line, line)
      end

      # On the last line, append existing text right of cursor
      if i == pasted_lines.size - 1
        @cursor.x = lines[@cursor.line].length
        lines[@cursor.line] += right
      else
        @cursor.line += 1
      end
    }

  when :scroll, :move
    # Insert each line after the current line
    pasted.each_line { |l|
      @cursor.line += 1
      lines.insert(@cursor.line, l)
    }
    @cursor.x = lines[@cursor.line].size

  else
    Curses.abort("Case not handled: #{@mode}");
  end
end

#render_windowsObject



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

def render_windows

  current_day = @journal.days[@cursor.day]

  @win1.box

  if Curses.debug_win
    Curses.debug_win.setpos(0, 0)
    Curses.debug_win.puts "Cursor @ #{@cursor.line}"
    Curses.debug_win.puts "Mode: #{@mode}"
    Curses.debug_win.refresh
  end

  # Next / prev Navigation
  has_next_day = @cursor.day > 0
  has_prev_day = @cursor.day < @journal.days.size - 1

  nav_str = []
  nav_str << "#{@journal.days[@cursor.day + 1].date_name}"   if has_prev_day
  nav_str <<   "#{@journal.days[@cursor.day - 1].date_name}" if has_next_day

  nav_str = nav_str.join(" ")
  @win1b.setpos(0, @win1b.maxx - nav_str.length - 1)
  @win1b.addstr(nav_str)

  # Contents of current day:
  lines = current_day.lines
  overflows = 0
  lines.each_with_index do |line, i|
    Curses.abort("Line #{i} is nil") if line == nil

    @win1b.setpos(i + overflows + 1, 0)
    if @cursor.line == i
      @win1b.attron(Curses.color_pair(255))
    else
      @win1b.attron(Curses.color_pair(246))
    end
    if (%i[scroll move].include?(@mode) && (@cursor.line != i || line.size != 0)) ||
       (%i[edit journalling].include?(@mode) && @cursor.line != i)
    then
       @win1b.puts line
    else
      line = line + " "

      if Curses.debug_win
        Curses.debug_win.puts "Before Cursor: '#{line[...@cursor.x]}'"
        Curses.debug_win.puts "Cursor_x: '#{@cursor.x}'"
        Curses.debug_win.refresh
      end

      @cursor.x = 0 if ![:edit, :journalling].include?(@mode)
      @cursor.x = line.size - 1 if @cursor.x >= line.size

      @win1b.addstr line[...@cursor.x] if @cursor.x > 0
      @win1b.attron(Curses::A_REVERSE)
      @win1b.addstr line[@cursor.x]
      @win1b.attroff(Curses::A_REVERSE)
      @win1b.addstr(line[(@cursor.x+1)..]) if @cursor.x < line.size
      @win1b.addstr("\n")
    end

    overflows += line.length / @win1b.maxx
  end

  # At the end switch color to normal again
  @win1b.attron(Curses.color_pair(246))
end

#saveObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/rodo.rb', line 115

def save
  FileUtils.mkdir_p "_bak"
  FileUtils.cp(@file_name,
    File.join(
      File.dirname(@file_name),
      "_bak",
      File.basename(@file_name) + "-#{Time.now.strftime("%Y-%m-%dT%H-%M-%S")}.bak"))
  File.write(@file_name, @journal.to_s)
  return :close
end