File: Readme.md

package info (click to toggle)
ruby-guard-shell 0.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 96 kB
  • sloc: ruby: 104; makefile: 3
file content (104 lines) | stat: -rw-r--r-- 2,224 bytes parent folder | download | duplicates (4)
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
102
103
104
# Guard::Shell

This little guard allows you to run shell commands when files are altered.


## Install

Make sure you have [guard](http://github.com/guard/guard) installed.

Install the gem with:

    gem install guard-shell

Or add it to your Gemfile:

    gem 'guard-shell'

And then add a basic setup to your Guardfile:

    guard init shell


## Usage

If you can do something in your shell, or in ruby, you can do it when a file changes
with guard-shell. It simply executes the block passed to watch if a change is 
detected, and if anything is returned from the block it will be printed. For example

``` ruby
guard :shell do
  watch /.*/ do |m|
    m[0] + " has changed."
  end
end
```

will simply print a message telling you a file has been changed when it is changed.
This admittedly isn't a very useful example, but you hopefully get the idea. To run
everything on start pass `:all_on_start` to `#guard`,

``` ruby
guard :shell, :all_on_start => true do
  # ...
end
```

There is also a shortcut for easily creating notifications,

``` ruby
guard :shell do
  watch /.*/ do |m|
    n m[0], 'File Changed'
  end
end
```

`#n` takes up to three arguments; the first is the body of the message, here the path
of the changed file; the second is the title for the notification; and the third is
the image to use. There are three (four counting `nil` the default) different images
that can be specified `:success`, `:pending` and `:failed`.


### Examples

#### Saying the Name of the File You Changed and Displaying a Notification

``` ruby
guard :shell do
  watch /(.*)/ do |m|
    n m[0], 'Changed'
    `say -v cello #{m[0]}`
  end
end
```

#### Rebuilding LaTeX

``` ruby
guard :shell, :all_on_start => true do
  watch /^([^\/]*)\.tex/ do |m|
    `pdflatex -shell-escape #{m[0]}`
    `rm #{m[1]}.log`

    count = `texcount -inc -nc -1 #{m[0]}`.split('+').first
    msg = "Built #{m[1]}.pdf (#{count} words)"
    n msg, 'LaTeX'
    "-> #{msg}"
  end
end
```

#### Check Syntax of a Ruby File

``` ruby
guard :shell do
  watch /.*\.rb$/ do |m|
    if system("ruby -c #{m[0]}")
      n "#{m[0]} is correct", 'Ruby Syntax', :success
    else
      n "#{m[0]} is incorrect", 'Ruby Syntax', :failed
    end
  end
end
```