mireru 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5b2947adb9a99a12d807c1f0e58d38038448eb3
4
- data.tar.gz: 1d9776369ac1a0464740d56299d051fa13c29f73
3
+ metadata.gz: 49dc2f9847431c7c454e31a51a6fab121a121385
4
+ data.tar.gz: 1c16885733d833d203b7e341b0c54ba633f317ea
5
5
  SHA512:
6
- metadata.gz: dba7074a99ee4fff2698f3da1f8fae313b92f136938fba0b1c8841437e13fb656c6ac047f011c5633685410cf48ee28cd77fe07eb72b75e5780543a7ac217dd4
7
- data.tar.gz: a352694d1383a69a3953a5ea2826f3e3280ca0cdab381e112604cf497b21cda5a17a9df8980742becfece519cf485eedafa6a71171dbffe58bc1b734628969ab
6
+ metadata.gz: 573345ab9d63ea7492d9e0fe8c3c904164d9b6621e767f394ff75d4fde896bfc8896a28bc8c4eee1e5b038c892740d6edeaa2975c7a55b4c10bbc017eeec83e4
7
+ data.tar.gz: d03f11c3e35095921c868cec068215d4a7660eddf5235da61487dc4253386f819239daea24b1e1926dae0e693c944ccd017ba87f906464ff1aed69fcc9671fb4
data/README.md CHANGED
@@ -12,16 +12,26 @@ A file viewer with a focus on flexibility by Ruby/GTK3.
12
12
 
13
13
  $ mireru [FILE...]
14
14
 
15
- If no argument then search current directory.
15
+ If no argument, then search current directory.
16
16
 
17
17
  ### Keybind
18
18
 
19
+ #### Common
20
+
19
21
  n: next
20
22
 
21
23
  p: prev
22
24
 
25
+ r: reload
26
+
23
27
  q: quit
24
28
 
29
+ #### Image
30
+
31
+ f: fits window size
32
+
33
+ o: original size
34
+
25
35
  ## Contributing
26
36
 
27
37
  1. Fork it
data/images/sorry.png CHANGED
Binary file
@@ -35,29 +35,8 @@ module Mireru
35
35
  exit(false)
36
36
  end
37
37
 
38
- file = file_container.shift
39
-
40
38
  window = ::Mireru::Window.new
41
- window.add_from_file(file)
42
-
43
- window.signal_connect("key_press_event") do |w, e|
44
- case e.keyval
45
- when Gdk::Keyval::GDK_KEY_n
46
- file = file_container.shift(file)
47
- window.add_from_file(file)
48
- when Gdk::Keyval::GDK_KEY_p
49
- file = file_container.pop(file)
50
- window.add_from_file(file)
51
- when Gdk::Keyval::GDK_KEY_r
52
- window.add_from_file(file)
53
- when Gdk::Keyval::GDK_KEY_q
54
- Gtk.main_quit
55
- end
56
- end
57
-
58
- window.signal_connect("destroy") do
59
- Gtk.main_quit
60
- end
39
+ window.add_container(file_container)
61
40
 
62
41
  Gtk.main
63
42
  end
@@ -70,16 +49,21 @@ module Mireru
70
49
  Keybind:
71
50
  n: next
72
51
  p: prev
52
+ r: reload
73
53
  q: quit
54
+
55
+ image:
56
+ f: fits window size
57
+ o: original size
74
58
  EOM
75
59
  @logger.info(message)
76
60
  end
77
61
 
78
62
  def write_empty_message
79
63
  message = <<-EOM
80
- Warning: valid file not found.
64
+ Warning: file not found.
81
65
  #{USAGE}
82
- Support file types: png, gif, jpeg(jpg). The others are...yet.
66
+ If no argument, then search current directory.
83
67
  EOM
84
68
  @logger.error(message)
85
69
  end
@@ -1,3 +1,3 @@
1
1
  module Mireru
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/mireru/widget.rb CHANGED
@@ -1,20 +1,24 @@
1
1
  require 'gtk3'
2
+ require 'gtksourceview3'
2
3
 
3
4
  module Mireru
5
+ class Error < StandardError
6
+ end
7
+
4
8
  class Widget
5
9
  class << self
6
- def create(file)
7
- case File.extname(file)
8
- when /\A\.(png|jpe?g|gif)\z/i
10
+ def create(file, width=nil, height=nil)
11
+ if image?(file)
9
12
  image = Gtk::Image.new
10
- image.file = file
13
+ pixbuf = Gdk::Pixbuf.new(file, width, height)
14
+ image.pixbuf = pixbuf
11
15
  widget = image
12
16
  else
13
- require 'gtksourceview3'
14
- text = File.open(file).read
15
- return sorry unless text.valid_encoding?
16
- buffer = GtkSource::Buffer.new
17
- buffer.text = text
17
+ begin
18
+ buffer = buffer_from_file(file)
19
+ rescue Mireru::Error
20
+ return sorry
21
+ end
18
22
  view = GtkSource::View.new(buffer)
19
23
  view.show_line_numbers = true
20
24
  lang = GtkSource::LanguageManager.new.get_language('ruby')
@@ -27,7 +31,24 @@ module Mireru
27
31
  widget
28
32
  end
29
33
 
34
+ def image?(file)
35
+ /\.(png|jpe?g|gif)\z/i =~ file
36
+ end
37
+
30
38
  private
39
+ def buffer_from_file(file)
40
+ text = File.open(file).read
41
+ buffer_from_text(text)
42
+ end
43
+
44
+ def buffer_from_text(text)
45
+ raise Mireru::Error unless text.valid_encoding?
46
+ text.encode!("utf-8") unless text.encoding == "utf-8"
47
+ buffer = GtkSource::Buffer.new
48
+ buffer.text = text
49
+ buffer
50
+ end
51
+
31
52
  def sorry
32
53
  image = Gtk::Image.new
33
54
  base_dir = File.join(File.dirname(__FILE__), "..", "..")
data/lib/mireru/window.rb CHANGED
@@ -3,13 +3,59 @@ require "mireru/widget"
3
3
 
4
4
  module Mireru
5
5
  class Window < Gtk::Window
6
+ def initialize
7
+ super
8
+ @scroll = Gtk::ScrolledWindow.new
9
+ @scroll.set_policy(:automatic, :automatic)
10
+ self.add(@scroll)
11
+ self.set_default_size(640, 640)
12
+ self.signal_connect("destroy") do
13
+ Gtk.main_quit
14
+ end
15
+ end
16
+
17
+ def add_container(container)
18
+ @container = container
19
+
20
+ @file = @container.shift
21
+ self.add_from_file(@file)
22
+
23
+ self.signal_connect("key_press_event") do |w, e|
24
+ case e.keyval
25
+ when Gdk::Keyval::GDK_KEY_n
26
+ @file = @container.shift(@file)
27
+ self.add_from_file(@file)
28
+ when Gdk::Keyval::GDK_KEY_p
29
+ @file = @container.pop(@file)
30
+ self.add_from_file(@file)
31
+ when Gdk::Keyval::GDK_KEY_r
32
+ self.add_from_file(@file)
33
+ when Gdk::Keyval::GDK_KEY_f
34
+ if Mireru::Widget.image?(@file)
35
+ pixbuf = Gdk::Pixbuf.new(@file, *self.size)
36
+ @widget.pixbuf = pixbuf
37
+ end
38
+ when Gdk::Keyval::GDK_KEY_o
39
+ if Mireru::Widget.image?(@file)
40
+ pixbuf = Gdk::Pixbuf.new(@file)
41
+ @widget.pixbuf = pixbuf
42
+ end
43
+ when Gdk::Keyval::GDK_KEY_q
44
+ Gtk.main_quit
45
+ end
46
+ end
47
+ end
48
+
6
49
  def add_from_file(file)
7
- self.remove(@widget) if @widget
8
- @widget = Mireru::Widget.create(file)
9
- self.add(@widget)
10
- self.show_all
50
+ @scroll.each {|child| @scroll.remove(child) }
51
+ @widget = Mireru::Widget.create(file, *self.size)
52
+ if @widget.is_a?(Gtk::Scrollable)
53
+ @scroll.add(@widget)
54
+ else
55
+ @scroll.add_with_viewport(@widget)
56
+ end
11
57
  self.title = File.basename(file)
12
- self.resize(1, 1)
58
+ self.show_all
13
59
  end
14
60
  end
15
61
  end
@@ -0,0 +1,29 @@
1
+ require "mireru/widget"
2
+
3
+ class TestWidget < Test::Unit::TestCase
4
+ def test_image?
5
+ assert_nil(Mireru::Widget.image?(__FILE__))
6
+ assert_not_nil(Mireru::Widget.image?("test/fixtures/nijip.png"))
7
+ end
8
+
9
+ def test_buffer_from_file_of_text
10
+ widget = Mireru::Widget.__send__(:buffer_from_file, __FILE__)
11
+ assert_equal(GtkSource::Buffer, widget.class)
12
+ end
13
+
14
+ def test_buffer_from_file_of_binary
15
+ assert_raise Mireru::Error do
16
+ Mireru::Widget.__send__(:buffer_from_file, "test/fixtures/nijip.png")
17
+ end
18
+ end
19
+
20
+ def test_buffer_from_text_of_utf8
21
+ widget = Mireru::Widget.__send__(:buffer_from_text, "御庭番")
22
+ assert_equal(GtkSource::Buffer, widget.class)
23
+ end
24
+
25
+ def test_buffer_from_text_of_sjis
26
+ widget = Mireru::Widget.__send__(:buffer_from_text, "御庭番".encode("SJIS"))
27
+ assert_equal(GtkSource::Buffer, widget.class)
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ require "mireru/window"
2
+
3
+ class TestWindow < Test::Unit::TestCase
4
+ def setup
5
+ @window = Mireru::Window.new
6
+ end
7
+
8
+ def test_add_container
9
+ container = %w(a, b, c)
10
+ mock(container).shift { "a" }
11
+ mock(@window).add_from_file("a")
12
+ @window.add_container(container)
13
+ end
14
+
15
+ def test_add_from_file_of_scrollable
16
+ file = __FILE__
17
+ mock(Mireru::Widget).create(file, *@window.size) do
18
+ Gtk::TextView.new
19
+ end
20
+ mock(@window).show_all
21
+ @window.add_from_file(file)
22
+ assert_equal(Gtk::ScrolledWindow, @window.child.class)
23
+ assert_equal(Gtk::TextView, @window.child.child.class)
24
+ end
25
+
26
+ def test_add_from_file_of_no_scrollable
27
+ file = "fixtures/nijip.png"
28
+ mock(Mireru::Widget).create(file, *@window.size) do
29
+ Gtk::Image.new
30
+ end
31
+ mock(@window).show_all
32
+ @window.add_from_file(file)
33
+ assert_equal(Gtk::ScrolledWindow, @window.child.class)
34
+ assert_equal(Gtk::Viewport, @window.child.child.class)
35
+ assert_equal(Gtk::Image, @window.child.child.child.class)
36
+ end
37
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mireru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masafumi Yokoyama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-07 00:00:00.000000000 Z
11
+ date: 2013-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gtk3
@@ -139,6 +139,8 @@ files:
139
139
  - test/test-container.rb
140
140
  - test/test-logger.rb
141
141
  - test/test-mireru.rb
142
+ - test/test-widget.rb
143
+ - test/test-window.rb
142
144
  homepage: https://github.com/myokoym/mireru
143
145
  licenses:
144
146
  - MIT
@@ -172,4 +174,6 @@ test_files:
172
174
  - test/test-container.rb
173
175
  - test/test-logger.rb
174
176
  - test/test-mireru.rb
177
+ - test/test-widget.rb
178
+ - test/test-window.rb
175
179
  has_rdoc: