Class: GithubCLI::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/github_cli/terminal.rb

Overview

Responsible for display and size detection.

Constant Summary collapse

DEFAULT_WIDTH =
80
DEFAULT_HEIGHT =
40

Class Method Summary collapse

Class Method Details

.build_command_output(cmd, indent = 3) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/github_cli/terminal.rb', line 48

def build_command_output(cmd, indent=3)
  prefix = " " * indent
  if cmd.namespace.empty?
    ["#{prefix + cmd.usage}", cmd.desc]
  else
    ["#{prefix + cmd.namespace} #{cmd.usage}", cmd.desc]
  end
end

.default_widthObject



12
13
14
# File 'lib/github_cli/terminal.rb', line 12

def default_width
  DEFAULT_WIDTH
end

.find_commands(pattern = nil) ⇒ Object



42
43
44
45
46
# File 'lib/github_cli/terminal.rb', line 42

def find_commands(pattern=nil)
  Command.all.select do |cmd|
    cmd if pattern && cmd.namespace =~ pattern
  end
end

.line(text) ⇒ Object



20
21
22
# File 'lib/github_cli/terminal.rb', line 20

def line(text)
  $stdout.print text
end

.newlineObject



24
25
26
27
# File 'lib/github_cli/terminal.rb', line 24

def newline
  $stdout.print "\n"
  nil
end

.paged_outputObject



29
30
31
32
33
34
35
36
# File 'lib/github_cli/terminal.rb', line 29

def paged_output
  if Pager.enabled?
    pager.page
    true
  else
    false
  end
end

.pagerObject



38
39
40
# File 'lib/github_cli/terminal.rb', line 38

def pager
  @pager ||= Pager.new
end


70
71
72
73
# File 'lib/github_cli/terminal.rb', line 70

def print_command_not_found(cmd)
  exec_name = GithubCLI.executable_name
  GithubCLI.ui.info "#{exec_name}: '#{cmd}' is not a #{exec_name} command. See '#{exec_name} --help'."
end


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/github_cli/terminal.rb', line 57

def print_commands(pattern=nil)
  Terminal.line "\n" + GithubCLI.program_name + "\n\n"

  commands = find_commands(pattern).map { |cmd| build_command_output cmd }

  if !commands.empty?
    GithubCLI.ui.info "Commands:"
    GithubCLI.ui.print_table commands, :truncate => true
  else
    print_command_not_found pattern.to_s.gsub(/\W|/, '')[3..-1]
  end
end


85
86
87
88
# File 'lib/github_cli/terminal.rb', line 85

def print_config(pattern=nil)
  GithubCLI.ui.info "Configuration options:"
  GithubCLI.ui.print_table GithubCLI.config.pretty(pattern)
end


75
76
77
78
79
80
81
82
83
# File 'lib/github_cli/terminal.rb', line 75

def print_usage(flags, command)
  GithubCLI.ui.info <<-TEXT

#{GithubCLI.program_name}

Usage: #{GithubCLI.executable_name} #{GithubCLI::Command::Usage.new(command, flags).format_usage }

  TEXT
end

.widthObject



16
17
18
# File 'lib/github_cli/terminal.rb', line 16

def width
  GithubCLI.ui.terminal_width
end