Skip to content

Commit

Permalink
Includes totally revamped, easy to use command line interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Maciel committed Oct 16, 2018
1 parent e082bfa commit dd094c9
Show file tree
Hide file tree
Showing 67 changed files with 818 additions and 508 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .ruby-version
@@ -0,0 +1 @@
2.5.1
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,9 @@
- Fix chords so that they generate a barre and a non-barre version and prevent
barre chords from picking notes before the barre.

## [4.0.0]
- Includes totally revamped, easy to use command line interface.

## [3.4.2]
- Fixes guitar strings rendering order (#41)

Expand Down
19 changes: 15 additions & 4 deletions Gemfile.lock
@@ -1,18 +1,24 @@
PATH
remote: .
specs:
coltrane (3.4.2)
coltrane (4.0.0)
activesupport (> 5.2)
cli-ui (~> 1.1)
color (~> 1.8)
dry-monads (~> 0.4)
paint (~> 2.0)
pedrozath-mercenary (= 0.3.8)
tty-reader (~> 0.2)

GEM
remote: https://rubygems.org/
specs:
activesupport (5.2.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
ast (2.4.0)
byebug (10.0.2)
cli-ui (1.1.4)
coderay (1.1.2)
color (1.8)
concurrent-ruby (1.0.5)
Expand All @@ -27,8 +33,11 @@ GEM
equatable (0.5.0)
hike (1.2.3)
hitimes (1.2.6)
i18n (1.1.1)
concurrent-ruby (~> 1.0)
json (2.1.0)
method_source (0.9.0)
minitest (5.11.3)
necromancer (0.4.0)
opal (0.10.5)
hike (~> 1.2)
Expand All @@ -44,7 +53,6 @@ GEM
pastel (0.7.2)
equatable (~> 0.5.0)
tty-color (~> 0.4.0)
pedrozath-mercenary (0.3.8)
powerpack (0.1.1)
pry (0.11.3)
coderay (~> 1.1.0)
Expand Down Expand Up @@ -85,6 +93,7 @@ GEM
sprockets (3.7.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
thread_safe (0.3.6)
tilt (2.0.8)
timers (4.1.2)
hitimes
Expand All @@ -101,6 +110,8 @@ GEM
tty-screen (~> 0.6.4)
wisper (~> 2.0.0)
tty-screen (0.6.4)
tzinfo (1.2.5)
thread_safe (~> 0.1)
unicode-display_width (1.3.0)
wisper (2.0.0)

Expand Down
29 changes: 29 additions & 0 deletions bin/old-coltrane
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'old-coltrane' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)

bundle_binstub = File.expand_path("../bundle", __FILE__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("coltrane", "old-coltrane")
8 changes: 4 additions & 4 deletions coltrane.gemspec
Expand Up @@ -32,11 +32,11 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_runtime_dependency 'tty-reader', '~> 0.2'
spec.add_runtime_dependency 'dry-monads', '~> 0.4'
spec.add_runtime_dependency 'paint', '~> 2.0'
spec.add_runtime_dependency 'color', '~> 1.8'
spec.add_runtime_dependency 'pedrozath-mercenary', '0.3.8'
spec.add_development_dependency "bundler", '~> 1.14'
spec.add_development_dependency "rake", '~> 10.0'
spec.add_runtime_dependency 'cli-ui', '~> 1.1'
spec.add_runtime_dependency 'activesupport', '> 5.2'
spec.add_development_dependency "bundler", '~> 1.14'
spec.add_development_dependency "rake", '~> 10.0'
end
130 changes: 83 additions & 47 deletions exe/coltrane
@@ -1,65 +1,101 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))

require 'paint'
require 'color'

require 'pedrozath-mercenary'
require 'coltrane/commands'
$debug = require 'pry'
require 'coltrane/ui'

if ENV['COLORTERM'] == 'truecolor'
Paint.mode = 0xFFFFFF
else
Paint.mode = 0
puts "This terminal doesn't seem to support true color. " \
"Try something like iTerm2 (for macOS), VTE or Konsole (for linux) or Hyper" \
" (for Windows) and you'll see the true magic!"
def debugger
binding.pry unless $debug
end

Mercenary.program(:Coltrane) do |p|
p.version Coltrane::VERSION
p.description <<~DESC
A music querying interface
by Pedro Maciel (pedro@pedromaciel.com)
module Coltrane
module Cli
class App
attr_reader :router

Check the chat room for project help/discussion/contribution: https://coltrane-ruby.herokuapp.com
DESC
p.syntax 'coltrane <subcommand> [options]'
def self.start(initial_route)
if ENV['COLORTERM'] == 'truecolor'
Paint.mode = 0xFFFFFF
else
Paint.mode = 0
puts "This terminal doesn't seem to support true color. " \
"Try something like iTerm2 (for macOS), VTE or Konsole (for linux) or Hyper" \
" (for Windows) and you'll see the true magic!"
end

Coltrane::Commands::Command.subclasses.each do |command|
command.mercenary_init(p)
end
path, params = initial_route.split(' ').each_with_object([nil, {}]) do |segment, memo|
if segment =~ /:/
key, value = segment.split(':')
memo[1].merge!([[key.to_sym, value.gsub('-', ' ')]].to_h)
else
memo[0] = [memo[0], segment].compact.join(' ')
end
end

p.command(:about) do |c|
c.description 'Shows this screen'
c.action { puts p }
end
response = {path: (path || ''), **params}
loop { response = app.flow(**response) }
end

p.command(:shell) do |c|
c.description 'Starts an interactive shell'
c.action do
system File.expand_path('../../bin', __FILE__) + "/coltrane-interactive"
end
end
def self.app
@app ||= new
end

def initialize
@router = Router.new
end

p.command(:help) do |c|
c.description 'May give you some help.'
c.syntax 'help <command> [subcommand, sub-subcommand, ...]'
c.action do |(*command_path), _options|
if command_path.empty?
puts p
else
puts begin
command_path.reduce(p) do |memo, key|
memo.commands.delete(key.to_sym)
end || "\n Sorry, command found."
def ask_questions(questions)
questions.map { |name, question| [name, ask_question(question)] }.to_h
end

def ask_question(question)
CLI::UI::Prompt.ask(question[:statement], options: question[:options])
end

def navigation(params)
return {} if params.empty?
CLI::UI::Prompt.ask('What to do now?') do |h|
params.each do |key, _value|
h.option("Different #{key}") { params[key] = nil }
end
h.option("Go back") { params[:path] = '' }
end
params
end

def output(response)
return unless response[:content]
puts [
nil,
"-> coltrane #{router.url}",
nil,
response[:content],
nil
]
end

def flow(response)
raise 'Response is empty' unless response
output(response)
response.delete(:content)
questions = response&.delete(:questions) || {}
response.merge!(navigation(response)) unless response.has_key?(:path) || questions.any?
response.merge!(ask_questions(questions)) if questions.any?
exit if response[:path] == 'exit'
router.get(**response.compact)
rescue => e
response.delete(:path)
puts e
puts e.backtrace if $debug
retry
end

def exit
Kernel.exit(0)
end
end
end

p.default_command(:shell)
end

Coltrane::Cli::App.start(ARGV.join(' '))
Binary file added lib/.DS_Store
Binary file not shown.
Binary file added lib/coltrane/.DS_Store
Binary file not shown.
13 changes: 4 additions & 9 deletions lib/coltrane/commands.rb
Expand Up @@ -3,13 +3,8 @@
require 'coltrane'
require 'coltrane/representation'
require 'coltrane/renderers'

require 'coltrane/commands/command'
require 'coltrane/commands/notes'
require 'coltrane/commands/chords'
require 'coltrane/commands/scale'
require 'coltrane/commands/progression'
require 'coltrane/commands/common_chords'
require 'coltrane/commands/find_scale'
require 'coltrane/commands/find_progression'
require 'coltrane/commands/find_guitar_chord'

Dir["#{Dir.pwd}/lib/coltrane/commands/*"].map do |command_file|
require(command_file)
end
9 changes: 9 additions & 0 deletions lib/coltrane/commands/available_chord_representations.rb
@@ -0,0 +1,9 @@
module Coltrane
module Commands
class AvailableChordRepresentations < Command
def run
return ['GuitarChordChart'] + AvailableRepresentations.run
end
end
end
end
9 changes: 9 additions & 0 deletions lib/coltrane/commands/available_classic_scales.rb
@@ -0,0 +1,9 @@
module Coltrane
module Commands
class AvailableClassicScales < Command
def run
Theory::Scale.known_scales
end
end
end
end
9 changes: 9 additions & 0 deletions lib/coltrane/commands/available_notable_progressions.rb
@@ -0,0 +1,9 @@
module Coltrane
module Commands
class AvailableNotableProgressions < Command
def run
Theory::NotableProgressions::PROGRESSIONS.keys
end
end
end
end
9 changes: 9 additions & 0 deletions lib/coltrane/commands/available_representations.rb
@@ -0,0 +1,9 @@
module Coltrane
module Commands
class AvailableRepresentations < Command
def run
['Text'] + Representation.constants.map(&:to_s)
end
end
end
end

0 comments on commit dd094c9

Please sign in to comment.