Class: Tb::Cmd::Ls
- Inherits:
-
Object
- Object
- Tb::Cmd::Ls
- Defined in:
- lib/tb/cmd_ls.rb
Instance Attribute Summary collapse
-
#fail ⇒ Object
readonly
Returns the value of attribute fail.
Instance Method Summary collapse
-
#initialize(y, opts) ⇒ Ls
constructor
A new instance of Ls.
- #ls_dir(dir, st) ⇒ Object
- #ls_file(path, st) ⇒ Object
- #ls_get_stat(path) ⇒ Object
- #ls_info_atime(path, st) ⇒ Object
- #ls_info_blksize(path, st) ⇒ Object
- #ls_info_blocks(path, st) ⇒ Object
- #ls_info_ctime(path, st) ⇒ Object
- #ls_info_dev(path, st) ⇒ Object
- #ls_info_filemode(path, st) ⇒ Object
- #ls_info_filename(path, st) ⇒ Object
- #ls_info_gid(path, st) ⇒ Object
- #ls_info_group(path, st) ⇒ Object
- #ls_info_ino(path, st) ⇒ Object
- #ls_info_mode(path, st) ⇒ Object
- #ls_info_mtime(path, st) ⇒ Object
- #ls_info_nlink(path, st) ⇒ Object
- #ls_info_rdev(path, st) ⇒ Object
- #ls_info_size(path, st) ⇒ Object
- #ls_info_symlink(path, st) ⇒ Object
- #ls_info_uid(path, st) ⇒ Object
- #ls_info_user(path, st) ⇒ Object
- #ls_long_header ⇒ Object
- #ls_long_info(path, st) ⇒ Object
- #ls_run(path) ⇒ Object
- #real_pathname_string(str) ⇒ Object
- #set_header ⇒ Object
Constructor Details
#initialize(y, opts) ⇒ Ls
Returns a new instance of Ls.
73 74 75 76 77 |
# File 'lib/tb/cmd_ls.rb', line 73 def initialize(y, opts) @y = y @opts = opts @fail = false end |
Instance Attribute Details
#fail ⇒ Object (readonly)
Returns the value of attribute fail.
78 79 80 |
# File 'lib/tb/cmd_ls.rb', line 78 def fail @fail end |
Instance Method Details
#ls_dir(dir, st) ⇒ Object
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 146 |
# File 'lib/tb/cmd_ls.rb', line 98 def ls_dir(dir, st) begin entries = Dir.entries(dir) rescue SystemCallError @fail = true warn "tb: #{$!}: #{dir}" return end entries.map! {|filename| real_pathname_string(filename) } entries = entries.sort_by {|filename| Tb::Func.smart_cmp_value(filename) } if @opts[:a] || @opts[:A] entries1, entries2 = entries.partition {|filename| /\A\./ =~ filename } entries0, entries1 = entries1.partition {|filename| filename == '.' || filename == '..' } entries0.sort! if @opts[:A] entries = entries1 + entries2 else entries = entries0 + entries1 + entries2 end else entries.reject! {|filename| /\A\./ =~ filename } end if !@opts[:R] entries.each {|filename| ls_file(dir + filename, nil) } else dirs = [] entries.each {|filename| path = dir + filename st2 = ls_get_stat(path) next if !st2 if filename == '.' || filename == '..' if dir.to_s != '.' path = Pathname(dir.to_s + "/" + filename) end ls_file(path, st2) elsif st2.directory? dirs << [path, st2] else ls_file(path, st2) end } dirs.each {|path, st2| ls_file(path, st2) ls_dir(path, st2) } end end |
#ls_file(path, st) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/tb/cmd_ls.rb', line 148 def ls_file(path, st) if 0 < @opts[:l] if !st st = ls_get_stat(path) return if !st end @y.yield ls_long_info(path, st) else @y.yield({'filename' => path.to_s}) end end |
#ls_get_stat(path) ⇒ Object
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/tb/cmd_ls.rb', line 160 def ls_get_stat(path) begin st = path.lstat rescue SystemCallError @fail = true warn "tb: #{$!}: #{path}" return nil end st end |
#ls_info_atime(path, st) ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/tb/cmd_ls.rb', line 252 def ls_info_atime(path, st) if 1 < @opts[:l] st.atime.iso8601(9) else st.atime.iso8601 end end |
#ls_info_blksize(path, st) ⇒ Object
193 |
# File 'lib/tb/cmd_ls.rb', line 193 def ls_info_blksize(path, st) st.blksize end |
#ls_info_blocks(path, st) ⇒ Object
194 |
# File 'lib/tb/cmd_ls.rb', line 194 def ls_info_blocks(path, st) st.blocks end |
#ls_info_ctime(path, st) ⇒ Object
268 269 270 271 272 273 274 |
# File 'lib/tb/cmd_ls.rb', line 268 def ls_info_ctime(path, st) if 1 < @opts[:l] st.ctime.iso8601(9) else st.ctime.iso8601 end end |
#ls_info_dev(path, st) ⇒ Object
185 |
# File 'lib/tb/cmd_ls.rb', line 185 def ls_info_dev(path, st) sprintf("0x%x", st.dev) end |
#ls_info_filemode(path, st) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/tb/cmd_ls.rb', line 196 def ls_info_filemode(path, st) entry_type = case st.ftype when "file" then '-' when "directory" then 'd' when "characterSpecial" then 'c' when "blockSpecial" then 'b' when "fifo" then 'p' when "link" then 'l' when "socket" then 's' when "unknown" then '?' else '?' end m = st.mode sprintf("%s%c%c%c%c%c%c%c%c%c", entry_type, (m & 0400 == 0 ? ?- : ?r), (m & 0200 == 0 ? ?- : ?w), (m & 0100 == 0 ? (m & 04000 == 0 ? ?- : ?S) : (m & 04000 == 0 ? ?x : ?s)), (m & 0040 == 0 ? ?- : ?r), (m & 0020 == 0 ? ?- : ?w), (m & 0010 == 0 ? (m & 02000 == 0 ? ?- : ?S) : (m & 02000 == 0 ? ?x : ?s)), (m & 0004 == 0 ? ?- : ?r), (m & 0002 == 0 ? ?- : ?w), (m & 0001 == 0 ? (m & 01000 == 0 ? ?- : ?T) : (m & 01000 == 0 ? ?x : ?t))) end |
#ls_info_filename(path, st) ⇒ Object
276 277 278 |
# File 'lib/tb/cmd_ls.rb', line 276 def ls_info_filename(path, st) path end |
#ls_info_gid(path, st) ⇒ Object
190 |
# File 'lib/tb/cmd_ls.rb', line 190 def ls_info_gid(path, st) st.gid end |
#ls_info_group(path, st) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/tb/cmd_ls.rb', line 239 def ls_info_group(path, st) gid = st.gid begin gr = Etc.getgrgid(gid) rescue ArgumentError end if gr gr.name else gid.to_s end end |
#ls_info_ino(path, st) ⇒ Object
186 |
# File 'lib/tb/cmd_ls.rb', line 186 def ls_info_ino(path, st) st.ino end |
#ls_info_mode(path, st) ⇒ Object
187 |
# File 'lib/tb/cmd_ls.rb', line 187 def ls_info_mode(path, st) sprintf("0%o", st.mode) end |
#ls_info_mtime(path, st) ⇒ Object
260 261 262 263 264 265 266 |
# File 'lib/tb/cmd_ls.rb', line 260 def ls_info_mtime(path, st) if 1 < @opts[:l] st.mtime.iso8601(9) else st.mtime.iso8601 end end |
#ls_info_nlink(path, st) ⇒ Object
188 |
# File 'lib/tb/cmd_ls.rb', line 188 def ls_info_nlink(path, st) st.nlink end |
#ls_info_rdev(path, st) ⇒ Object
191 |
# File 'lib/tb/cmd_ls.rb', line 191 def ls_info_rdev(path, st) sprintf("0x%x", st.rdev) end |
#ls_info_size(path, st) ⇒ Object
192 |
# File 'lib/tb/cmd_ls.rb', line 192 def ls_info_size(path, st) st.size end |
#ls_info_symlink(path, st) ⇒ Object
280 281 282 283 284 285 286 287 288 289 |
# File 'lib/tb/cmd_ls.rb', line 280 def ls_info_symlink(path, st) return nil if !st.symlink? begin File.readlink(path) rescue SystemCallError @fail = true warn "tb: #{$!}: #{path}" return nil end end |
#ls_info_uid(path, st) ⇒ Object
189 |
# File 'lib/tb/cmd_ls.rb', line 189 def ls_info_uid(path, st) st.uid end |
#ls_info_user(path, st) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/tb/cmd_ls.rb', line 226 def ls_info_user(path, st) uid = st.uid begin pw = Etc.getpwuid(uid) rescue ArgumentError end if pw pw.name else uid.to_s end end |
#ls_long_header ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/tb/cmd_ls.rb', line 171 def ls_long_header if 1 < @opts[:l] %w[dev ino mode filemode nlink uid user gid group rdev size blksize blocks atime mtime ctime filename symlink] else %w[filemode nlink user group size mtime filename symlink] end end |
#ls_long_info(path, st) ⇒ Object
179 180 181 182 183 |
# File 'lib/tb/cmd_ls.rb', line 179 def ls_long_info(path, st) Hash[ls_long_header.map {|info_type| [info_type, self.send("ls_info_#{info_type}", path, st)] }] end |
#ls_run(path) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/tb/cmd_ls.rb', line 88 def ls_run(path) st = ls_get_stat(path) return if !st if st.directory? ls_dir(path, st) else ls_file(path, st) end end |
#real_pathname_string(str) ⇒ Object
291 292 293 |
# File 'lib/tb/cmd_ls.rb', line 291 def real_pathname_string(str) str.dup.force_encoding("ASCII-8BIT") end |
#set_header ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/tb/cmd_ls.rb', line 80 def set_header if @opts[:l] == 0 @y.set_header ['filename'] else @y.set_header(ls_long_header()) end end |