Class: Roebe::ShowAvailableUsers

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

Overview

Roebe::ShowAvailableUsers

Constant Summary collapse

LJUST =
#

LJUST

#
18
PASSWD_FILE =
#

PASSWD_FILE

#
'/etc/passwd'

Constants inherited from Base

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#actions, #append_this_onto_that_file, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #burlywood, #call_next, #chdir, #cheerful_guy, #cliner, #complex_esystem, #convert_global_env, #copy_directory, #copy_file, #cornflowerblue, #create_directory, #crimson, #current_month?, #current_time?, #current_year?, #cyan, #darkcyan, #darkgreen, #darkkhaki, #darkslateblue, #deeppink, #delete_symlink, #do_not_use_the_base_colours, #do_use_the_base_colours, #e, #ecomment, #editor_to_use?, #efancy, #eimp, #emphasis, #ensure_main_encoding, #ensure_utf_encoding, #eparse, #erev, #esteelblue, #etomato, #ewarn, #exit_program, #firebrick, #get_current_day, #get_current_month, #get_files_and_directories, #get_files_from, #get_german_name_for_this_weekday, #gold, #green, #grey, #hh_mm_ss, #hh_mm_ss_day_month_year, #home_dir?, #home_directory_of_user_x?, #infer_the_namespace, #internal_hash?, #is_a_directory?, #is_a_file?, #is_a_jpg_file?, #is_an_image_file?, #is_archive?, #is_audio_file?, #is_in_studium_dir?, #is_multimedia_file?, #is_on_roebe?, #is_on_windows?, #is_studium_available?, #is_symlink?, #is_this_a_ruby_file?, #is_video_file?, #iso_encoding?, #le, #left_colour, #lightblue, #lightgreen, #lightseagreen, #lightsteelblue, #lime, #limegreen, #localhost_to_data, #log_directory?, #main_encoding?, #mediumorchid, #mediumpurple, #mediumseagreen, #mediumslateblue, #mediumspringgreen, #mediumturquoise, #mkdir_p, #mv, #n_days_in_this_month, #n_pages_in_this_pdf_file?, #namespace?, #no_file_exists_at, #ogrey, #olive, #olivedrab, #open_in_browser, #opne, #opnesystem, #opnn, #orange, #orchid, #orev, #palegoldenrod, #palevioletred, #pink, #powderblue, #programs_dir?, #project_base_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, #require_rescue, #reset_the_internal_hash, #return_all_directories_from_this_directory, #return_all_files_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, #roebe_log_directory?, #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, #simple_esystem, #skyblue, #slateblue, #slategray, #springgreen, #steelblue, #string_italic, #swarn, #symlink, #teal, #temp_dir?, #to_camelcase, #to_counted_hash, #tomato, #touch, #try_to_require_the_beautiful_url_gem, #try_to_require_the_html_template, #try_to_require_the_open_gem, #try_to_require_the_program_information_gem, #try_to_require_the_xorg_buffer, #use_colours?, #verbose_truth, #weekday?, #word_wrap, #write_what_into, #yellow

Methods included from Base::CommandlineArguments

#append_onto_the_commandline_arguments, #clear_commandline_arguments, #commandline_arguments?, #commandline_arguments_as_string?, #commandline_arguments_without_leading_hyphens?, #first_argument?, #first_argument_without_leading_hyphens?, #has_an_argument_been_passed?, #remove_hyphened_arguments_from_the_commandline_arguments, #return_commandline_arguments_with_leading_hyphens, #second_argument?, #set_commandline_arguments

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
#


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

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[]

#


292
293
294
# File 'lib/roebe/classes/show_available_users.rb', line 292

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

.long_formatObject

#

Roebe::ShowAvailableUsers.long_format

#


299
300
301
302
303
# File 'lib/roebe/classes/show_available_users.rb', line 299

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

Instance Method Details

#disable_coloursObject

#

disable_colours

#


118
119
120
# File 'lib/roebe/classes/show_available_users.rb', line 118

def disable_colours
  @use_colours = false
end
#

menu

#


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/roebe/classes/show_available_users.rb', line 216

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)


191
192
193
# File 'lib/roebe/classes/show_available_users.rb', line 191

def padding?
  @left_padding
end

#report_available_usersObject

#

report_available_users

This is the method that will report to the user.

#


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/roebe/classes/show_available_users.rb', line 253

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)

#


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/roebe/classes/show_available_users.rb', line 96

def reset
  super()
  infer_the_namespace
  set_padding '  '
  set_ljust(LJUST)
  # ======================================================================= #
  # === @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)

#


279
280
281
282
283
284
285
286
287
# File 'lib/roebe/classes/show_available_users.rb', line 279

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

#sanitize_dataObject

#

sanitize_data

#


178
179
180
181
182
183
184
185
186
# File 'lib/roebe/classes/show_available_users.rb', line 178

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_input(i) ⇒ Object

#

set_commandline_input

#


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

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

#set_ljust(i) ⇒ Object

#

set_ljust

#


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

def set_ljust(i)
  @ljust = i
end

#set_padding(i = ' ') ⇒ Object

#

set_padding

#


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

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

#show_defaultObject

#

show_default

#


238
239
240
241
242
243
244
245
246
# File 'lib/roebe/classes/show_available_users.rb', line 238

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

#show_helpObject

#

show_help (help tag)

#


205
206
207
208
209
210
211
# File 'lib/roebe/classes/show_available_users.rb', line 205

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

#


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/roebe/classes/show_available_users.rb', line 147

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)


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

def string?
  @_
end

#users?Boolean

#

users?

#

Returns:

  • (Boolean)


198
199
200
# File 'lib/roebe/classes/show_available_users.rb', line 198

def users?
  @data
end