Class: Gloo::App::Help

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/app/help.rb

Constant Summary collapse

APPLICATION =
'application'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHelp

Set up the help system. We won’t load help topics until we know we need them.



22
23
24
# File 'lib/gloo/app/help.rb', line 22

def initialize
  @topics = nil
end

Instance Attribute Details

#topicsObject

Returns the value of attribute topics.



16
17
18
# File 'lib/gloo/app/help.rb', line 16

def topics
  @topics
end

Instance Method Details

#get_topic_data(topic) ⇒ Object

Get topic data.



116
117
118
119
120
121
122
123
# File 'lib/gloo/app/help.rb', line 116

def get_topic_data( topic )
  lazy_load_index

  topic_file = @topics[ topic ]
  return nil unless topic_file

  File.read topic_file
end

#lazy_load_indexObject

Lazy load topic index. We’ll only load the index the first time a help topic is requested. After that we’ll use the cached index.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/gloo/app/help.rb', line 130

def lazy_load_index
  return if @topics

  @topics = {}
  $log.debug 'loading help file index...'

  pn = File.dirname( File.absolute_path( __FILE__ ) )
  pn = File.dirname( pn )

  root = File.join( pn, 'help', '**/*.txt' )
  Dir.glob( root ).each do |txt_file|
    key = File.basename( txt_file, '.txt' )
    @topics[ key ] = txt_file
  end

  root = File.join( pn, 'help', '**/*.md' )
  Dir.glob( root ).each do |md_file|
    key = File.basename( md_file, '.md' )
    @topics[ key ] = md_file
  end

  $log.debug "Found #{@topics.count} help files"
end

#page_topic(topic) ⇒ Object

Show a help topic, optionally paginaged. If the content of the help page fits on a single screen, it won’t paginate.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gloo/app/help.rb', line 59

def page_topic( topic )
  return if $engine.args.quiet?

  data = self.get_topic_data( topic )
  page = data.lines.count > Settings.page_size
  if topic_is_md?( topic )
    show_markdown_data( data, page )
  else
    show_text_data( data, page )
  end
end

#show_app_helpObject

Show application help. This is the what is shown when help is run from the CLI.



30
31
32
33
# File 'lib/gloo/app/help.rb', line 30

def show_app_help
  puts Info.display_title unless $engine.args.quiet?
  self.show_topic APPLICATION
end

#show_markdown_data(data, page = false) ⇒ Object

Show the markdown data.



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gloo/app/help.rb', line 101

def show_markdown_data( data, page = false )
  md = TTY::Markdown.parse data

  if page
    pager = TTY::Pager.new
    pager.page( md )
  else
    puts md
    puts
  end
end

#show_text_data(data, page = false) ⇒ Object

Show the markdown data.



88
89
90
91
92
93
94
95
96
# File 'lib/gloo/app/help.rb', line 88

def show_text_data( data, page = false )
  if page
    pager = TTY::Pager.new
    pager.page( data )
  else
    puts data.white
    puts
  end
end

#show_topic(topic) ⇒ Object

Show a help topic.



74
75
76
77
78
79
80
81
82
83
# File 'lib/gloo/app/help.rb', line 74

def show_topic( topic )
  return if $engine.args.quiet?

  data = self.get_topic_data( topic )
  if topic_is_md?( topic )
    show_markdown_data data
  else
    show_text_data data
  end
end

#topic?(name) ⇒ Boolean

Check to see if there is a topic by the given name.

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/gloo/app/help.rb', line 38

def topic?( name )
  lazy_load_index

  return @topics.key?( name )
end

#topic_is_md?(name) ⇒ Boolean

Is the current topic Markdown?

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/gloo/app/help.rb', line 47

def topic_is_md?( name )
  lazy_load_index

  topic_file = @topics[ name ]
  return topic_file.end_with? '.md'
end