raindown 0.25.3 → 0.25.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 194b98d62de51917177324f44da4d16242935ab1a1e94cba0ff124b3a0dcec34
4
- data.tar.gz: 8725027a8065cb5d856db47f840e4b75cdbdc9dc293440063c5d417ea4dfa14f
3
+ metadata.gz: 902e7d196394f6fe68451f7e7835339fc2db78f1834ee5bc520de6ab89fde600
4
+ data.tar.gz: 47e342e68a8999f80af3c00ba226967073af9f79fe22fb12ad0decbb506135fd
5
5
  SHA512:
6
- metadata.gz: 4335c0d6b39bc51b413dbd4cddbb7336967a9e049f6d77411665336c5b2d7ed4b00f269217c335c6d3da196effb961f5d10a06df64b67abf2b739606576bf179
7
- data.tar.gz: ad850114a4f0029e85df6f554acfb04edcb4a981a36e0799952488cc8fa0066bdfdbcbeacd2eac815482bb5d6519e3f9e1be72bd2f3d5039bea27703a40db393
6
+ metadata.gz: 2005574b317e4cf0e9fe8583e5784d3338ac21748931e168d92c87fe4f9ec1c52a0124f016d5d47089907cadabfbcfcec0a5b8ca1e9f698f757a2a4f90cf00d1
7
+ data.tar.gz: 9280dcb507dde8d964922b7e101214755d0d55c9901546ac3c08b1821db69a33db8c18a1d1515957e47eae8b59960ecec4c2433b0f09ee9236466f74cf5d8b01
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open3'
4
+ require_relative 'install_progress'
5
+
6
+ module Raindown
7
+ module TUI
8
+ # The terminal app needs the curses gem, which compiles against ncurses. Raindown doesn't depend on it, so a
9
+ # framework like Raindeer that only renders Markdown never installs it. The first time the app starts without it,
10
+ # it installs it and carries on, and only asks for help when that fails.
11
+ module CursesInstaller
12
+ REQUIREMENT = '~> 1.7'
13
+ # RubyGems' own "gem" command, run by this Ruby, so curses installs where it'll load from.
14
+ GEM_COMMAND = 'require "rubygems/gem_runner"; Gem::GemRunner.new.run(ARGV)'
15
+ INSTALL = ['install', 'curses', '--version', REQUIREMENT, '--no-document'].freeze
16
+ PERMISSION = "You can't write to Ruby's gems. Install curses for yourself with: gem install curses --user-install"
17
+ HEADERS_NEEDED = 'curses compiles against ncurses, so install its headers, then: gem install curses'
18
+
19
+ # What to install for curses to compile, by system.
20
+ HEADERS = <<~TEXT
21
+ On macOS: xcode-select --install
22
+ On Debian, Ubuntu: sudo apt install libncurses-dev
23
+ On Fedora: sudo dnf install ncurses-devel
24
+ On Alpine: apk add ncurses-dev
25
+ TEXT
26
+
27
+ module_function
28
+
29
+ # Loads curses, installing it first when it's missing. It's false when curses can't be loaded, having said why and
30
+ # what to do. Without a terminal the app can't run, so it isn't installed then.
31
+ def ensure_loaded(input: $stdin, output: $stderr)
32
+ return true if loadable?
33
+ return explain(output, "Add gem 'curses' to your Gemfile for Raindown's terminal app.") if bundled?
34
+ return explain(output, "Raindown's terminal app needs the curses gem: gem install curses") unless input.tty?
35
+
36
+ install(output) && loadable?
37
+ end
38
+
39
+ def loadable?
40
+ gem 'curses', REQUIREMENT
41
+ require 'curses'
42
+ true
43
+ rescue LoadError
44
+ false
45
+ end
46
+
47
+ # Bundler only loads its bundle's gems, so one installed while running wouldn't load.
48
+ def bundled?
49
+ defined?(Bundler) && ENV.key?('BUNDLE_GEMFILE')
50
+ end
51
+
52
+ def install(output)
53
+ progress = InstallProgress.new(output)
54
+ succeeded, log = run_gem_install(progress)
55
+ return explain_failure(output, log) unless succeeded
56
+
57
+ progress.done
58
+ Gem::Specification.reset
59
+ true
60
+ end
61
+
62
+ # Runs "gem install curses" in its own process, drawing the bar meanwhile, and switching it to building once the
63
+ # gem has downloaded. It's whether it worked, and what it said.
64
+ def run_gem_install(progress)
65
+ log = +''
66
+ status = Open3.popen2e(Gem.ruby, '-e', GEM_COMMAND, *INSTALL) do |stdin, out, waiting|
67
+ stdin.close
68
+ reading = Thread.new { out.each_line { |line| progress.step = 'Building' if (log << line).include?('native extensions') } }
69
+ progress.show_until { !waiting.alive? }
70
+ reading.join
71
+ waiting.value
72
+ end
73
+ [status.success?, log]
74
+ end
75
+
76
+ # What went wrong and what to do: install for yourself without permission, or install ncurses' headers when
77
+ # curses wouldn't build.
78
+ def explain_failure(output, log)
79
+ return explain(output, PERMISSION) if log.include?('write permissions')
80
+
81
+ reason = log.lines.find { |line| line.start_with?('ERROR') }&.strip || 'gem install curses failed'
82
+ return explain(output, "#{reason}\n#{HEADERS_NEEDED}\n#{HEADERS}") if log.include?('native extension')
83
+
84
+ explain(output, "#{reason}\nTry again, or install it with: gem install curses")
85
+ end
86
+
87
+ def explain(output, message)
88
+ output.puts message
89
+ false
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raindown
4
+ module TUI
5
+ # A bar for installing curses, with its step and the seconds so far. Installing has no total to count towards, so
6
+ # the bar sweeps back and forth until it's done. Without a terminal to draw in, it says what it's doing once.
7
+ class InstallProgress
8
+ WIDTH = 24
9
+ MARK = '==='
10
+
11
+ attr_writer :step
12
+
13
+ def initialize(output)
14
+ @output = output
15
+ @step = 'Downloading'
16
+ @started = clock
17
+ end
18
+
19
+ # Draws the bar every tenth of a second until the block says the install is done.
20
+ def show_until
21
+ @output.puts("Installing curses for Raindown's terminal app...") unless @output.tty?
22
+ frame = 0
23
+ until yield
24
+ draw(frame) if @output.tty?
25
+ frame += 1
26
+ sleep(0.1)
27
+ end
28
+ clear
29
+ end
30
+
31
+ def done
32
+ @output.puts("Installed curses in #{seconds}s")
33
+ end
34
+
35
+ # The bar on one line, drawn over the last one: "Building curses for Raindown's terminal app [ === ] 12s".
36
+ def line(frame)
37
+ travel = WIDTH - MARK.size
38
+ position = frame % (travel * 2)
39
+ position = (travel * 2) - position if position > travel
40
+ bar = (' ' * position) + MARK + (' ' * (travel - position))
41
+ "#{@step} curses for Raindown's terminal app [#{bar}] #{seconds}s"
42
+ end
43
+
44
+ private
45
+
46
+ def draw(frame)
47
+ @output.print("\r\e[2K#{line(frame)}")
48
+ @output.flush
49
+ end
50
+
51
+ def clear
52
+ @output.print("\r\e[2K") if @output.tty?
53
+ end
54
+
55
+ def seconds
56
+ (clock - @started).round
57
+ end
58
+
59
+ def clock
60
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
61
+ end
62
+ end
63
+ end
64
+ end
data/lib/raindown/tui.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'projects'
4
4
  require_relative 'tui/navigator/navigator'
5
+ require_relative 'tui/curses_installer'
5
6
 
6
7
  module Raindown
7
8
  # The terminal UI, started with "raindown": a project's Markdown files beside the open file's outline,
@@ -15,11 +16,7 @@ module Raindown
15
16
  # watched instead, as curses' own error would be "Error opening terminal: unknown". Bare and execute, like --bare and
16
17
  # -x, allow bare directives in any file and run endpoints with no response as it writes back.
17
18
  def start(path, bare: false, execute: false)
18
- require_relative 'tui/terminal/screen'
19
- unless $stdin.tty? && $stdout.tty?
20
- warn("raindown needs a terminal, so it's watching instead")
21
- return false
22
- end
19
+ return false unless ready?
23
20
 
24
21
  path = File.expand_path(path)
25
22
  root = Projects.root(path) || (File.directory?(path) ? path : File.dirname(path))
@@ -42,5 +39,17 @@ module Raindown
42
39
  saves&.stop
43
40
  workers&.stop
44
41
  end
42
+
43
+ # Curses is loaded, installing it first if need be, and there's a terminal to draw in. Without one, like in a
44
+ # script or a service, it says so once curses has loaded, as curses' own error would be "Error opening terminal".
45
+ def ready?
46
+ return false unless CursesInstaller.ensure_loaded
47
+
48
+ require_relative 'tui/terminal/screen'
49
+ return true if $stdin.tty? && $stdout.tty?
50
+
51
+ warn("raindown needs a terminal, so it's watching instead")
52
+ false
53
+ end
45
54
  end
46
55
  end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Raindown
4
- VERSION = '0.25.3'
4
+ VERSION = '0.25.4'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raindown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.25.3
4
+ version: 0.25.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
@@ -51,20 +51,6 @@ dependencies:
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
- - !ruby/object:Gem::Dependency
55
- name: curses
56
- requirement: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: '1.7'
61
- type: :runtime
62
- prerelease: false
63
- version_requirements: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '1.7'
68
54
  - !ruby/object:Gem::Dependency
69
55
  name: dirload
70
56
  requirement: !ruby/object:Gem::Requirement
@@ -300,6 +286,7 @@ files:
300
286
  - lib/raindown/tui/blueprints/tracing.rb
301
287
  - lib/raindown/tui/cloud.rb
302
288
  - lib/raindown/tui/code_box.rb
289
+ - lib/raindown/tui/curses_installer.rb
303
290
  - lib/raindown/tui/endpoints.rb
304
291
  - lib/raindown/tui/events.rb
305
292
  - lib/raindown/tui/feelings/batch.rb
@@ -325,6 +312,7 @@ files:
325
312
  - lib/raindown/tui/feelings/weekly_batch.rb
326
313
  - lib/raindown/tui/file_tree.rb
327
314
  - lib/raindown/tui/highlighter.rb
315
+ - lib/raindown/tui/install_progress.rb
328
316
  - lib/raindown/tui/layout.rb
329
317
  - lib/raindown/tui/navigator/commands.rb
330
318
  - lib/raindown/tui/navigator/focus.rb