Class: Canis::HelpManager

Inherits:
Object show all
Defined in:
lib/canis/core/util/helpmanager.rb

Overview

manages the help file of an application and the inbuilt help the application provides

for the widgets.

Instance Method Summary collapse

Constructor Details

#initialize(form, config = {}, &block) ⇒ HelpManager

{{{



18
19
20
21
22
# File 'lib/canis/core/util/helpmanager.rb', line 18

def initialize form, config={}, &block
  @form = form
   #super
   #instance_eval &block if block_given?
end

Instance Method Details

#display_helpObject

Displays help provided by program. If no program is specified, then default help is displayed. If help was provided, then default help is also displayed on next page after program’s help



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/canis/core/util/helpmanager.rb', line 37

def display_help
  require 'canis/core/util/textutils'
  filename = CANIS_DOCPATH + "index.txt"
  stylesheet = CANIS_DOCPATH + "style_help.yml"
  # defarr contains default help
  if File.exists?(filename)
    defarr = File.open(filename,'r').read.split("\n")
    # convert help file into styles for use by tmux
    # quick dirty converter for the moment
    defarr = Canis::TextUtils::help2tmux defarr
  else
    arr = []
    arr << "  Could not find help file for application "
    arr << "    "
    arr << "Most applications provide the following keys, unless overriden:"
    arr << "    "
    arr << "    F10         -  exit application "
    arr << "    C-q         -  exit application "
    arr << "    ? (or M-?)  -  current widget key bindings  "
    arr << "    "
    arr << "    Alt-x       -  select commands  "
    arr << "    : (or M-:)  -  select commands  "
    arr << "    "
    defarr = arr
  end
  defhelp = true
  if @help_text
    defhelp = false
    arr = @help_text
    arr = arr.split("\n") if arr.is_a? String
    arr = Canis::TextUtils::help2tmux arr # FIXME can this happen automatically if it is help format
  else
    arr = defarr
  end
  #w = arr.max_by(&:length).length
  h = FFI::NCurses.LINES - 4
  w = FFI::NCurses.COLS - 10
  wbkgd = get_color($reversecolor, :black, :cyan)

  require 'canis/core/util/viewer'
  # this was the old layout that centered with a border, but was a slight bit confusing since the bg was the same
  # as the lower window.
  _layout = [h, w, 2, 4]
  sh = Ncurses.LINES-1
  sc = Ncurses.COLS-0
  # this is the new layout that is much like bline's command list. no border, a thick app header on top
  #  and no side margin
  #  Suppressing border means that title will not be updated on app_header, we have to do so FIXME
  _layout = [ h, sc, sh - h, 0]
  doc = TextDocument.new :text => arr, :content_type => :tmux, :stylesheet => stylesheet
  Canis::Viewer.view(doc, :layout => _layout, :close_key => KEY_F10, :title => "[ Help ]", :print_footer => true,
                    :app_header => true ) do |t, items|
    # would have liked it to be 'md' or :help
    #t.content_type = :tmux
    #t.stylesheet   = stylesheet
    t.suppress_borders = true
    t.print_footer = false
    t.bgcolor = :black
    t.bgcolor = 16
    t.color = :white
    ah = items[:header]
    t.bind(:PROPERTY_CHANGE) { |eve|
      # title is not a property, so we check if text has changed and then look for title.
      if eve.property_name == :text
        #$log.debug "  PROP NAME IS #{eve.property_name} , title is #{t.title} "
        ah.text_center = t.title
      end
    }
    #t.text_patterns[:link] = Regexp.new(/\[[^\]]\]/)
    t.text_patterns[:link] = Regexp.new(/\[\w+\]/)
    t.bind_key(KEY_TAB, "goto link") { t.next_regex(:link) }
    # FIXME bgcolor add only works if numberm not symbol
    t.bind_key(?a, "increment bgcolor") { t.bgcolor += 1 ; t.bgcolor = 1 if t.bgcolor > 256; 
                                  $log.debug " HELP BGCOLOR is #{t.bgcolor} ";
                                  t.clear_pad; t.render_all }
    t.bind(:PRESS){|eve| 
      link = nil
      s = eve.word_under_cursor
      if is_link?(t, s)
        link = get_link(t, s)
      end
      #alert "word under cursor is #{eve.word_under_cursor}, link is #{link}"
      if link
        arr = read_help_file link
        if arr
          doc = TextDocument.new :text => arr, :content_type => :tmux, :stylesheet => stylesheet, :title => link
          #t.add_content arr, :title => link
          t.add_content doc
          #items[:header].text_center = "[#{link}]" 
          t.buffer_last
        else
          alert "No help file for #{link}"
        end
      else
      end
    }

    # help was provided, so default help is provided in second buffer
    unless defhelp
      doc = TextDocument.new :text => defarr, :content_type => :tmux, :stylesheet => stylesheet, :title => " General Help "
      #t.add_content defarr, :title => ' General Help ', :stylesheet => stylesheet, :content_type => :tmux
      t.add_content doc
    end
  end
end


145
146
147
# File 'lib/canis/core/util/helpmanager.rb', line 145

def get_link t, s
  s.match(t.text_patterns[:link])[0].gsub!(/[\[\]]/,"")
end

#help_text(text = nil) ⇒ Object

super instance_eval &block if block_given?



23
24
25
26
27
28
# File 'lib/canis/core/util/helpmanager.rb', line 23

def help_text text=nil
  if text
    @help_text = text
  end
  return @help_text
end

#help_text=(text) ⇒ Object

Assign help text to variable

Parameters:

  • help (String)

    text is a string with newlines, or an Array. Will be split if String. May use markup for help files which is a very limited subset of markdown.



32
# File 'lib/canis/core/util/helpmanager.rb', line 32

def help_text=(text); help_text(text); end

#is_link?(t, s) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/canis/core/util/helpmanager.rb', line 142

def is_link? t, s
  s.index(t.text_patterns[:link]) >= 0
end

#read_help_file(link) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/canis/core/util/helpmanager.rb', line 148

def read_help_file link
  filename = CANIS_DOCPATH + "#{link}.txt"
  defarr = nil
  # defarr contains default help
  if File.exists?(filename)
    defarr = File.open(filename,'r').read.split("\n")
    # convert help file into styles for use by tmux
    # quick dirty converter for the moment
    defarr = Canis::TextUtils::help2tmux defarr
  end
end