├── .gitignore ├── Gemfile ├── CHANGELOG.md ├── spec └── cd_spec.rb ├── README.md ├── lib └── cd.rb ├── cd.gemspec ├── Rakefile ├── MIT-LICENSE.txt └── .github └── workflows └── test.yml /.gitignore: -------------------------------------------------------------------------------- 1 | Gemfile.lock 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gemspec 4 | 5 | gem "minitest" 6 | gem "rake" 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## CHANGELOG 2 | 3 | ### v1.0.2 4 | 5 | - 2024 re-relase, with GitHub CI and gemspec updates 6 | 7 | ### v1.0.1 8 | 9 | - Relax Ruby version requirement 10 | 11 | ### v1.0.0 12 | 13 | - Initial release 14 | 15 | -------------------------------------------------------------------------------- /spec/cd_spec.rb: -------------------------------------------------------------------------------- 1 | require_relative "../lib/cd" 2 | require "minitest/autorun" 3 | 4 | require 'fileutils' 5 | 6 | describe Cd do 7 | include Cd 8 | 9 | it "changes the directory" do 10 | FileUtils.cd "/" 11 | old_directory = FileUtils.pwd 12 | cd "~" 13 | new_directory = FileUtils.pwd 14 | assert new_directory != old_directory 15 | end 16 | end 17 | 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cd [![[version]](https://badge.fury.io/rb/cd.svg)](https://badge.fury.io/rb/cd) 2 | 3 | Enhanced cd command for the Ruby console. 4 | 5 | 6 | ## Setup 7 | 8 | ``` 9 | gem install cd 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```ruby 16 | require 'cd' 17 | extend Cd 18 | 19 | cd 'some/dir' # change to that directory and list its content 20 | ~cd # change to home directory 21 | -cd # change to last directory 22 | ``` 23 | 24 | 25 | ## MIT License 26 | 27 | Copyright (C) 2015 Jan Lelis . Released under the MIT license. 28 | -------------------------------------------------------------------------------- /lib/cd.rb: -------------------------------------------------------------------------------- 1 | require 'fileutils' 2 | 3 | module Cd 4 | VERSION = '1.0.2'.freeze 5 | 6 | extend self 7 | 8 | def cd(path = nil) 9 | if !path 10 | Cd::Proxy 11 | else 12 | Cd::Proxy[path] 13 | end 14 | end 15 | 16 | module Proxy 17 | def self.~ 18 | cd '~' 19 | end 20 | 21 | def self.-@ 22 | if @last_path 23 | cd @last_path 24 | else 25 | warn "Sorry, there is no previous directory." 26 | end 27 | end 28 | 29 | def self.[](path) 30 | next_last_path = pwd 31 | FileUtils::Verbose.cd File.expand_path(path) 32 | @last_path = next_last_path 33 | Cd::Proxy.ls 34 | end 35 | 36 | def self.ls(path = '.') 37 | Dir["#{path}/*"].map{ |filename| File.basename filename } 38 | end 39 | 40 | def self.pwd 41 | FileUtils.pwd 42 | end 43 | 44 | def self.inspect 45 | "#{to_s}[#{pwd.inspect}]" 46 | end 47 | end 48 | end 49 | 50 | 51 | -------------------------------------------------------------------------------- /cd.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | 3 | require File.dirname(__FILE__) + "/lib/cd" 4 | 5 | Gem::Specification.new do |gem| 6 | gem.name = "cd" 7 | gem.version = Cd::VERSION 8 | gem.summary = "Enhanced cd command for the Ruby console" 9 | gem.description = "Enhanced cd command for the Ruby console. Supports changing to previous directory using `-cd` and to the user's home directory with `~cd`." 10 | gem.authors = ["Jan Lelis"] 11 | gem.email = ["hi@ruby.consulting"] 12 | gem.homepage = "https://github.com/janlelis/cd" 13 | gem.license = "MIT" 14 | 15 | gem.files = Dir['{**/}{.*,*}'].select{ |path| File.file?(path) && path !~ /^pkg/ } 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 | gem.metadata = { "rubygems_mfa_required" => "true" } 20 | 21 | gem.required_ruby_version = '>= 1.9.3' 22 | end 23 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # # # 2 | # Get gemspec info 3 | 4 | gemspec_file = Dir['*.gemspec'].first 5 | gemspec = eval File.read(gemspec_file), binding, gemspec_file 6 | info = "#{gemspec.name} | #{gemspec.version} | " \ 7 | "#{gemspec.runtime_dependencies.size} dependencies | " \ 8 | "#{gemspec.files.size} files" 9 | 10 | 11 | # # # 12 | # Gem build and install task 13 | 14 | desc info 15 | task :gem do 16 | puts info + "\n\n" 17 | print " "; sh "gem build #{gemspec_file}" 18 | FileUtils.mkdir_p 'pkg' 19 | FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg' 20 | puts; sh %{gem install --no-document pkg/#{gemspec.name}-#{gemspec.version}.gem} 21 | end 22 | 23 | 24 | # # # 25 | # Start an IRB session with the gem loaded 26 | 27 | desc "#{gemspec.name} | IRB" 28 | task :irb do 29 | sh "irb -I ./lib -r #{gemspec.name.gsub '-','/'}" 30 | end 31 | 32 | 33 | # # # 34 | # Run specs 35 | 36 | desc "#{gemspec.name} | Spec" 37 | task :spec do 38 | # sh "for file in spec/*_spec.rb; do ruby $file; done" 39 | ruby "spec/cd_spec.rb" 40 | end 41 | task default: :spec 42 | 43 | -------------------------------------------------------------------------------- /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Jan Lelis, https://janlelis.com 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | name: Ruby ${{ matrix.ruby }} (${{ matrix.os }}) 8 | if: "!contains(github.event.head_commit.message, '[skip ci]')" 9 | strategy: 10 | matrix: 11 | ruby: 12 | - '3.3' 13 | - '3.2' 14 | - '3.1' 15 | - '3.0' 16 | - jruby 17 | - truffleruby 18 | os: 19 | - ubuntu-latest 20 | - macos-latest 21 | runs-on: ${{matrix.os}} 22 | steps: 23 | - uses: actions/checkout@v4 24 | - name: Set up Ruby 25 | uses: ruby/setup-ruby@v1 26 | with: 27 | ruby-version: ${{matrix.ruby}} 28 | bundler-cache: true 29 | - name: Run tests 30 | run: bundle exec rake 31 | 32 | test-windows: 33 | name: Ruby ${{ matrix.ruby }} (windows-latest) 34 | if: "!contains(github.event.head_commit.message, '[skip ci]')" 35 | strategy: 36 | matrix: 37 | ruby: 38 | - '3.3' 39 | - '3.2' 40 | - '3.1' 41 | - '3.0' 42 | runs-on: windows-latest 43 | steps: 44 | - uses: actions/checkout@v4 45 | - name: Set up Ruby 46 | uses: ruby/setup-ruby@v1 47 | with: 48 | ruby-version: ${{matrix.ruby}} 49 | bundler-cache: true 50 | - name: Run tests 51 | run: bundle exec rake 52 | --------------------------------------------------------------------------------