browser_gui 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +116 -0
- data/Rakefile +2 -0
- data/browser_gui.gemspec +17 -0
- data/examples/gui_optional.rb +23 -0
- data/examples/simple_form.rb +16 -0
- data/examples/simple_markdown.rb +19 -0
- data/lib/browser_gui/version.rb +3 -0
- data/lib/browser_gui.rb +22 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jack Veenstra
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# BrowserGui
|
2
|
+
|
3
|
+
Create simple GUI apps in Ruby, using the web browser as the GUI.
|
4
|
+
|
5
|
+
Use this gem with Sinatra to automatically open a web page that is controlled by
|
6
|
+
your Ruby script. This allows you to package up a simple Sinatra server as a
|
7
|
+
command-line script and share that script with others. When someone runs that
|
8
|
+
script, it opens up a new tab in a web browser and they can immediately start
|
9
|
+
interacting with it.
|
10
|
+
|
11
|
+
Works on Mac, Linux, and Windows.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Install using:
|
16
|
+
|
17
|
+
$ [sudo] gem install browser_gui
|
18
|
+
|
19
|
+
## Example Usage
|
20
|
+
|
21
|
+
### A simple HTML form
|
22
|
+
|
23
|
+
```
|
24
|
+
#!/usr/bin/env ruby
|
25
|
+
|
26
|
+
require "sinatra"
|
27
|
+
require "browser_gui"
|
28
|
+
|
29
|
+
FORM = '<form action="/action" method="post">
|
30
|
+
Name: <input type="text" name="text" size="30"><br><input type="submit">
|
31
|
+
</form>'
|
32
|
+
|
33
|
+
get "/" do
|
34
|
+
FORM
|
35
|
+
end
|
36
|
+
|
37
|
+
post "/action" do
|
38
|
+
params.inspect
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
### A simple Markdown example
|
43
|
+
|
44
|
+
```
|
45
|
+
#!/usr/bin/env ruby
|
46
|
+
|
47
|
+
require "sinatra"
|
48
|
+
require "browser_gui"
|
49
|
+
|
50
|
+
CONTENTS_MD =<<EOF
|
51
|
+
# An Example Using Markdown and Sinatra
|
52
|
+
* [Click me](/action/1)
|
53
|
+
* [No, click me](/action/2)
|
54
|
+
EOF
|
55
|
+
|
56
|
+
get "/" do
|
57
|
+
markdown CONTENTS_MD
|
58
|
+
end
|
59
|
+
|
60
|
+
get "/action/:id" do
|
61
|
+
"You clicked link #{params[:id]}"
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
## Using command-line options
|
66
|
+
|
67
|
+
```
|
68
|
+
#!/usr/bin/env ruby
|
69
|
+
|
70
|
+
# Handle options before 'require "sinatra"'
|
71
|
+
|
72
|
+
require "trollop"
|
73
|
+
|
74
|
+
opts = Trollop::options do
|
75
|
+
banner "Usage: #$0 [options]"
|
76
|
+
opt :gui, "Use gui", :default => true
|
77
|
+
opt :text, "text", :type => :string, :required => true
|
78
|
+
end
|
79
|
+
|
80
|
+
if opts[:gui] == false
|
81
|
+
puts opts[:text]
|
82
|
+
exit 0
|
83
|
+
end
|
84
|
+
|
85
|
+
require "sinatra"
|
86
|
+
require "browser_gui"
|
87
|
+
|
88
|
+
get "/" do
|
89
|
+
opts[:text]
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
If you want to support command-line options in your script, you need to process
|
94
|
+
the options before `require "sinatra"`. The example above uses the
|
95
|
+
[Trollop](http://trollop.rubyforge.org/) gem to parse the options. This example
|
96
|
+
supports a `--no-gui` option to disable the default behavior of opening a web
|
97
|
+
page, and it requires a `--text` string argument (or `-t`). If the example code
|
98
|
+
above is in the file `gui_optional.rb`, you could run it like this:
|
99
|
+
|
100
|
+
```
|
101
|
+
./gui_optional.rb -t "Hello, world." # Displays "Hello, world." in a web page
|
102
|
+
```
|
103
|
+
or
|
104
|
+
```
|
105
|
+
./gui_optional.rb -t "Hello, world." --no-gui # Displays "Hello, world" to the console.
|
106
|
+
```
|
107
|
+
|
108
|
+
You can still pass arguments to Sinatra (such as setting the port number), like this:
|
109
|
+
|
110
|
+
```
|
111
|
+
./gui_optional.rb -t "Hello, world." -- -p 5678
|
112
|
+
```
|
113
|
+
|
114
|
+
The double dash `--` stops the option processing in the script and passes the
|
115
|
+
remaining options to Sinatra. The `-t` option is parsed by the script and the
|
116
|
+
`-p` option is parsed by Sinatra.
|
data/Rakefile
ADDED
data/browser_gui.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/browser_gui/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jack Veenstra"]
|
6
|
+
gem.email = ["veenstra@gmail.com"]
|
7
|
+
gem.description = %q{Use with Sinatra to create command-line scripts that open the browser as a GUI.}
|
8
|
+
gem.summary = %q{Use the browser as a GUI.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "browser_gui"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = BrowserGui::VERSION
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Handle options before 'require "sinatra"'
|
4
|
+
|
5
|
+
require "trollop"
|
6
|
+
|
7
|
+
opts = Trollop::options do
|
8
|
+
banner "Usage: #$0 [options]"
|
9
|
+
opt :gui, "Use gui", :default => true
|
10
|
+
opt :text, "text", :type => :string, :required => true
|
11
|
+
end
|
12
|
+
|
13
|
+
if opts[:gui] == false
|
14
|
+
puts opts[:text]
|
15
|
+
exit 0
|
16
|
+
end
|
17
|
+
|
18
|
+
require "sinatra"
|
19
|
+
require "browser_gui"
|
20
|
+
|
21
|
+
get "/" do
|
22
|
+
opts[:text]
|
23
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "sinatra"
|
4
|
+
require "browser_gui"
|
5
|
+
|
6
|
+
FORM = '<form action="/action" method="post">
|
7
|
+
Name: <input type="text" name="text" size="30"><br><input type="submit">
|
8
|
+
</form>'
|
9
|
+
|
10
|
+
get "/" do
|
11
|
+
FORM
|
12
|
+
end
|
13
|
+
|
14
|
+
post "/action" do
|
15
|
+
params.inspect
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "sinatra"
|
4
|
+
require "browser_gui"
|
5
|
+
|
6
|
+
CONTENTS_MD =<<EOF
|
7
|
+
# An Example Using Markdown and Sinatra
|
8
|
+
* [Click me](/action/1)
|
9
|
+
* [No, click me](/action/2)
|
10
|
+
EOF
|
11
|
+
|
12
|
+
|
13
|
+
get "/" do
|
14
|
+
markdown CONTENTS_MD
|
15
|
+
end
|
16
|
+
|
17
|
+
get "/action/:id" do
|
18
|
+
"You clicked link #{params[:id]}"
|
19
|
+
end
|
data/lib/browser_gui.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "browser_gui/version"
|
2
|
+
|
3
|
+
unless defined? settings
|
4
|
+
warn "Cannot find Sinatra settings. Did you require \"sinatra\"?"
|
5
|
+
exit 1
|
6
|
+
end
|
7
|
+
|
8
|
+
module BrowserGui
|
9
|
+
def self.open_browser(url)
|
10
|
+
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ then
|
11
|
+
system("start #{url}")
|
12
|
+
elsif RbConfig::CONFIG['host_os'] =~ /darwin/ then
|
13
|
+
system("open #{url}")
|
14
|
+
elsif RbConfig::CONFIG['host_os'] =~ /linux/ then
|
15
|
+
system("xdg-open #{url}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Sinatra uses an "at_exit" block to start the server, so we have to use
|
21
|
+
# an "at_exit" block to open the browser.
|
22
|
+
at_exit { BrowserGui.open_browser("http://localhost:#{settings.port}/") }
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: browser_gui
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jack Veenstra
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-22 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Use with Sinatra to create command-line scripts that open the browser
|
15
|
+
as a GUI.
|
16
|
+
email:
|
17
|
+
- veenstra@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- browser_gui.gemspec
|
28
|
+
- examples/gui_optional.rb
|
29
|
+
- examples/simple_form.rb
|
30
|
+
- examples/simple_markdown.rb
|
31
|
+
- lib/browser_gui.rb
|
32
|
+
- lib/browser_gui/version.rb
|
33
|
+
homepage: ''
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.23
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Use the browser as a GUI.
|
57
|
+
test_files: []
|
58
|
+
has_rdoc:
|