Module: Cinch::Helpers

Included in:
Admin
Defined in:
lib/cinch/helpers/check_user.rb,
lib/cinch/helpers/table_format.rb,
lib/cinch/helpers/natural_language.rb,
lib/cinch/helpers/is_channel_disabled.rb

Defined Under Namespace

Modules: NaturalLanguage

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.table_format(array, params = {}) ⇒ String

TODO:

Have the ability to output either an array or string (params?)

Formats a supplied array into a list or table.

Parameters:

  • array (Array)

    The source array.

  • params (Hash) (defaults to: {})

    Options for generating the table.

Options Hash (params):

  • :splitchars (String) — default: '\t'

    Characters to split each individual item on.

  • :regexp (Regexp) — default: nil

    A Regexp to split each individual item on. If one is supplied, then :splitchars is ignored.

  • :justify (Array) — default: [:left]

    Changes the justification of individual table items. If there are more headers set than column justifications, the default will be used. Valid options are :left or :right.

  • :headers (Array) — default: []

    Sets the names for the table column headers. If left unset, no headers will be generated.

  • :gutter (Integer) — default: 4

    Sets the gutter size between columns and optionally the left gutter.

  • :left_gutter (Boolean) — default: false

    Generates a gutter on the left side of the generated table, equal to the same length as column gutters.

  • :display_indices (Boolean) — default: false

    Shows the index #+1 of the row.

  • :display_noitems (Boolean) — default: true

    Displays a notice if there are no items to show.

  • :noitems_msg (String) — default: "There is nothing to show."

    Sets the message for display_noitems.

  • :display_eot (Boolean) — default: true

    Displays a notice at the end of the table.

  • :eot_msg (String) — default: "End of results."

    Sets the message for display_eot.

Returns:

  • (String)

    The formatted string with line endings.

Author:


24
25
26
27
28
29
30
31
32
33
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
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
# File 'lib/cinch/helpers/table_format.rb', line 24

def Helpers.table_format array, params={}
  params = {
    splitchars: "\t", 
    regexp: nil,
    justify: [:left], # :left, :right
    headers: [], # ary (ex: ["a","b","c"])
    gutter: 4,
    left_gutter: false,
    display_indices: false,
    display_noitems: true,
    noitems_msg: "There is nothing to show.",
    display_eot: true,
    eot_msg: "End of results."
  }.merge!(params)
  
  source = array.dup # We cannot be altering our original object now, can we?

  # We use the str.tr method if our regexp param is nil.
  # Otherwise, we just use regexp.
  # That is, unless we have a hash. Then we just make the columns be the keys and values.
  if source.is_a? Array
    source.map! {|e| 
      if e.is_a? String
        if params[:regexp].nil? 
          e.tr(params[:splitchars], "\t").split("\t") 
        else
          e.match(params[:regexp])[1..-1].map {|e| e.nil? ? "" : e; } 
        end
      elsif e.is_a? Array
        next
      else
        e.to_s
      end
    }
  #=begin
  elsif source.is_a? Hash
    source_new = []
    source.each {|key,value|
      source_new << [key.to_s, value.to_s]
    }
    source = source_new
  #=end
  end

  # calculating the maximum length of each column
  column_lengths = []
 
 # optional index
  if params[:display_indices]
    params[:justify].unshift(:right)
    params[:headers].unshift("#")
    source.each_with_index.map {|e,i| e.unshift(i.succ)}
  end

  source.dup.unshift(params[:headers]).each {|e| 
    e.each_with_index {|item,index| 
      column_lengths[index] = [] if column_lengths[index].blank?
      column_lengths[index] << item.size 
    }
  }
  column_lengths.map! {|e| e.sort.last }

  data = []

  # Generating table headers
  if !params[:headers].blank?
    # Generating the headers, separators, etc.
    s_header = []
    s_separator = s_header.dup
    params[:headers].each_with_index {|e,i|
      s_header << "%#{"-" if params[:justify][i] == :left || params[:justify][i].nil?}#{column_lengths[i]}s" % e.to_s
      s_separator << "-"*column_lengths[i]
    }
    data << s_header.join(" "*params[:gutter]) << s_separator.join(" "*params[:gutter])
  end

  # Generating formatted table rows
  if source.is_a? Array
    source.each {|e|
      line = []
      e.each_with_index {|item,index|
        line << "%#{"-" if params[:justify][index] == :left || params[:justify][index].nil?}#{column_lengths[index]}s" % item.to_s
      }
      data << line.join(" "*params[:gutter])
    }
  end

  # Adding noitems_msg if there are well... no items.
  if source.empty? && params[:display_noitems] then data << params[:noitems_msg] end
  
  # Adding EOT message
  if params[:display_eot]
    data << "-" * params[:eot_msg].length
    data << params[:eot_msg]
  end

  # Adding a gutter to the left side
  if params[:left_gutter] === true then data.map! {|e| " "*params[:gutter] + e } end
  
  data.join("\n")
end

Instance Method Details

#check_user(channel, user, ignored_members = ["v"]) ⇒ Boolean

Checks a user's access in the specified channel.

Parameters:

  • channel (Channel, String)

    A Channel object or string that is the name of the channel. (i.e. #chat)

  • user (User)

    A User object

  • ignored_members (Array<String>) (defaults to: ["v"])

    An array of modes that this method will ignore

Returns:

  • (Boolean)

    Returns True if the user has any channel modes other than the ignored modechars

Author:


9
10
11
12
13
14
# File 'lib/cinch/helpers/check_user.rb', line 9

def check_user(channel, user, ignored_members=["v"])
  ignored_members ||= [] # If nil, assign an empty array.
  users = Channel(channel).users # All users from supplied channel
  modes = @bot.irc.isupport["PREFIX"].keys - ignored_members
  modes.any? {|mode| users[user].include?(mode)}
end

#is_channel_disabled?(c) ⇒ Boolean

Determines if the requested channel is on the plugin's disabled list

Parameters:

  • c (Channel, String)

    A Channel object or String

Returns:

  • (Boolean)

5
6
7
8
9
10
# File 'lib/cinch/helpers/is_channel_disabled.rb', line 5

def is_channel_disabled?(c)
  c = Channel(c) if !c.respond_to?(:name) # Converts input into a Channel unless the input is a Channel object itself
  if config.has_key?(:disabled_channels)
    config[:disabled_channels].any? {|chan| c == Channel(chan) }
  end
end