summaryrefslogtreecommitdiff
path: root/lib/pry/commands/pipe_cuties.rb
blob: deefc5e6f49ddb2ce349f062b769d7eefae394e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
Pry::Commands.block_command "grep", "grep stuff" do
  if in_pipe?
    obj = pipe.read

    if obj.respond_to?(:grep)
      grepped = obj.grep Regexp.new(arg_string)
    elsif obj.is_a?(String)
      grepped = obj.lines.to_a.grep Regexp.new(arg_string)
    else
      raise Pry::CommandError, "Can't grep passed object!"
    end

    if out_pipe?
      pipe.write grepped.join
    else
      output.puts grepped.join
    end

  else
    raise Pry::CommandError, "grep can only be used with piped input!"
  end
end

Pry::Commands.block_command "sort", "sort stuff" do
  if in_pipe?
    obj = pipe.read

    if obj.respond_to?(:sort_by)
      sorted = obj.sort_by { |v| text.strip_color(v.strip) }.join
    elsif obj.is_a?(String)
      sorted = obj.lines.to_a.sort_by { |v| text.strip_color(v.strip) }.join
    else
      raise Pry::CommandError, "Can't sort passed object!"
    end

    if out_pipe?
      pipe.write sorted
    else
      output.puts sorted
    end

  else
    raise Pry::CommandError, "sort can only be used with piped input!"
  end
end

Pry::Commands.block_command "less", "page stuff" do
  if in_pipe?
    obj = pipe.read

    if out_pipe?
      pipe.write obj
    else
      stagger_output obj.to_s
    end

  else
    raise Pry::CommandError, "can only be used with piped input!"
  end
end

Pry::Commands.block_command "wc", "page stuff" do
  if in_pipe?
    obj = pipe.read

    if obj.is_a?(String)
      count = obj.lines.count
    elsif obj.respond_to?(:count)
      count = obj.count
    else
      raise Pry::CommandError, "Can't count the object!"
    end

    if out_pipe?
      pipe.write count.to_s
    else
      output.puts "Number of lines: #{count.to_s}"
    end

  else
    raise Pry::CommandError, "can only be used with piped input!"
  end
end


Pry::Commands.block_command "less2", "page stuff" do
  if in_pipe?
    obj = pipe.read

    binding.pry

    if out_pipe?
      pipe.write obj
    else
      stagger_output obj.to_s
    end

  else
    raise Pry::CommandError, "sort can only be used with piped input!"
  end
end