rb

Turns Ruby into a versatile command line utility

  • 所有者: thisredone/rb
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

rb

With 9 lines of Ruby replace most of the command line tools that you use to process text inside of the terminal.

Here's the code

#!/usr/bin/env ruby
File.join(Dir.home, '.rbrc').tap {, f, load f if File.exists?(f) }

def execute(_, code)
  puts _.instance_eval(&code)
rescue Errno::EPIPE
  exit
end

single_line = ARGV.delete('-l')
code = eval("Proc.new { #{ARGV.join(' ')} }")
single_line ? STDIN.each {, l, execute(l.chomp, code) } : execute(STDIN.each_line, code)

Clone this repo and copy the rb file to somewhere in your path (or just copy and paste the above).

With this you can use ruby as a command line utility much more ergonomically than invoking it the standard way.

There's only one switch -l which runs your code on each line separately. Otherwise you get an Enumerator that returns the lines of stdin. It's instance_evaled so some methods need self to work, eg. self[-1]

Install

Just paste this line into your terminal to install rb:

sudo curl https://raw.githubusercontent.com/thisredone/rb/master/rb -o /usr/local/bin/rb && sudo chmod +x /usr/local/bin/rb

Examples

Extract docker images from running containers
> docker ps, rb drop 1, rb -l split[1]

# ubuntu
# postgres
Display how much time ago containers have exited
> docker ps -a, rb grep /Exited/, rb -l 'split.last.ljust(20) + " => " + split(/ {2,}/)[-2]'

# angry_hamilton      => Exited (0) 18 hours ago
# dreamy_lamport      => Exited (0) 3 days ago
# prickly_hypatia     => Exited (0) 2 weeks ago
Sort df -h output by Use%
> df -h, rb 'drop(1).sort_by {, l, l.split[-2].to_f }'

# udev                         3,9G     0  3,9G   0% /dev
# tmpfs                        3,9G     0  3,9G   0% /sys/fs/cgroup
# /dev/sda1                    511M  3,4M  508M   1% /boot/efi
# /dev/sda2                    237M   85M  140M  38% /boot

# or leave the header if you want
> df -h, rb '[first].concat drop(1).sort_by {, l, l.split[-2].to_f }'

# Filesystem                   Size  Used Avail Use% Mounted on
# udev                         3,9G     0  3,9G   0% /dev
# tmpfs                        3,9G     0  3,9G   0% /sys/fs/cgroup
# /dev/sda1                    511M  3,4M  508M   1% /boot/efi
# /dev/sda2                    237M   85M  140M  38% /boot
Count files by their extension
> find . -type f, rb 'group_by(&File.method(:extname)).map {, ext, o, "#{ext.chomp}: #{o.size}" }'

# : 3
# .rb: 19
# .md: 1
This problem http://vegardstikbakke.com/unix/
ls, rb 'group_by {, x, x[/\d+/] }.select {, _, y, y.one? }.keys'

Extending rb

The ~/.rbrc file is loaded if it's available. Anything defined in there will be available inside rb scripts.

# ~/.rbrc

class String
  def black; "\033[30m#{self}\033[0m" end
  def red;   "\033[31m#{self}\033[0m" end
end
> ls, rb first.red

主要指标

概览
名称与所有者thisredone/rb
主编程语言Ruby
编程语言Ruby (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2018-08-14 02:31:27
推送于2023-02-16 14:51:16
最后一次提交2023-02-16 14:43:06
发布数0
用户参与
星数1.5k
关注者数30
派生数42
提交数32
已启用问题?
问题数7
打开的问题数1
拉请求数12
打开的拉请求数0
关闭的拉请求数1
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?