Skip to content

Commit 4f3ff1c

Browse files
committed
Implement the views we want to display to the user.
The Folder_view is a list of all the folders of the mailbox. Label_view is a list of all the labels of the mailbox. The Mail_view displays the mail itself.
1 parent 4bb7ab9 commit 4f3ff1c

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed

lib/views/folder_view.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Folder_view < View
2+
3+
def initialize location
4+
super
5+
end
6+
7+
def draw
8+
draw_folders
9+
draw_horizontal_line @end_y
10+
end
11+
12+
private
13+
14+
def draw_folders
15+
x = @start_x
16+
y = @start_y
17+
y = draw_text("Folders:", x, y)
18+
@mailbox.folders.each do |folder|
19+
y = draw_text folder, x, y + 1
20+
end
21+
end
22+
end

lib/views/label_view.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Label_view < View
2+
3+
def initialize location
4+
super
5+
end
6+
7+
def draw
8+
draw_labels
9+
end
10+
11+
private
12+
13+
def draw_labels
14+
x = @start_x
15+
y = @start_y
16+
y = draw_text("Labels:", x, y)
17+
@mailbox.labels.each do |label|
18+
y = draw_text label, x, y + 1
19+
end
20+
end
21+
end

lib/views/mail_view.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'views/view'
2+
3+
class Mail_view < View
4+
require 'termbox'
5+
6+
def initialize location
7+
super
8+
@mail = @mailbox.most_recent
9+
end
10+
11+
def draw
12+
draw_borders
13+
draw_mail
14+
end
15+
16+
private
17+
18+
def draw_borders
19+
draw_horizontal_line @start_y
20+
draw_horizontal_line @end_y
21+
draw_vertical_line @start_x
22+
draw_vertical_line @end_x
23+
end
24+
25+
def draw_mail
26+
from = @mail.sender
27+
subject = @mail.subject
28+
text = @mail.body.decoded
29+
x = @start_x + @Border_width + 1
30+
y = @start_y + @Border_width + 1
31+
32+
y = draw_text(("From: " + from), x, y) + 1
33+
y = draw_text(("Subject: " + subject), x, y + 1) + 1
34+
draw_text text, x, y + 1
35+
end
36+
37+
end

lib/views/view.rb

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

spec/views/view_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'spec_helper'
2+
require 'termbox'
3+
4+
describe View do
5+
6+
let(:config) {Configuration.instance}
7+
8+
subject(:view) { View.new({x0: 1, x1: 1, y0: 1, y1: 1}) }
9+
10+
it "instantiates the correct mailbox by reading the config file" do
11+
maildir_format = config.maildir_format
12+
view.mailbox.class.name =~ /#{maildir_format}/
13+
end
14+
15+
it "reads its color attribute from the config file" do
16+
fg = config.colors.foreground
17+
bg = config.colors.background
18+
view.colors[:fg].should eq Termbox::Colors[:green]
19+
view.colors[:bg].should eq Termbox::Colors[:black]
20+
end
21+
end

0 commit comments

Comments
 (0)