Skip to content

Commit 76b5eac

Browse files
committed
Add download_update! and update actions to set DEVELOPMENT and VERSION.
1 parent 14e2186 commit 76b5eac

6 files changed

Lines changed: 89 additions & 7 deletions

File tree

.github/workflows/release.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ jobs:
2323
echo "# Copyright (c) 2024 Marc Heiligers" >> input.rb
2424
echo "# See https://github.com/marcheiligers/dr-input" >> input.rb
2525
echo "" >> input.rb
26-
grep -v require_relative lib/input.rb >> input.rb
26+
echo "module Input; DEVELOPMENT = false; VERSION = '$VERSION'; end" >> input.rb
27+
echo "" >> input.rb
28+
grep -v -E "(require_relative|DEVELOPMENT)" lib/input.rb >> input.rb
2729
for file in ${files}
2830
do
2931
cat "lib/${file}" >> input.rb

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# v0.1.0 - 28 September 2026
2+
3+
* Updated release workflow to use maintained actions.
4+
* Removed references to $clipboard.
5+
* Added `Input::DEVELOPMENT` (false in released versions) and `Input::VERSION`.
6+
* Added `Input.download_update!` which will fetch the most recent version from GitHub.
7+
18
# v0.0.25 - 26 September 2025
29

310
* Fixed a crashing bug when assigning `nil` to an input. (thanks @vinnydiehl on Discord for reporting)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ The argument list below will list `prompt_color` but not the individual `prompt_
123123
* `#find(text)` - Selects the searched for text if found
124124
* `#find_next` - Selects the next instance of the currently selected text
125125
* `#find_prev` - Selects the previous instance of the currently selected text
126-
* `#cut` - Cut selection to `$clipboard`
127-
* `#copy` - Copy selection to `$clipboard`
128-
* `#paste` - Paste value in `$clipboard`
126+
* `#cut` - Cut selection to system clipboard
127+
* `#copy` - Copy selection to system clipboard
128+
* `#paste` - Paste value in system clipboard
129129
* `#move_to_start` - Move to the start of the current line
130130
* `#move_word_left` - Move the cursor a word to the left
131131
* `#move_char_left` - Move the cursor a character to the left

lib/input.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
require_relative 'line_collection.rb'
66
require_relative 'font_style.rb'
77
require_relative 'base.rb'
8+
require_relative 'update.rb'
89
require_relative 'text.rb'
910
require_relative 'multiline.rb'
1011
require_relative 'menu.rb'
1112
require_relative 'console.rb'
1213

13-
$clipboard = ''
14+
module Input; DEVELOPMENT = true; VERSION = '0.0.0'; end
1415

1516
# TODO: Drag selected text
1617
# TODO: Render Squiggly lines

lib/update.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module Input
2+
# This works by:
3+
# 1. Overriding the `GTK::Runtime#tick_core method` (example found in lowrez.rb)
4+
# 2. It fetches 'https://github.com/marcheiligers/dr-input/releases/latest' which will redirect to the latest release.
5+
# DRGKT follows redirects, so we get the HTML from the final destination.
6+
# 3. That HTML contains OpenGraph tags including:
7+
# `<meta property="og:url" content="/marcheiligers/dr-input/releases/tag/v0.0.25" />`
8+
# which contains the latest version number.
9+
# 4. That then allows us to fetch that version from the releases using $gtk.download_stb_rb_raw
10+
# 5. Finally it aliases the original `tick_core` method back, and cleans up the methods and instance variables created.
11+
# State is maintained through the existence of method aliases and instance variables, which are removed on completion.
12+
# Error handling ensures everything is cleaned up if there is a failure.
13+
14+
def self.download_update!
15+
return puts 'Already busy updating' if GTK::Runtime.instance_variable_defined?(:@__input_file_path)
16+
17+
GTK::Runtime.alias_method(:__input_orig_tick_core, :tick_core)
18+
GTK::Runtime.instance_variable_set(:@__input_file_path, Input::DEVELOPMENT ? 'input.rb' : 'file.rb')
19+
20+
GTK::Runtime.define_method(:tick_core) do
21+
__input_orig_tick_core
22+
23+
if !instance_variable_defined?(:@__input_version_response)
24+
@__input_version_response = $gtk.http_get 'https://github.com/marcheiligers/dr-input/releases/latest'
25+
elsif @__input_version_response[:complete] && !instance_variable_defined?(:@__input_download_response)
26+
raise "Received status code #{@__input_version_response[:http_response_code]}" unless @__input_version_response[:http_response_code] == 200
27+
28+
data = @__input_version_response[:response_data]
29+
search = '<meta property="og:url" content="/marcheiligers/dr-input/releases/tag/v'
30+
start_char = data.index(search) + search.length
31+
end_char = data.index('"', start_char)
32+
@__input_version = data.slice(start_char...end_char)
33+
puts "Found version #{@__input_version}."
34+
raise "Remote version is the same as local #{@__input_version}" if @__input_version == Input::VERSION
35+
36+
url = "https://github.com/marcheiligers/dr-input/releases/download/v#{@__input_version}/input.rb"
37+
@__input_download_response = $gtk.http_get url
38+
puts "download: #{@__input_download_response.inspect}"
39+
elsif instance_variable_defined?(:@__input_download_response) && @__input_download_response[:complete]
40+
raise "Received status code #{@__input_download_response[:http_response_code]}" unless @__input_download_response[:http_response_code] == 200
41+
42+
path = self.class.instance_variable_get(:@__input_file_path)
43+
data = @__input_download_response[:response_data]
44+
$gtk.write_file path, data
45+
46+
puts "Updated to version #{@__input_version} at #{path}"
47+
__input_cleanup_update
48+
end
49+
rescue StandardError => e
50+
puts "Failed to download update: #{e.message}"
51+
__input_cleanup_update
52+
end
53+
54+
GTK::Runtime.define_method(:__input_cleanup_update) do
55+
self.class.alias_method :tick_core, :__input_orig_tick_core
56+
self.class.remove_method :__input_orig_tick_core
57+
self.class.remove_method :__input_cleanup_update
58+
remove_instance_variable :@__input_version_response if instance_variable_defined? :@__input_version_response
59+
remove_instance_variable :@__input_version if instance_variable_defined? :@__input_version
60+
remove_instance_variable :@__input_download_response if instance_variable_defined? :@__input_download_response
61+
self.class.remove_instance_variable :@__input_file_path if self.class.instance_variable_defined? :@__input_file_path
62+
end
63+
end
64+
end

release

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
#!/usr/bin/env bash
22

33
rm input.rb | true
4-
files=$(grep require_relative lib/input.rb | gsed -En "s/require_relative\s+'(\w+\.rb)'/\1/p")
54

6-
grep -v require_relative lib/input.rb >> input.rb
5+
export VERSION=$(head -n1 CHANGELOG.md | gsed -En 's/# v([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+).*/\1/p')
76

7+
files=$(grep require_relative lib/input.rb | gsed -En "s/require_relative\s+'(\w+\.rb)'/\1/p")
8+
echo "# dr-input $VERSION" >> input.rb
9+
echo "# MIT Licensed" >> input.rb
10+
echo "# Copyright (c) 2024 Marc Heiligers" >> input.rb
11+
echo "# See https://github.com/marcheiligers/dr-input" >> input.rb
12+
echo "" >> input.rb
13+
echo "module Input; DEVELOPMENT = false; VERSION = '$VERSION'; end" >> input.rb
14+
echo "" >> input.rb
15+
grep -v -E "(require_relative|DEVELOPMENT)" lib/input.rb >> input.rb
816
for file in ${files}
917
do
1018
cat "lib/${file}" >> input.rb

0 commit comments

Comments
 (0)