utils 0.6.4 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27961e1f6195cb69054f6b658820b1544d33ab46
4
- data.tar.gz: e0a540a6f1cb21602322d8741a349212c9330882
3
+ metadata.gz: fd2e201421219d45b45d2fe83a850971c3b1f311
4
+ data.tar.gz: 5ba5419654b33453698c6c249749b132081b4555
5
5
  SHA512:
6
- metadata.gz: f13ebaa7a97cc7a8067f63149a3ecccfedf237686cdd7864cddee1e8d52fba0e411a0329fb3eb8a764c593cfb49aed8943167c74bb0ed546802511b2b36acda5
7
- data.tar.gz: 81efb168bb27cd2498147c6b48f18dad0714a575dd4d516d021acd57817a2b8072d1a297890cf172bf93efb807a4879542b8166dd28c5c12e7b9528d385ef1db
6
+ metadata.gz: 6553dda498be7fd555abacb3cd3ef8df38d7d0e408af0fddc76f856cd21ff93c73972fd1719c1cc68e1ddff346d774162b750c736faf4e4a10d6b7f5f3dfe4df
7
+ data.tar.gz: aced2f21c40473b7d006abe96be590a4095a86b32a1c62e2cc9b6eb0335a8630e09c545e13ace4941baec8bc5f86114f26f90b5244051826ea307c5a0bf3dd05
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  .*.sw[pon]
2
2
  .AppleDouble
3
+ .DS_Store
3
4
  .bundle
4
5
  .rvmrc
5
6
  Gemfile.lock
data/Rakefile CHANGED
@@ -12,7 +12,8 @@ GemHadar do
12
12
  bindir 'bin'
13
13
  executables Dir['bin/*'].map(&File.method(:basename))
14
14
  test_dir 'tests'
15
- ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', '.rvmrc', '.AppleDouble', 'tags', '.bundle'
15
+ ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', '.rvmrc', '.AppleDouble',
16
+ 'tags', '.bundle', '.DS_Store'
16
17
  readme 'README.md'
17
18
 
18
19
  dependency 'tins', '~>1.8'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.4
1
+ 0.8.0
data/bin/blameline CHANGED
@@ -22,8 +22,8 @@ end
22
22
 
23
23
  for line in lines
24
24
  blamer = Utils::LineBlamer.for_line(line) or next
25
- blame = blamer.perform
26
- blame.sub!(/^[0-9a-f]+/) { Term::ANSIColor.yellow($&) }
25
+ blame = blamer.perform or next
26
+ blame.sub!(/^[0-9a-f^]+/) { Term::ANSIColor.yellow($&) }
27
27
  blame.sub!(/\(([^)]+)\)/) { "(#{Term::ANSIColor.red($1)})" }
28
28
  puts blame
29
29
  end
data/bin/classify CHANGED
@@ -1,14 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'utils'
3
4
  require 'tins/go'
4
5
  include Tins::GO
5
6
  require 'tins/xt/string'
7
+ require 'term/ansicolor'
6
8
 
7
- $opts = go 'bdtsn:'
8
-
9
- string = ARGV.shift or fail "need a class/filepath/filename"
10
-
11
- def path_shifter(string, separator: ?/, n: 0)
9
+ def path_shifter(string, separator: ?/, n: nil)
12
10
  n or return string
13
11
  n, path = n.to_i, string.split(separator)
14
12
  if n < 0
@@ -21,27 +19,65 @@ end
21
19
 
22
20
  def underscore(string)
23
21
  string = path_shifter(string, n: $opts['n'], separator: '::')
24
- string = string.underscore
22
+ string = Tins::StringUnderscore.instance_method(:underscore).bind(string).()
25
23
  $opts['s'] and string << '.rb'
26
- print string
24
+ string
25
+ end
26
+
27
+ def parameterize(string, separator)
28
+ underscore(string).gsub(?/, separator) # quick and dirty
27
29
  end
28
30
 
29
31
  def camelize(string)
30
32
  string = path_shifter(string, n: $opts['n'])
31
33
  string = string.gsub(/#{Regexp.quote(File.extname(string))}\Z/, '')
32
- print string.camelize
34
+ string.camelize
35
+ end
36
+
37
+ def camelcase?(string)
38
+ string[0, 1] =~ /[A-Z]/
39
+ end
40
+
41
+ def compute_shift(config, string)
42
+ string = underscore(string)
43
+ result = config.classify.shift_path_by_default
44
+ for prefix in config.classify.shift_path_for_prefix
45
+ if string.start_with? prefix
46
+ return prefix.count(?/) + 1
47
+ end
48
+ end
49
+ result
33
50
  end
34
51
 
52
+ $opts = go 'bdtsp:n:'
53
+
54
+
55
+ string = ARGV.shift or fail "need a class/filepath/filename"
56
+ string = Term::ANSIColor.uncolor string
57
+
58
+ config = Utils::ConfigFile.new
59
+ config.configure_from_paths
60
+
61
+ $opts['n'] ||= compute_shift(config, string)
62
+
35
63
  $opts['b'] and $opts['n'] = '-1'
36
- case
64
+ print case
37
65
  when $opts['t']
38
- if string[0, 1] =~ /[A-Z]/
39
- underscore string
66
+ if camelcase?(string)
67
+ if separator = $opts['p']
68
+ parameterize string, separator
69
+ else
70
+ underscore string
71
+ end
40
72
  else
41
73
  camelize string
42
74
  end
43
75
  when $opts['d']
44
- underscore string
76
+ if separator = $opts['p']
77
+ parameterize string, separator
78
+ else
79
+ underscore string
80
+ end
45
81
  else
46
82
  camelize string
47
83
  end
data/bin/dialog-pick ADDED
@@ -0,0 +1,11 @@
1
+ #!/bin/sh
2
+
3
+ text=${1:-Pick}
4
+ tags=`cat`
5
+ lines=$(tput lines)
6
+ columns=$(tput cols)
7
+
8
+ result=$(dialog --no-items --menu "$text" $((lines - 5)) $((columns - 5)) $((lines - 10)) $tags 2>&1 1>/dev/tty)
9
+ exitcode=$?
10
+ echo $result
11
+ exit $exitcode
data/bin/discover CHANGED
@@ -32,10 +32,7 @@ directory and file paths that are searched.
32
32
  Options are
33
33
 
34
34
  -r interpret PATTERN argument as regex not fuzzy
35
- -d discover directories as well
36
- -D discover only directories
37
35
  -c disable color output
38
- -i use case insensitive matches
39
36
  -e open the matching files with edit command
40
37
  -E pick one file to edit
41
38
  -a CSET use only character set CSET from PATTERN
@@ -49,14 +46,14 @@ Version is #{File.basename($0)} #{Utils::VERSION}.
49
46
  exit 1
50
47
  end
51
48
 
52
- args = go 'I:a:rdDcieEbvh'
49
+ args = go 'I:a:rceEbvh'
53
50
  args['h'] and usage
54
51
  pattern = ARGV.shift or usage
55
52
  roots = (ARGV.empty? ? [ Dir.pwd ] : ARGV).map { |f| File.expand_path(f) }
56
53
 
57
54
  Term::ANSIColor.coloring = (STDIN.tty? && ENV['TERM'] !~ /dumb/) && !args['c']
58
55
  STDOUT.sync = true
59
- config = Utils::Config::ConfigFile.new
56
+ config = Utils::ConfigFile.new
60
57
  config.configure_from_paths
61
58
  args['b'] ||= config.discover.binary
62
59
  finder = Finder.new(
data/bin/edit CHANGED
@@ -32,7 +32,7 @@ end
32
32
  $opt = go 'p:S:c:g:wsmh'
33
33
  $opt['h'] and usage
34
34
 
35
- config = Utils::Config::ConfigFile.new
35
+ config = Utils::ConfigFile.new
36
36
  config.configure_from_paths
37
37
 
38
38
  editor = Editor.new do |c|
data/bin/probe CHANGED
@@ -92,7 +92,7 @@ def connect_server
92
92
  end
93
93
  end
94
94
 
95
- $config = Utils::Config::ConfigFile.new
95
+ $config = Utils::ConfigFile.new
96
96
  $config.configure_from_paths
97
97
  testrunner_args = []
98
98
  if i = ARGV.index('--')
data/bin/rssr ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rss'
4
+ require 'tins'
5
+ require 'open-uri'
6
+ require 'term/ansicolor'
7
+
8
+
9
+ class String
10
+ include Term::ANSIColor
11
+
12
+ def wrap(cols: Tins::Terminal.cols)
13
+ split(?\n).each do |line|
14
+ line.size > cols and line.gsub!(/(.{1,#{cols}})(\s+|$)/, "\\1\n")
15
+ line.strip!
16
+ end * ?\n
17
+ end
18
+ end
19
+
20
+ IO.popen(ENV.fetch('PAGER', `which less`.chomp << ' -r'), 'w') do |pager|
21
+ url = ARGV.shift
22
+ open(url) do |rss|
23
+ feed = RSS::Parser.parse(rss)
24
+ pager.puts "Feed: #{feed.channel.title}",
25
+ ?– * Tins::Terminal.cols
26
+ feed.items.sort_by { |item| -item.date.to_f }.each do |item|
27
+ pager.puts(
28
+ "Title: #{item.title.bright_blue}",
29
+ "Link: #{item.link.red}",
30
+ "Date: #{item.date.strftime('%FT%T%z')}",
31
+ "",
32
+ item.description.wrap.chomp.white,
33
+ ?– * Tins::Terminal.cols
34
+ )
35
+ end
36
+ end
37
+ end
data/bin/search CHANGED
@@ -125,6 +125,7 @@ Options are
125
125
  -e open the matching files with edit command
126
126
  -E pick one file to edit
127
127
  -r REPLACE replace the searched match with REPLACE
128
+ -b
128
129
  -a CSET use only character set CSET from PATTERN
129
130
  -v be verbose
130
131
  -h display this help
@@ -134,14 +135,14 @@ Version is #{File.basename($0)} #{Utils::VERSION}.
134
135
  exit 1
135
136
  end
136
137
 
137
- args = go 'r:I:A:B:C:s:S:n:N:a:RciflLeEvh'
138
+ args = go 'r:I:A:B:C:s:S:n:N:a:RciflLeEvbh'
138
139
  args['h'] and usage
139
140
  pattern = ARGV.shift or usage
140
141
  roots = (ARGV.empty? ? [ Dir.pwd ] : ARGV).map { |f| File.expand_path(f) }
141
142
 
142
143
  Term::ANSIColor.coloring = (STDIN.tty? && ENV['TERM'] !~ /dumb/) && !args['c']
143
144
  STDOUT.sync = true
144
- config = Utils::Config::ConfigFile.new
145
+ config = Utils::ConfigFile.new
145
146
  config.configure_from_paths
146
147
  grepper = Grepper.new(
147
148
  :pattern => pattern,
data/bin/serve ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'tins/go'
4
+ include Tins::GO
5
+ require 'webrick'
6
+
7
+ $opts = go 'p:h'
8
+
9
+ if $opts[?h]
10
+ puts <<USAGE
11
+ #{File.basename($0)} [OPTIONS] [DIR]
12
+ USAGE
13
+ exit
14
+ end
15
+
16
+ port = ($opts[?p] || 8888).to_i
17
+ s = WEBrick::HTTPServer.new(
18
+ Port: port,
19
+ DocumentRoot: ARGV.shift || Dir.pwd
20
+ )
21
+ trap('INT') { s.shutdown }
22
+ puts "You have been served: http://localhost:#{port}/"
23
+ s.start
24
+
data/bin/ssh-tunnel CHANGED
@@ -44,7 +44,7 @@ def cmd(string)
44
44
  exec string
45
45
  end
46
46
 
47
- config = Utils::Config::ConfigFile.new
47
+ config = Utils::ConfigFile.new
48
48
 
49
49
  arguments = ARGV
50
50
  opts = go 't:n:C:m:e:hNTd', arguments
data/bin/strip_spaces CHANGED
@@ -38,7 +38,7 @@ end
38
38
 
39
39
  suffix = args['I'].ask_and_send(:split, /[\s,]+/).to_a
40
40
 
41
- config = Utils::Config::ConfigFile.new
41
+ config = Utils::ConfigFile.new
42
42
  config.configure_from_paths
43
43
 
44
44
  if paths
data/bin/utils-utilsrc CHANGED
@@ -4,7 +4,7 @@ require 'tempfile'
4
4
  require 'utils'
5
5
  include Utils
6
6
 
7
- $config = Utils::Config::ConfigFile.new
7
+ $config = Utils::ConfigFile.new
8
8
  $utilsrc = File.expand_path('~/.utilsrc')
9
9
 
10
10
  def create_default_utilsrc
data/lib/utils.rb CHANGED
@@ -3,8 +3,7 @@ module Utils
3
3
  require 'utils/file_xt'
4
4
  require 'utils/md5'
5
5
  require 'utils/patterns'
6
- require 'utils/config'
7
- require 'utils/config/config_file'
6
+ require 'utils/config_file'
8
7
  require 'utils/editor'
9
8
  require 'utils/finder'
10
9
  require 'utils/grepper'
@@ -1,7 +1,7 @@
1
1
  require 'tins'
2
2
  require 'tins/xt/string'
3
3
 
4
- class Utils::Config::ConfigFile
4
+ class Utils::ConfigFile
5
5
  class << self
6
6
  attr_accessor :config_file_paths
7
7
  end
@@ -64,13 +64,15 @@ class Utils::Config::ConfigFile
64
64
 
65
65
  def to_ruby(depth = 0)
66
66
  result = ''
67
- result << ' ' * 2 * depth << "#{self.class.name[/::([^:]+)\z/, 1].underscore} do\n"
67
+ result << ' ' * 2 * depth <<
68
+ "#{self.class.name[/::([^:]+)\z/, 1].underscore} do\n"
68
69
  for name in self.class.config_settings
69
70
  value = __send__(name)
70
71
  if value.respond_to?(:to_ruby)
71
72
  result << ' ' * 2 * (depth + 1) << value.to_ruby(depth + 1)
72
73
  else
73
- result << ' ' * 2 * (depth + 1) << "#{name} #{Array(value).map(&:inspect) * ', '}\n"
74
+ result << ' ' * 2 * (depth + 1) <<
75
+ "#{name} #{Array(value).map(&:inspect) * ', '}\n"
74
76
  end
75
77
  end
76
78
  result << ' ' * 2 * depth << "end\n"
@@ -244,9 +246,22 @@ class Utils::Config::ConfigFile
244
246
  @edit ||= Edit.new
245
247
  end
246
248
 
249
+ class Classify < BlockConfig
250
+ config :shift_path_by_default, 0
251
+
252
+ config :shift_path_for_prefix, []
253
+ end
254
+
255
+ def classify(&block)
256
+ if block
257
+ @classify = Classify.new(&block)
258
+ end
259
+ @classify ||= Classify.new
260
+ end
261
+
247
262
  def to_ruby
248
263
  result = "# vim: set ft=ruby:\n"
249
- for bc in %w[search discover strip_spaces probe ssh_tunnel]
264
+ for bc in %w[search discover strip_spaces probe ssh_tunnel edit classify]
250
265
  result << "\n" << __send__(bc).to_ruby
251
266
  end
252
267
  result
data/lib/utils/editor.rb CHANGED
@@ -9,7 +9,7 @@ module Utils
9
9
  self.wait = false
10
10
  self.pause_duration = 1
11
11
  self.servername = derive_server_name
12
- config = Utils::Config::ConfigFile.new
12
+ config = Utils::ConfigFile.new
13
13
  config.configure_from_paths
14
14
  self.config = config.edit
15
15
  yield self if block_given?
data/lib/utils/finder.rb CHANGED
@@ -11,19 +11,17 @@ class Utils::Finder
11
11
  include Term::ANSIColor
12
12
 
13
13
  def initialize(opts = {})
14
- @args = opts[:args] || {}
15
- @roots = opts[:roots] || []
16
- @config = opts[:config] || Utils::Config::ConfigFile.new
14
+ @args = opts[:args] || {}
15
+ @roots = discover_roots(opts[:roots])
16
+ @config = opts[:config] || Utils::ConfigFile.new
17
17
  pattern_opts = opts.subhash(:pattern) | {
18
18
  :cset => @args['a'],
19
- :icase => @args['i'],
19
+ :icase => @args.fetch('i', true),
20
20
  }
21
21
  @binary = @args['b']
22
22
  @pattern = @args['r'] ?
23
23
  RegexpPattern.new(pattern_opts) :
24
24
  FuzzyPattern.new(pattern_opts)
25
- @directory = @args['d']
26
- @only_directory = @args['D']
27
25
  @paths = []
28
26
  end
29
27
 
@@ -38,13 +36,7 @@ class Utils::Finder
38
36
  def attempt_match?(path)
39
37
  stat = path.stat
40
38
  stat.symlink? and stat = path.lstat
41
- if @only_directory
42
- stat.directory?
43
- elsif @directory
44
- stat.directory? || ascii_file?(stat, path)
45
- else
46
- ascii_file?(stat, path)
47
- end
39
+ stat.directory? || ascii_file?(stat, path)
48
40
  rescue SystemCallError => e
49
41
  warn "Caught #{e.class}: #{e}"
50
42
  nil
@@ -73,7 +65,7 @@ class Utils::Finder
73
65
  if do_match = attempt_match?(path) and @args['v']
74
66
  warn "Attempt match of #{path.inspect}"
75
67
  end
76
- if do_match and match = @pattern.match(file)
68
+ if do_match and match = @pattern.match(path)
77
69
  if FuzzyPattern === @pattern
78
70
  current = 0
79
71
  marked_file = ''
@@ -81,8 +73,8 @@ class Utils::Finder
81
73
  for i in 1...(match.size)
82
74
  match[i] or next
83
75
  b = match.begin(i)
84
- marked_file << file[current...b]
85
- marked_file << red(file[b, 1])
76
+ marked_file << path[current...b]
77
+ marked_file << red(path[b, 1])
86
78
  score += (b - e)
87
79
  e = match.end(i)
88
80
  current = b + 1
@@ -90,9 +82,9 @@ class Utils::Finder
90
82
  marked_file << match.post_match
91
83
  [ score, file.size, path, File.join(dir, marked_file) ]
92
84
  else
93
- marked_file = file[0...match.begin(0)] <<
94
- red(file[match.begin(0)...match.end(0)]) <<
95
- file[match.end(0)..-1]
85
+ marked_file = path[0...match.begin(0)] <<
86
+ red(path[match.begin(0)...match.end(0)]) <<
87
+ path[match.end(0)..-1]
96
88
  [ 0, file.size, path, File.join(dir, marked_file) ]
97
89
  end
98
90
  end
@@ -101,4 +93,11 @@ class Utils::Finder
101
93
  @paths, @output = paths.sort.transpose.values_at(-2, -1)
102
94
  self
103
95
  end
96
+
97
+ private
98
+
99
+ def discover_roots(roots)
100
+ roots ||= []
101
+ roots.inject([]) { |rs, r| rs.concat Dir[r] }
102
+ end
104
103
  end