Class: L

Inherits:
Object
  • Object
show all
Defined in:
lib/l.rb

Defined Under Namespace

Modules: VERSION

Class Method Summary collapse

Class Method Details

.build_command(args) ⇒ Object

Put together command for execution



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/l.rb', line 202

def self.build_command(args)
  command = ['ls']

  if args.length > 0
    if(File.file?(args[0]))
      command << @@file_options
      command[0] = 'less'
    else
      command << @@dir_options
    end
    command << args
  else
    command << @@dir_options
  end  
  command.flatten
end

.clear_optionsObject

reset options lists



43
44
45
46
47
# File 'lib/l.rb', line 43

def self.clear_options
  # used to build filtered command
  @@dir_options = Array.new
  @@file_options = Array.new
end

.init_dir_options(opts) ⇒ Object

Options that are passed on to ls:



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
142
143
144
145
# File 'lib/l.rb', line 66

def self.init_dir_options(opts)
  opts.separator ''      
  opts.separator 'Directory options:'
    
  opts.on('-a', '--all', 'do not ignore entries starting with .') do
    @@dir_options << '-a'
  end
  
  opts.on('-A', '--almost-all', 'do not list implied . and ..') do
    @@dir_options << '-A'
  end

  opts.on('-C', 'list entries by columns') do
    @@dir_options << '-C'
  end

  opts.on("--color=[WHEN]", 
          "control whether color is used to",
          "  distinguish file types. WHEN may be",
          "  'never', 'always', or 'auto'"
          ) do |wh|
    @@dir_options << '--color' + (wh) ? "=#{wh}" : ''
  end
  
  opts.on('-F', '--classify', 'append indicator (one of */=>@|) to entries') do
    @@dir_options << '-F'
  end

  opts.on('--file-type', "likewise, except do not append '*'") do
    @@dir_options << '--file-type'
  end

  # allowed formats for --format
  formats = 
    %w(verbose long commas horizontal across vertical single-column context)
    
  opts.on("--format=WORD", 
          Regexp.new(formats.join('|')),    # only allowed formats
          formats[0..4].join(', '),         # line one of usage
          '  ' + formats[5..-1].join(', ')  # line two of usage
          ) do |wd|
    @@dir_options << "--format=#{wd}"
  end

  opts.on('--inode', 'print the index number of each file') do
    @@dir_options << '-i'
  end

  opts.on("--ignore=PATTERN", 
          'do not list implied entries matching', 
          '  shell PATTERN'
          ) do |pattern|
    @@dir_options << "--ignore=#{pattern}"
  end

  opts.on('-l', 'use long listing format') do
    @@dir_options << '-l'
  end

  opts.on('-r', '--reverse', 'reverse order while sorting') do
    @@dir_options << '-r'
  end
  
  opts.on('-R', '--recursive', 'list subdirectories recursively') do
    @@dir_options << '-R'
  end
  
  opts.on('-t', 'sort by modification time') do
    @@dir_options << '-t'
  end

  opts.on('-v', 'sort by version') do
    @@dir_options << '-v'
  end
  
  opts.on('-1', 'list one file per line') do
    @@dir_options << '-1'
  end

end

.init_file_options(opts) ⇒ Object

Options that are passed on to less:



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/l.rb', line 148

def self.init_file_options(opts)
  opts.separator ''
  opts.separator 'File options:'

  opts.on('-e', '-E', '--quit-at-eof', 
          'quit at end of file') do
    @@file_options << '-e'
  end

  opts.on('-i', 
          'ignore case in searches that do not', 
          '  contain uppercase') do
    @@file_options << '-i'
  end

  opts.on('-I', 'ignore case in all searches') do
    @@file_options << '-I'
  end

  opts.on('--raw-control-chars',
          'output "raw" control characters') do
    @@file_options << '-r'
  end
  
end

.init_general_options(opts) ⇒ Object

Options for l itself:



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/l.rb', line 175

def self.init_general_options(opts)     
  opts.separator ''
  opts.separator 'General options:'

  opts.on('-V', '--version', 'show version info') do
    puts "l version #{L::VERSION::STRING}"
    exit
  end
  
  opts.on_tail('-h', '--help', 'show this message') do
    puts opts
    exit
  end
end

.init_options(args) ⇒ Object

Init options parser



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/l.rb', line 50

def self.init_options(args)  
  clear_options

  @@opts = OptionParser.new(args) do |opts|

    opts.banner = "Usage: l [OPTION]... [FILE/DIRECTORY]..."
    opts.separator 'Displays directory or file contents seamlessly with the'
    opts.separator 'same command (ls and less combined).'
    
    init_dir_options(opts)
    init_file_options(opts)
    init_general_options(opts)
  end
end

.parse(args) ⇒ Object

Run optionsparser, die with a message if error



191
192
193
194
195
196
197
198
199
# File 'lib/l.rb', line 191

def self.parse(args)
  begin
    @@opts.parse!(args)
  rescue OptionParser::ParseError => e
    warn "l: #{e}"
    warn "Try 'l --help' for more info."
    exit(1)
  end  
end

.run(args = ARGV) ⇒ Object

main entry point



35
36
37
38
39
40
# File 'lib/l.rb', line 35

def self.run(args = ARGV)
  init_options(args)
  parse(args)
  command = build_command(args)
  system(*command)
end