Module: Twig::Cli::Help

Defined in:
lib/twig/cli/help.rb

Overview

Handles printing help output for ‘twig help`.

Class Method Summary collapse

Class Method Details

.console_widthObject



5
6
7
# File 'lib/twig/cli/help.rb', line 5

def self.console_width
  80
end

.description(text, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/twig/cli/help.rb', line 34

def self.description(text, options = {})
  defaults = {
    :add_blank_line => false,
    :width => 40
  }
  options = defaults.merge(options)

  width = options[:width]
  words = text.gsub(/\n?\s+/, ' ').strip.split(' ')
  lines = []

  # Split words into lines
  while words.any?
    current_word      = words.shift
    current_word_size = Display.unformat_string(current_word).size
    last_line         = lines.last
    last_line_size    = last_line && Display.unformat_string(last_line).size

    if last_line_size && (last_line_size + current_word_size + 1 <= width)
      last_line << ' ' << current_word
    elsif current_word_size >= width
      lines << current_word[0...width]
      words.unshift(current_word[width..-1])
    else
      lines << current_word
    end
  end

  lines << ' ' if options[:add_blank_line]
  lines
end

.description_for_custom_property(option_parser, desc_lines, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/twig/cli/help.rb', line 66

def self.description_for_custom_property(option_parser, desc_lines, options = {})
  options[:trailing] ||= "\n"
  indent = '      '
  left_column_width = 29

  help_desc = desc_lines.inject('') do |desc, (left_column, right_column)|
    desc + indent +
    sprintf("%-#{left_column_width}s", left_column) + right_column + "\n"
  end

  Help.print_section(option_parser, help_desc, :trailing => options[:trailing])
end

.header(option_parser, text, separator_options = {}, header_options = {}) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/twig/cli/help.rb', line 158

def self.header(option_parser, text, separator_options = {}, header_options = {})
  separator_options[:trailing] ||= "\n\n"
  header_options[:underline]   ||= '='

  Help.print_section(
    option_parser,
    text + "\n" + (header_options[:underline] * text.size),
    separator_options
  )
end

.introObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/twig/cli/help.rb', line 9

def self.intro
  version_string = "Twig v#{Twig::VERSION}"

  intro = Help.paragraph(%{
    Twig is your personal Git branch assistant. It's a command-line tool
    for listing your most recent branches, and for remembering branch
    details for you, like issue tracker ids and todos. It also supports
    subcommands, like automatically fetching statuses from your issue
    tracking system.
  })

  intro = <<-BANNER.gsub(/^[ ]+/, '')

    #{'=' * version_string.size}
    #{version_string}
    #{'=' * version_string.size}

    #{intro}

    #{Twig::HOMEPAGE}
  BANNER

  intro + ' ' # Force extra blank line
end

.line_for_custom_property?(line) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/twig/cli/help.rb', line 79

def self.line_for_custom_property?(line)
  is_custom_property_except = (
    line.include?('--except-') &&
    !line.include?('--except-branch') &&
    !line.include?('--except-property') &&
    !line.include?('--except-PROPERTY')
  )
  is_custom_property_only = (
    line.include?('--only-') &&
    !line.include?('--only-branch') &&
    !line.include?('--only-property') &&
    !line.include?('--only-PROPERTY')
  )
  is_custom_property_width = (
    line =~ /--.+-width/ &&
    !line.include?('--branch-width') &&
    !line.include?('--PROPERTY-width')
  )

  is_custom_property_except ||
  is_custom_property_only ||
  is_custom_property_width
end

.paragraph(text) ⇒ Object



103
104
105
# File 'lib/twig/cli/help.rb', line 103

def self.paragraph(text)
  Help.description(text, :width => console_width).join("\n")
end


107
108
109
110
# File 'lib/twig/cli/help.rb', line 107

def self.print_line(option_parser, text)
  # Prints a single line of text without line breaks.
  option_parser.separator(text)
end


112
113
114
115
116
117
118
# File 'lib/twig/cli/help.rb', line 112

def self.print_paragraph(option_parser, text, separator_options = {})
  # Prints a long chunk of text with automatic word wrapping and a leading
  # line break.

  separator_options[:trailing] ||= ''
  Help.print_section(option_parser, Help.paragraph(text), separator_options)
end


120
121
122
123
124
125
# File 'lib/twig/cli/help.rb', line 120

def self.print_section(option_parser, text, options = {})
  # Prints text with leading and trailing line breaks.

  options[:trailing] ||= ''
  option_parser.separator "\n#{text}#{options[:trailing]}"
end

.subcommand_descriptionsObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/twig/cli/help.rb', line 127

def self.subcommand_descriptions
  descs = {
    'checkout-child'  => 'Checks out a branch\'s child branch, if any.',
    'checkout-parent' => 'Checks out a branch\'s parent branch.',
    'create-branch'   => 'Creates a branch and sets its `diff-branch` property to the previous branch name.',
    'diff'            => 'Shows the diff between a branch and its parent branch (`diff-branch`).',
    'gh-open'         => 'Opens a browser window for the current GitHub repository.',
    'gh-open-issue'   => 'Opens a browser window for a branch\'s GitHub issue, if any.',
    'gh-update'       => 'Updates each branch with the latest issue status on GitHub.',
    'help'            => 'Provides help for Twig and its subcommands.',
    'init'            => 'Runs all Twig setup commands.',
    'init-completion' => 'Initializes tab completion for Twig. Runs as part of `twig init`.',
    'init-config'     => 'Creates a default `~/.twigconfig` file. Runs as part of `twig init`.',
    'rebase'          => 'Rebases a branch onto its parent branch (`diff-branch`).'
  }

  line_prefix    = '- '
  gutter_width   = 2 # Space between columns
  names          = descs.keys.sort
  max_name_width = names.map { |name| name.length }.max
  names_width    = max_name_width + gutter_width
  descs_width    = Help.console_width - line_prefix.length - names_width
  desc_indent    = ' ' * (names_width + line_prefix.length)

  names.map do |name|
    line_prefix +
    sprintf("%-#{names_width}s", name) +
    Help.description(descs[name], :width => descs_width).join("\n" + desc_indent)
  end
end

.subheader(option_parser, text, separator_options = {}) ⇒ Object



169
170
171
# File 'lib/twig/cli/help.rb', line 169

def self.subheader(option_parser, text, separator_options = {})
  header(option_parser, text, separator_options, :underline => '-')
end