require_relative '../interface' module SctCore # Shell is the terminal output of things # For documentation for each of the methods open `interface.rb` class Shell < Interface require 'tty-screen' def log $stdout.sync = true end def writeToUser(message) puts message end def error(message) writeToUser(message.to_s.red) end def important(message) writeToUser(message.to_s.yellow) end def success(message) writeToUser(message.to_s.green) end def message(message) writeToUser(message.to_s) end def deprecated(message) writeToUser(message.to_s.deprecated) end def command(message) writeToUser("$ #{message}".cyan) end def command_output(message) actual = (message.split("\r").last || "") # as clearing the line will remove the `>` and the time stamp actual.split("\n").each do |msg| # prefix = msg.include?("▸") ? "" : "▸ " prefix = "" writeToUser(prefix + "" + msg.magenta) end end def verbose(message) message.to_s if $verbose end def header(message) format = format_string if message.length + 8 < TTY::Screen.width - format.length message = "--- #{message} ---" i = message.length else i = TTY::Screen.width - format.length end success("-" * i) success(message) success("-" * i) end def content_error(content, error_line) error_line = error_line.to_i return unless error_line > 0 contents = content.split(/\r?\n/).map(&:chomp) start_line = error_line - 2 < 1 ? 1 : error_line - 2 end_line = error_line + 2 < contents.length ? error_line + 2 : contents.length Range.new(start_line, end_line).each do |line| str = line == error_line ? " => " : " " str << line.to_s.rjust(Math.log10(end_line) + 1) str << ":\t#{contents[line - 1]}" error(str) end end ##################################################### # @!group Errors: Inputs ##################################################### def interactive? interactive = true interactive = false if $stdout.isatty == false return interactive end def input(message) verify_interactive!(message) ask("#{format_string}#{message.to_s.yellow}").to_s.strip end def confirm(message) verify_interactive!(message) agree("#{format_string}#{message.to_s.yellow} (y/n)", true) end def select(message, options) verify_interactive!(message) important(message) choose(*options) end def password(message) verify_interactive!(message) ask("#{format_string}#{message.to_s.yellow}") { |q| q.echo = "*" } end def user_error!(error_message, options = {}) writeToUser(error_message.yellow) end private def verify_interactive!(message) return if interactive? important(message) crash!("Could not retrieve response as sct runs in non-interactive mode") end end end