File: warn.rb

package info (click to toggle)
ruby-console 1.34.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: ruby: 1,509; makefile: 4
file content (33 lines) | stat: -rw-r--r-- 717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024-2025, by Samuel Williams.

require_relative "logger"

module Console
	# Whether the current fiber is emitting a warning.
	Fiber.attr_accessor :console_warn
	
	# Redirect warnings to Console.warn.
	module Warn
		# Redirect warnings to {Console.warn}.
		def warn(message, **options)
			fiber = Fiber.current
			
			# We do this to be extra pendantic about avoiding infinite recursion.
			return super if fiber.console_warn
			
			begin
				fiber.console_warn = true
				message.chomp!
				
				Console::Interface.instance.warn(message, **options)
			ensure
				fiber.console_warn = false
			end
		end
	end
	
	::Warning.extend(Warn)
end