|
| 1 | +class View |
| 2 | + require 'configuration' |
| 3 | + require 'termbox' |
| 4 | + |
| 5 | + attr_reader :colors, :mailbox |
| 6 | + |
| 7 | + def initialize location |
| 8 | + @Border_width = 1 |
| 9 | + @start_x = location[:x0] |
| 10 | + @start_y = location[:y0] |
| 11 | + @end_x = location[:x1] |
| 12 | + @end_y = location[:y1] |
| 13 | + set_mailbox |
| 14 | + set_colors |
| 15 | + end |
| 16 | + |
| 17 | + def draw_text text, x, y |
| 18 | + words = text.split(/\s+/) |
| 19 | + words.each do |word| |
| 20 | + if x + word.length + 1 >= @end_x |
| 21 | + y += 1 |
| 22 | + x = @start_x + @Border_width + 1 |
| 23 | + redo |
| 24 | + elsif y >= @end_y |
| 25 | + return y |
| 26 | + end |
| 27 | + word.each_char do |c| |
| 28 | + Termbox.tb_change_cell(x, y, c.ord, @colors[:bg], @colors[:fg]) |
| 29 | + x += 1 |
| 30 | + end |
| 31 | + Termbox.tb_change_cell(x, y, " ".ord, @colors[:bg], @colors[:fg]) |
| 32 | + x += 1 |
| 33 | + end |
| 34 | + return y |
| 35 | + end |
| 36 | + |
| 37 | + def draw_horizontal_line at |
| 38 | + @start_x.upto(@end_x) do |x| |
| 39 | + Termbox.tb_change_cell(x, at, " ".ord, @colors[:bg], @colors[:fg]) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + def draw_vertical_line at |
| 44 | + @start_y.upto(@end_y) do |y| |
| 45 | + Termbox.tb_change_cell(at, y, " ".ord, @colors[:bg], @colors[:fg]) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + private |
| 50 | + |
| 51 | + # Reads the maildir format from config file. Requires the class and sets |
| 52 | + # @mailbox to mailbox/<maildir-format>_mailbox |
| 53 | + def set_mailbox |
| 54 | + config = Configuration.instance |
| 55 | + maildir_format = config.maildir_format |
| 56 | + mailbox_class = maildir_format.capitalize + "_mailbox" |
| 57 | + class_dir = File.expand_path('../../', __FILE__) + "/mailbox" |
| 58 | + mailbox_file = File.join class_dir, mailbox_class.downcase |
| 59 | + require mailbox_file |
| 60 | + maildir_path = config.maildir |
| 61 | + @mailbox = self.class.const_get(mailbox_class).new maildir_path |
| 62 | + end |
| 63 | + |
| 64 | + def set_colors |
| 65 | + colors = Configuration.instance.colors |
| 66 | + prefix = 'Termbox::Colors[:' |
| 67 | + suffix = ']' |
| 68 | + fg = prefix + colors.foreground + suffix |
| 69 | + bg = prefix + colors.background + suffix |
| 70 | + @colors = {fg: (eval fg), bg: (eval bg)} |
| 71 | + end |
| 72 | + |
| 73 | +end |
0 commit comments