Class: Roebe::ShowAvailableUsers

Inherits:
Base show all
Defined in:
lib/roebe/classes/show_available_users.rb

Overview

Roebe::ShowAvailableUsers

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
LJUST =
#

LJUST

#
18
PASSWD_FILE =
#

PASSWD_FILE

#
'/etc/passwd'

Constants inherited from Base

Base::COLOURS, Base::HOME_DIRECTORY_OF_USER_X, Base::N

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#append_this_onto_that_file, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #bold_blue, #burlywood, #call_next, #chdir, #cheerful_guy, #cliner, #convert_global_env, #copy_directory, #copy_file, #cornflowerblue, #create_directory, #crimson, #current_month?, #current_time?, #current_year?, #cyan, #darkcyan, #darkgreen, #darkslateblue, #deeppink, #delete_symlink, #e, #ecomment, #editor_to_use?, #efancy, #eimp, #ensure_main_encoding, #ensure_utf_encoding, #eparse, #erev, #esystem, #esystem_colourized, #ewarn, #firebrick, #get_current_day, #get_current_month, #get_files_from, #get_german_name_for_this_weekday, #gold, #grey, #hh_mm_ss, #home_dir?, #home_directory_of_user_x?, #is_a_file?, #is_an_image_file?, #is_archive?, #is_audio_file?, #is_in_studium_dir?, #is_multimedia_file?, #is_on_roebe?, #is_on_windows?, #is_symlink?, #is_this_a_ruby_file?, #is_video_file?, #iso_encoding?, #left_colour, #lightblue, #lightgreen, #lightseagreen, #lime, #localhost_to_data, #log_directory?, #main_encoding?, #mediumorchid, #mediumpurple, #mediumseagreen, #mediumspringgreen, #mediumturquoise, #mkdir, #mv, #n_days_in_this_month, #no_file_exists_at, #olivedrab, #open_in_browser, #opne, #opnn, #orange, #orchid, #palegoldenrod, #palevioletred, #pink, #powderblue, #programs_dir?, #random_html_colour, #rds, #read_file_in_iso_encoding, #read_file_via_the_default_encoding, #read_lines_via_iso_encoding, #readlines_with_main_encoding, #red, #register_sigint, #remove, #remove_directory, #remove_file, #rename_kde_konsole_tab, #replace_localhost_with_data, #report_pwd, #return_all_directories_from_this_directory, #return_current_directory, #return_dd_mm_yyyy, #return_file_or_directory_of, #return_files_from_pwd, #return_last_part_of_the_current_directory, #return_utc, #rev, #right_arrow?, #right_colour, #rosybrown, #royalblue, #ruby_base_directory?, #run_in_background, #run_rcfiles_then_run_ata_via_qdbus, #sandybrown, #sdir, #seagreen, #set_be_verbose, #set_xorg_buffer, #sfancy, #sfile, #silent_redirection?, #simp, #skyblue, #slateblue, #slategray, #springgreen, #steelblue, #swarn, #symlink, #teal, #to_camelcase, #tomato, #touch, #use_colours?, #verbose_truth, #weekday?, #write_what_into, #yellow

Methods included from Base::CommandlineArguments

#clear_commandline_arguments, #commandline_arguments_as_string?, #commandline_arguments_without_leading_hyphens?, #first_argument_without_leading_hyphens?, #has_an_argument_been_passed?, #return_commandline_arguments_with_leading_hyphens

Constructor Details

#initialize(commandline_arguments = nil, run_already = true) ⇒ ShowAvailableUsers

#

initialize

The entries in /etc/passwd are normally kept in this format:

(1) Username
(2) Password
(3) User ID (UID)
(4) Group ID (GID)
(5) User ID Info 
(6) Home directory
(7) Command/shell
#

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
# File 'lib/roebe/classes/show_available_users.rb', line 65

def initialize(
    commandline_arguments = nil,
    run_already           = true
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  case commandline_arguments
  when :dont_run_yet
    run_already = false
  end
  if run_already.is_a? Hash
    if run_already.has_key? :padding
      _ = run_already.delete(:padding)
      set_padding(_)
    end
    run_already = true # Restore the default run-behaviour here.
  end
  # ========================================================================= #
  # === Handle blocks next
  # ========================================================================= #
  if block_given?
    yielded = yield
    case yielded
    when :disable_colours,
         :no_colours
      disable_colours
    end
  end
  run if run_already
end

Class Method Details

.[](i = '') ⇒ Object

#

Roebe::ShowAvailableUsers[]

#

322
323
324
# File 'lib/roebe/classes/show_available_users.rb', line 322

def self.[](i = '')
  new(i)
end

.long_formatObject

#

Roebe::ShowAvailableUsers.long_format

#

329
330
331
332
333
# File 'lib/roebe/classes/show_available_users.rb', line 329

def self.long_format
  _ = new(nil, false)
  _.show_long_format(false) # Don't output the long format.
  _.string?
end

Instance Method Details

#commandline_arguments?Boolean

#

commandline_arguments?

#

Returns:

  • (Boolean)

155
156
157
# File 'lib/roebe/classes/show_available_users.rb', line 155

def commandline_arguments?
  @commandline_arguments
end

#disable_coloursObject

#

disable_colours

#

126
127
128
# File 'lib/roebe/classes/show_available_users.rb', line 126

def disable_colours
  @use_colours = false
end

#first_argument?Boolean

#

first_argument?

#

Returns:

  • (Boolean)

162
163
164
# File 'lib/roebe/classes/show_available_users.rb', line 162

def first_argument?
  @commandline_arguments.first
end
#

menu

#

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/roebe/classes/show_available_users.rb', line 246

def menu(
    i = commandline_arguments?
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i # case tag
    # ===================================================================== #
    # === long_format
    # ===================================================================== #
    when /^long(_|-)?format/, # Show in longformat style.
         'show_long_format','long','LONG'
      @display_format = :long
    when 'HELP','help'
      show_help; exit
    end
  end
end

#padding?Boolean

#

padding?

#

Returns:

  • (Boolean)

221
222
223
# File 'lib/roebe/classes/show_available_users.rb', line 221

def padding?
  @left_padding
end

#report_available_usersObject

#

report_available_users

This is the method that will report to the user.

#

283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/roebe/classes/show_available_users.rb', line 283

def report_available_users
  opnn { :no_trailing }
  e
  e
  e 'The available users ('+steelblue(@data.size.to_s)+
    ' in total), '+
    'as read in by '+
    sfile(PASSWD_FILE)+', are:'
  e
  @data.each {|user, userid|
    userid = userid.rjust(4) # Pad it a bit towards the right side.
    result = padding?+'- '+user.ljust(@ljust)
    result << ' (UID: '
    if @use_colours
      result << Colours.slateblue(userid.to_s)
    else
      result << userid.to_s
    end
    result << ')' 
    e result
  }; e
end

#resetObject

#

reset (reset tag)

#

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/roebe/classes/show_available_users.rb', line 101

def reset
  super()
  set_padding '  '
  set_ljust(LJUST)
  # ======================================================================= #
  # === @namespace
  # ======================================================================= #
  @namespace = NAMESPACE
  # ======================================================================= #
  # === @display_format
  # ======================================================================= #
  @display_format = :default # This is the default, obviously.
  # ======================================================================= #
  # === @use_colours
  # ======================================================================= #
  @use_colours = true
  # ======================================================================= #
  # === @_
  # ======================================================================= #
  @_ = ''.dup # ← This String will be returned.
end

#runObject

#

run (run tag)

#

309
310
311
312
313
314
315
316
317
# File 'lib/roebe/classes/show_available_users.rb', line 309

def run
  menu
  case @display_format
  when :long
    show_long_format
  else # Else, default.
    show_default
  end
end

#sanitize_dataObject

#

sanitize_data

#

208
209
210
211
212
213
214
215
216
# File 'lib/roebe/classes/show_available_users.rb', line 208

def sanitize_data
  @data.map! {|entry|
    splitted = entry.split(':')
    [splitted[0], splitted[2]]
  }
  @data = @data.sort_by {|entry| # sort tag
    '%05d' % entry[1]
  }
end

#set_commandline_arguments(i = '') ⇒ Object

#

set_commandline_arguments

#

147
148
149
150
# File 'lib/roebe/classes/show_available_users.rb', line 147

def set_commandline_arguments(i = '')
  i = [i].flatten.compact
  @commandline_arguments = i
end

#set_commandline_input(i) ⇒ Object

#

set_commandline_input

#

200
201
202
203
# File 'lib/roebe/classes/show_available_users.rb', line 200

def set_commandline_input(i)
  i = i[0] if i.is_a? Array
  @commandline_input = i
end

#set_ljust(i) ⇒ Object

#

set_ljust

#

140
141
142
# File 'lib/roebe/classes/show_available_users.rb', line 140

def set_ljust(i)
  @ljust = i
end

#set_padding(i = ' ') ⇒ Object

#

set_padding

#

169
170
171
172
# File 'lib/roebe/classes/show_available_users.rb', line 169

def set_padding(i = '  ')
  i = ' ' * i.to_i if i.is_a? Integer
  @left_padding = i
end

#show_defaultObject

#

show_default

#

268
269
270
271
272
273
274
275
276
# File 'lib/roebe/classes/show_available_users.rb', line 268

def show_default
  if File.exist? PASSWD_FILE
    @data = File.readlines(PASSWD_FILE)
    sanitize_data
    report_available_users
  else
    opnn; e 'We could not find the file at '+sfile(PASSWD_FILE)+'.'
  end
end

#show_helpObject

#

show_help (help tag)

#

235
236
237
238
239
240
241
# File 'lib/roebe/classes/show_available_users.rb', line 235

def show_help
  e
  e 'Not many options are known. The passwd file is at'
  e
  efile(PASSWD_FILE)
  e
end

#show_long_format(be_verbose = true) ⇒ Object

#

show_long_format

#

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/roebe/classes/show_available_users.rb', line 177

def show_long_format(be_verbose = true)
  line = 'Username '+(' ' * (@ljust - 8))+' | User ID   | Group   |  Real name        | Home directory'
  @_ << line+N
  if be_verbose
    e line
    cliner
  end
  Etc.passwd {|user|
    real_user_name = user.gecos.to_s
    home_dir = user.dir.to_s
    line = padding?+user.name.ljust(@ljust)+'| '+
           user.uid.to_s.rjust(10)+'| '+
           user.gid.to_s.ljust(8)+'| '+
           real_user_name.ljust(18)+'| '+
           home_dir.ljust(15)
    @_ << line+N
    e line if be_verbose
  }
end

#string?Boolean

#

string?

#

Returns:

  • (Boolean)

133
134
135
# File 'lib/roebe/classes/show_available_users.rb', line 133

def string?
  @_
end

#users?Boolean

#

users?

#

Returns:

  • (Boolean)

228
229
230
# File 'lib/roebe/classes/show_available_users.rb', line 228

def users?
  @data
end