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.
82 83 84 85 86 |
# File 'lib/tb/cmd_ls.rb', line 82 def initialize(y, opts) @y = y @opts = opts @fail = false end |
Instance Attribute Details
#fail ⇒ Object (readonly)
Returns the value of attribute fail.
87 88 89 |
# File 'lib/tb/cmd_ls.rb', line 87 def fail @fail end |
Instance Method Details
#ls_dir(dir, st) ⇒ Object
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 147 148 149 150 151 152 153 154 155 |
# File 'lib/tb/cmd_ls.rb', line 107 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
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/tb/cmd_ls.rb', line 157 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
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tb/cmd_ls.rb', line 169 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
261 262 263 264 265 266 267 |
# File 'lib/tb/cmd_ls.rb', line 261 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
202 |
# File 'lib/tb/cmd_ls.rb', line 202 def ls_info_blksize(path, st) st.blksize end |
#ls_info_blocks(path, st) ⇒ Object
203 |
# File 'lib/tb/cmd_ls.rb', line 203 def ls_info_blocks(path, st) st.blocks end |
#ls_info_ctime(path, st) ⇒ Object
277 278 279 280 281 282 283 |
# File 'lib/tb/cmd_ls.rb', line 277 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
194 |
# File 'lib/tb/cmd_ls.rb', line 194 def ls_info_dev(path, st) sprintf("0x%x", st.dev) end |
#ls_info_filemode(path, st) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/tb/cmd_ls.rb', line 205 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
285 286 287 |
# File 'lib/tb/cmd_ls.rb', line 285 def ls_info_filename(path, st) path end |
#ls_info_gid(path, st) ⇒ Object
199 |
# File 'lib/tb/cmd_ls.rb', line 199 def ls_info_gid(path, st) st.gid end |
#ls_info_group(path, st) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/tb/cmd_ls.rb', line 248 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
195 |
# File 'lib/tb/cmd_ls.rb', line 195 def ls_info_ino(path, st) st.ino end |
#ls_info_mode(path, st) ⇒ Object
196 |
# File 'lib/tb/cmd_ls.rb', line 196 def ls_info_mode(path, st) sprintf("0%o", st.mode) end |
#ls_info_mtime(path, st) ⇒ Object
269 270 271 272 273 274 275 |
# File 'lib/tb/cmd_ls.rb', line 269 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
197 |
# File 'lib/tb/cmd_ls.rb', line 197 def ls_info_nlink(path, st) st.nlink end |
#ls_info_rdev(path, st) ⇒ Object
200 |
# File 'lib/tb/cmd_ls.rb', line 200 def ls_info_rdev(path, st) sprintf("0x%x", st.rdev) end |
#ls_info_size(path, st) ⇒ Object
201 |
# File 'lib/tb/cmd_ls.rb', line 201 def ls_info_size(path, st) st.size end |
#ls_info_symlink(path, st) ⇒ Object
289 290 291 292 293 294 295 296 297 298 |
# File 'lib/tb/cmd_ls.rb', line 289 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
198 |
# File 'lib/tb/cmd_ls.rb', line 198 def ls_info_uid(path, st) st.uid end |
#ls_info_user(path, st) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/tb/cmd_ls.rb', line 235 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
180 181 182 183 184 185 186 |
# File 'lib/tb/cmd_ls.rb', line 180 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
188 189 190 191 192 |
# File 'lib/tb/cmd_ls.rb', line 188 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
97 98 99 100 101 102 103 104 105 |
# File 'lib/tb/cmd_ls.rb', line 97 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
300 301 302 |
# File 'lib/tb/cmd_ls.rb', line 300 def real_pathname_string(str) str.dup.force_encoding("ASCII-8BIT") end |
#set_header ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/tb/cmd_ls.rb', line 89 def set_header if @opts[:l] == 0 @y.set_header ['filename'] else @y.set_header(ls_long_header()) end end |