#!/usr/bin/env ruby #============================================================================== # Copyright 2020 William McCumstie # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. #============================================================================== require "bundler/setup" require "output_mode" require 'erb' class DemoIndex < OutputMode::Formatters::Index constructor do register(header: 'Integer', row_color: [:yellow, :bold]) { |i| i } register(header: 'Standard', header_color: [:strikethrough] ) { 'always visible' } if verbose? register(header: 'Verbose') { 'verbose visible' } else register(header: 'Simplified') { 'simplified visible' } end if interactive? register(header: 'Interactive') { 'interactive visible' } else register(header: 'Non Interactive') { 'non-interactive visible' } end register(header: 'Yes/True') { true } register(header: 'No/False', row_color: [:clear]) { false } register(header: 'Missing') { nil } end end class DemoShow < OutputMode::Formatters::Show constructor do register(header: 'Integer') { |i| i } register(header: 'Standard') { 'always visible' } if verbose? register(header: 'Verbose') { 'verbose visible' } else register(header: 'Simplified') { 'simplified visible' } end if interactive? register(header: 'Interactive') { 'interactive visible' } else register(header: 'Non Interactive') { 'non-interactive visible' } end register(header: 'Yes/True', section: :boolean) { true } register(header: 'No/False', section: :boolean) { false } register(header: 'Missing') { nil } register(header: 'Tab') { "tab1\ttab2" } register(header: 'New line') { "line1\nline2" } end end data = [1, 2, 3] puts <<~EOF #============================================================================== #============================================================================== # INDEX OUTPUTS #============================================================================== #============================================================================== #============================================================================== # Default Demo Index # Simplified in interactive shells, verbose in non-interactive #============================================================================== #{DemoIndex.render(*data)} #============================================================================== # Demo Verbose Index #============================================================================== #{DemoIndex.render(*data, verbose: true)} #============================================================================== # Demo "Simplified" Index #============================================================================== #{DemoIndex.render(*data, verbose: false)} #============================================================================== # Force Interactive # Always print as if the shell is interactive #============================================================================== #{DemoIndex.render(*data, interactive: true)} #============================================================================== # Force Non-Interactive # Always print as if the shell is non-interactive #============================================================================== #{DemoIndex.render(*data, interactive: false)} #============================================================================== # Demo ASCII Index #============================================================================== #{DemoIndex.render(*data, ascii: true)} #============================================================================== #============================================================================== # SHOW OUTPUTS #============================================================================== #============================================================================== #============================================================================== # Default Settings # Simplified in interactive shells, verbose in non-interactive #============================================================================== #{DemoShow.render(data.first)} #============================================================================== # Demo Verbose Show #============================================================================== #{DemoShow.render(data.first, verbose: true)} #============================================================================== # Demo "Simplified" Show #============================================================================== #{DemoShow.render(data.first, verbose: false)} #============================================================================== # Force Interactive # Always print as if the shell is interactive #============================================================================== #{DemoShow.render(data.first, interactive: true)} #============================================================================== # Force Non-Interactive # Always print as if the shell is non-interactive #============================================================================== #{DemoShow.render(data.first, interactive: false)} #============================================================================== # Demo ASCII Index #============================================================================== #{DemoShow.render(data.first, ascii: true)} EOF