dlister 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dlister.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Anthony Cook
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ Dlister
2
+ =======
3
+
4
+ Dlister is basically an 'ls' clone with some enhancements.
5
+
6
+ *Anthony M. Cook 2013 - http://github.com/acook | @anthony_m_cook | http://anthonymcook.com*
7
+
8
+ Features
9
+ --------
10
+
11
+ - Unicode sigils
12
+ - Improved multi-attribute sorting
13
+ - Color
14
+ - 256-color support (planned)
15
+ - SCM (git) integration (planned)
16
+
17
+ Installation
18
+ ------------
19
+
20
+ To use it as an `ls` replacement, just install the gem:
21
+
22
+ $ gem install dlister
23
+
24
+ And then make a symlink to it (I don't suggest overwriting ls):
25
+
26
+ $ alias l='dlister'
27
+
28
+ Usage
29
+ -----
30
+
31
+ Pretty basic just do `dlister path`. Examples:
32
+
33
+ $ dlister # list current directory
34
+ $ dlister ~ # list home directory
35
+ $ dlister /bin ../ .bash_profile # list absolute path, relative path, and file
36
+
37
+ Contributing
38
+ ------------
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'dlister'
5
+ rescue LoadError => error
6
+ def delink path
7
+ if File.symlink? path then
8
+ delink File.readlink(path)
9
+ elsif File.directory? path then
10
+ path
11
+ elsif File.file? path then
12
+ File.dirname path
13
+ elsif !(File.exists? path) then
14
+ raise 'Path not found!'
15
+ else
16
+ raise 'Sanity check.. what did you DO?!'
17
+ end
18
+ end
19
+
20
+ def find_real_path some_path = __FILE__
21
+ path = File.expand_path some_path
22
+
23
+ delink path
24
+ end
25
+
26
+ $LOAD_PATH << File.join(find_real_path, '..', 'lib')
27
+ require 'dlister'
28
+ end
29
+
30
+ paths = ARGV.empty? ? [Dir.pwd] : ARGV
31
+
32
+ Dlister.list paths
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dlister/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'dlister'
8
+ gem.version = Dlister::VERSION
9
+ gem.authors = ['Anthony Cook']
10
+ gem.email = ['anthonymichaelcook@gmail.com']
11
+ gem.description = %q{Dlister is sort of an 'ls' clone for Ruby. It has some enhanced features and minor differences. It's killer feature though will be SCM integration (in progress).}
12
+ gem.summary = %q{Enhanced 'ls' clone.}
13
+ gem.homepage = 'http://github.com/acook/dlister#readme'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+ end
@@ -0,0 +1,35 @@
1
+ require 'dlister/version'
2
+ require 'dlister/dir_list'
3
+ require 'dlister/color'
4
+
5
+ module Dlister
6
+ module_function
7
+ extend Dlister::Color
8
+
9
+ def list paths
10
+ @paths = paths
11
+
12
+ @paths.sort.each_with_index do |path, index|
13
+ print normal
14
+ puts "#{path}:" if many?
15
+
16
+ puts DirList.new path
17
+
18
+ puts if many? && not_last?(index)
19
+ end
20
+ end
21
+
22
+ def count
23
+ @paths.length
24
+ end
25
+
26
+ def not_last? index
27
+ index + 1 < count
28
+ end
29
+
30
+ def many?
31
+ count > 1
32
+ end
33
+
34
+ end
35
+
@@ -0,0 +1,58 @@
1
+ module Dlister
2
+ module Color
3
+ def bright name
4
+ fg name, :bright
5
+ end
6
+
7
+ def normal name = nil
8
+ if name then
9
+ fg name, :normal
10
+ else
11
+ esc 0
12
+ end
13
+ end
14
+
15
+ def fg name, style_name = :normal
16
+ esc style(style_name), "3#{color(name)}"
17
+ end
18
+
19
+ def bg name, style_name = :normal
20
+ esc style(style_name), "4#{color(name)}"
21
+ end
22
+
23
+ def style name
24
+ [
25
+ :normal,
26
+ :bright,
27
+ :underlined
28
+ ].index name
29
+ end
30
+
31
+ def color name
32
+ [
33
+ :black,
34
+ :red,
35
+ :green,
36
+ :yellow,
37
+ :blue,
38
+ :magenta,
39
+ :cyan,
40
+ :white
41
+ ].index name
42
+ end
43
+
44
+ def esc *code
45
+ "\e[#{code.compact.join(';')}m"
46
+ end
47
+
48
+ def console_width
49
+ tiocgwinsz = 0x40087468
50
+ str = [0, 0, 0, 0].pack('SSSS')
51
+ if STDOUT.ioctl(tiocgwinsz, str) >= 0 then
52
+ str.unpack('SSSS')[1]
53
+ else
54
+ 1.0 / 0
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,130 @@
1
+ require 'pathname'
2
+ require 'dlister/file_info'
3
+ #require 'dlister/parse_git'
4
+ require 'dlister/color'
5
+
6
+ module Dlister
7
+ class DirList
8
+ include Dlister::Color
9
+
10
+ def initialize path
11
+ @path = Pathname.new(path).expand_path
12
+ end
13
+ attr :path
14
+
15
+ def info
16
+ FileInfo.for_path path
17
+ end
18
+
19
+ def git
20
+ ParseGit.for_path path
21
+ end
22
+
23
+ def to_s
24
+ entries = remap_entries entry_sort(info)
25
+ clean_entries = entries.map{|entry| clean entry }
26
+
27
+ if clean_entries.join(spacer).length < width then
28
+ entries.join spacer
29
+ else
30
+ columnize entries, clean_entries
31
+ end
32
+ end
33
+
34
+ def columnize entries, clean_entries
35
+ longest_entry = clean_entries.max{|a,b| a.length <=> b.length }
36
+ column_width = longest_entry.length + spacer.length
37
+ columns = width / column_width
38
+
39
+ lines = Array.new
40
+ entries.each_slice(columns) do |line|
41
+ lines << line.map do |entry|
42
+ entry + (' ' * (column_width - clean(entry).length))
43
+ end.join
44
+ end
45
+ lines.join newline
46
+ end
47
+
48
+ def entry_sort entries
49
+ order = [:directory, :executable, :file, :link, :invalid_link]
50
+
51
+ entries.sort do |(entry_a, attr_a), (entry_b, attr_b)|
52
+ rtype_a, rtype_b = attr_a[:real_type], attr_b[:real_type]
53
+
54
+ rtype_sort = order.index(rtype_a) <=> order.index(rtype_b)
55
+
56
+ if rtype_sort == 0 then
57
+ if entry_a =~ /^\d/ && entry_b =~ /^\d/ then
58
+ entry_a.to_i <=> entry_b.to_i
59
+ else
60
+ entry_a <=> entry_b
61
+ end
62
+ else
63
+ rtype_sort
64
+ end
65
+ end
66
+ end
67
+
68
+ def remap_entries entries
69
+ entries.map do |entry, attributes|
70
+ entry_to_s entry, attributes
71
+ end
72
+ end
73
+
74
+ def entry_to_s entry, attributes
75
+ type = attributes[:type]
76
+ real_type = attributes[:real_type]
77
+
78
+ text = String.new
79
+
80
+ text << colormap(type)
81
+ text << entry
82
+
83
+ text << normal
84
+ text << glyphmap(type)
85
+
86
+ if type != real_type then
87
+ text << glyphmap(real_type)
88
+ else
89
+ text
90
+ end
91
+ end
92
+
93
+ def glyphmap type
94
+ {
95
+ directory: '/',
96
+ link: "\u2A1D",
97
+ file: '',
98
+ executable: "\u25B7",
99
+ invalid_link: "\u02E3"
100
+ }[type] || ''
101
+ end
102
+
103
+ def colormap type
104
+ {
105
+ directory: bright(:blue),
106
+ link: fg(:yellow),
107
+ file: fg(:normal),
108
+ executable: bright(:green),
109
+ invalid_link: bg(:red)
110
+ }[type] || fg(:normal)
111
+ end
112
+
113
+ def clean text
114
+ ansi_color_codes = /\e\[\d+(?>(;\d+)*)m/
115
+ text.gsub ansi_color_codes, ''
116
+ end
117
+
118
+ def width
119
+ @width ||= console_width
120
+ end
121
+
122
+ def spacer
123
+ ' '
124
+ end
125
+
126
+ def newline
127
+ "\n"
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,61 @@
1
+ class FileInfo
2
+
3
+ def self.for_path path
4
+ if path.directory? then
5
+ contents = Hash.new
6
+ path.each_child(true) do |entry|
7
+ info = self.new entry
8
+ contents[info.name] = info.attributes
9
+ end
10
+ contents
11
+ else
12
+ info = self.new path
13
+ {info.name => info.attributes}
14
+ end
15
+ end
16
+
17
+ def initialize path
18
+ @path = path
19
+ end
20
+ attr :path
21
+
22
+ def type
23
+ if !path.exist? then
24
+ if path.symlink? then
25
+ :invalid_link
26
+ else
27
+ :not_found
28
+ end
29
+ elsif path.symlink? then
30
+ :link
31
+ elsif path.executable? then
32
+ if path.directory? then
33
+ :directory
34
+ else
35
+ :executable
36
+ end
37
+ else
38
+ path.ftype.to_sym
39
+ end
40
+ end
41
+
42
+ def real_type
43
+ if type == :link then
44
+ self.class.new(path.readlink).real_type
45
+ else
46
+ type
47
+ end
48
+ end
49
+
50
+ def basename
51
+ path.basename
52
+ end
53
+
54
+ def attributes
55
+ {type: type, real_type: real_type}
56
+ end
57
+
58
+ def name
59
+ basename.to_s
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module Dlister
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dlister
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anthony Cook
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Dlister is sort of an 'ls' clone for Ruby. It has some enhanced features
15
+ and minor differences. It's killer feature though will be SCM integration (in progress).
16
+ email:
17
+ - anthonymichaelcook@gmail.com
18
+ executables:
19
+ - dlister
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - .gitignore
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.markdown
27
+ - Rakefile
28
+ - bin/dlister
29
+ - dlister.gemspec
30
+ - lib/dlister.rb
31
+ - lib/dlister/color.rb
32
+ - lib/dlister/dir_list.rb
33
+ - lib/dlister/file_info.rb
34
+ - lib/dlister/version.rb
35
+ homepage: http://github.com/acook/dlister#readme
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ none: false
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ none: false
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.24
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Enhanced 'ls' clone.
59
+ test_files: []
60
+ has_rdoc: