Skip to content

Commit

Permalink
Add --music/--music-relative option (close #47)
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed Jan 12, 2017
1 parent 35271ae commit a2cff50
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Add `--music`/`--music-relative` command line switch for quickly entering music on the command line (#47).

# Version 1.3.4

- Recover from trying to open broken Lilypond archives.
Expand Down
4 changes: 3 additions & 1 deletion bin/lilypond
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ Lyp-provided options:
-c, --cropped crop output (requires setting 0 margins)
-E, --env use version specified in $LILYPOND_VERSION
-F, --force-version use lilypond version specified in user file
-m, --music-relative=MUSIC enter music inline (relative pitch)
-M, --music=MUSIC enter music inline (absolute pitch)
-n, --install install the specified version if not found
-O, --open open the target file after compilation
-R, --raw run raw lilypond (no pre-processing)
-r, --require=PACKAGE preload the specified package
-R, --raw run raw lilypond (no pre-processing)
-S, --snippet produce png cropped images at 600dpi
(--cropped --png -dresolution=600)
-u, --use=VERSION use the given version of lilypond
Expand Down
5 changes: 5 additions & 0 deletions lib/lyp/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ def self.ensure_dir(dir)
FileUtils.mkdir_p(dir) unless File.directory?(dir)
dir
end

def self.tmp_filename(suffix = nil)
fn = (Thread.current.hash * (Time.now.to_f * 1000).to_i % 2**32).to_s(36)
"#{TMP_ROOT}/#{fn}#{suffix}"
end

def self.sudo_cp(src, dest)
cmd = "sudo cp #{src} #{dest}"
Expand Down
24 changes: 23 additions & 1 deletion lib/lyp/lilypond.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def parse_lilypond_arg(arg, argv, argv_clean, options)
tmp_args = []
$1.each_char {|c| tmp_args << "-#{c}"}
tmp_args << "-#{$2}"
argv = tmp_args + argv
argv.insert(0, *tmp_args)# = tmp_args + argv
when '-A', '--auto-install-deps'
options[:resolve] = true
when '-c', '--cropped'
Expand All @@ -38,6 +38,12 @@ def parse_lilypond_arg(arg, argv, argv_clean, options)
options[:include_paths] ||= []
options[:include_paths] << path
argv_clean << "--include=#{path}"
when '-M', '--music'
fn = prepare_inline_music_file(argv.shift)
argv << fn
when '-m', '--music-relative'
fn = prepare_inline_music_file(argv.shift, relative: true)
argv << fn
when '-n', '--install'
options[:install] = true
when '-O', '--open'
Expand Down Expand Up @@ -66,6 +72,22 @@ def parse_lilypond_arg(arg, argv, argv_clean, options)
end
end

def prepare_inline_music_file(music, opts = {})
filename = Lyp.tmp_filename('.ly')
File.open(filename, 'w+') do |f|
f << format_inline_music(music, opts)
end
filename
end

def format_inline_music(music, opts)
if opts[:relative]
"\\relative c' { #{music} }"
else
"{ #{music} }"
end
end

VERSION_STATEMENT_REGEX = /\\version "([^"]+)"/

def select_lilypond_version(opts, file_path)
Expand Down

0 comments on commit a2cff50

Please sign in to comment.