-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use a single Scarpe::App instance as self nearly everywhere #277
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
def printish(msg) | ||
#$stderr.puts msg | ||
File.open("/tmp/shoesy_stuff.txt", "a") do |f| | ||
f.write(msg + "\n") | ||
end | ||
end | ||
|
||
Shoes.app do | ||
printish "Self top: #{self.inspect}" # It's an instance of Shoes::App, yes | ||
stack do | ||
printish "Self in stack: #{self.inspect}" # Yup, here too | ||
end | ||
button("Clickity") do | ||
alert("You clicked me!") | ||
printish "Self in button handler: #{self.inspect}" # And here too | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class Scarpe | ||
class DocumentRoot < Scarpe::Widget | ||
include Scarpe::Background | ||
|
||
class DocumentRoot < Scarpe::Slot | ||
def initialize | ||
super | ||
|
||
create_display_widget | ||
end | ||
|
||
# This can be absolutely huge in console output, and it's frequently printed. | ||
# The default inspect string can be absolutely huge in console output, and it's frequently printed. | ||
def inspect | ||
"<Scarpe::DocumentRoot>" | ||
end | ||
|
||
alias_method :info, :puts | ||
|
||
def all_widgets | ||
out = [] | ||
|
||
to_add = self.children | ||
until to_add.empty? | ||
out.concat(to_add) | ||
to_add = to_add.flat_map(&:children).compact | ||
end | ||
|
||
out | ||
end | ||
|
||
# We can add various ways to find widgets here. | ||
# These are sort of like Shoes selectors, used for testing. | ||
def find_widgets_by(*specs) | ||
widgets = all_widgets | ||
app = Scarpe::App.instance | ||
specs.each do |spec| | ||
if spec.is_a?(Class) | ||
widgets.select! { |w| spec === w } | ||
elsif spec.is_a?(Symbol) | ||
s = spec.to_s | ||
case s[0] | ||
when "$" | ||
begin | ||
# I'm not finding a global_variable_get or similar... | ||
global_value = eval s | ||
widgets = [global_value] | ||
rescue | ||
raise "Error getting global variable: #{spec.inspect}" | ||
end | ||
when "@" | ||
if app.instance_variables.include?(spec) | ||
widgets = [app.instance_variable_get(spec)] | ||
else | ||
raise "Can't find top-level instance variable: #{spec.inspect}!" | ||
end | ||
else | ||
end | ||
else | ||
raise("Don't know how to find widgets by #{spec.inspect}!") | ||
end | ||
end | ||
widgets | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require "net/http" | ||
require "openssl" | ||
require "nokogiri" | ||
|
||
class Scarpe::Widget | ||
def download(url) | ||
Thread.new do | ||
begin | ||
uri = URI(url) | ||
http = Net::HTTP.new(uri.host, uri.port) | ||
http.use_ssl = true | ||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
|
||
request = Net::HTTP::Get.new(uri.request_uri) | ||
|
||
http.request(request) do |response| | ||
case response | ||
when Net::HTTPSuccess | ||
# content = response.body | ||
|
||
# headers = response.header | ||
|
||
html_get_title(content) | ||
else | ||
Scarpe.error("Failed to download content. Response code: #{response.code}") | ||
end | ||
end | ||
rescue StandardError => e | ||
Scarpe.error("Error occurred while downloading: #{e.message}") | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def html_get_title(content) | ||
doc = Nokogiri::HTML(content) | ||
|
||
title = doc.at_css("title")&.text&.strip || "" | ||
|
||
# headings = doc.css("h1").map(&:text) | ||
|
||
title | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
class Scarpe::Slot < Scarpe::Widget | ||
include Scarpe::Background | ||
include Scarpe::Border | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to me: make sure we have a test for download. Is this on the right class?