Class: Tweemux::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/tweemux/action.rb

Direct Known Subclasses

Forward, Help, Host, Hubkey, Log, On, Share, Version

Defined Under Namespace

Classes: At, DubiousSystemInvocation, Forward, FunkyUsage, Help, Host, Hubkey, Log, NoRestartsException, NoSuchHomeDir, On, Share, Version

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Action

Returns a new instance of Action.



5
# File 'lib/tweemux/action.rb', line 5

def initialize args; @args = args end

Class Method Details

.colorize_tmux_command(arr) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tweemux/action.rb', line 65

def colorize_tmux_command arr
  # Index access in Ruby is a smell. @ConradIrwin, help me!!
  socket_idx = arr.find_index '-S'
  arr.inject [] do |a,e|
    a <<
      if socket_idx and (e == arr[socket_idx] or e == arr[socket_idx+1])
        e.color :gray245
      else
        e.color :brighty_blue
      end
  end.join ' '
end

.explain(what, why) ⇒ Object



47
48
49
# File 'lib/tweemux/action.rb', line 47

def explain what, why
  warn highlight_command(what) + highlight_explanation(why)
end

.explained_run(what, why) ⇒ Object



35
36
37
38
39
40
# File 'lib/tweemux/action.rb', line 35

def explained_run what, why
raise DubiousSystemInvocation, "Given string-arg of #{what}" \
  if what.is_a? String
    explain what, why
    system_or_raise what
end

.explained_run_as(user, what, why) ⇒ Object



42
43
44
45
# File 'lib/tweemux/action.rb', line 42

def explained_run_as user, what, why
  full_command = ['sudo', '-u', user] + what
  explained_run full_command, why
end

.highlight_command(arr) ⇒ Object



55
56
57
58
59
# File 'lib/tweemux/action.rb', line 55

def highlight_command arr
  ': Running'.color(:middle_blue) \
    + '; '.color(:gray245) \
    + colorize_tmux_command(arr)
end

.highlight_explanation(msg) ⇒ Object



61
62
63
# File 'lib/tweemux/action.rb', line 61

def highlight_explanation msg
  '  # '.color(:orange) + msg.color(:lemon)
end

.load_all!Object



102
103
104
105
106
107
# File 'lib/tweemux/action.rb', line 102

def load_all!
  dir = __FILE__.sub /\.rb$/, ''
  Dir[dir + '/*.rb'].each do |e|
    require e.sub /.*(tweemux\/.+)\.rb$/, '\1'
  end
end

.pseudo_restarts(cmd) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tweemux/action.rb', line 78

def pseudo_restarts cmd
  warn '# failed ☹'.color :error
  ctrl_c = 'Ctrl+c'.color :keypress, :prompt
  enter = 'Enter'.color :keypress, :prompt
  letter_p = 'p'.color :keypress, :prompt
  # TODO: work pry-rescue into this so we can offer a 'try-again'
  # See also: https://github.com/ConradIrwin/pry-rescue/issues/29
  print <<-EOT.chomp.color :prompt
   To give up, hit: #{ctrl_c}
To run anyway, hit: #{enter}
  To pry from here: #{letter_p}
> 
  EOT
  answer = $stdin.readline
  if answer[/p/i]
    notice = 'I will be very impressed if this is useful...'
    warn notice.color :error
    require 'pry'
    binding.pry
  end
rescue Interrupt
  exit 2
end

.system_or_raise(cmd) ⇒ Object



51
52
53
# File 'lib/tweemux/action.rb', line 51

def system_or_raise cmd
  system *cmd or pseudo_restarts cmd.join(' ')
end

.tmux_S(args, why) ⇒ Object



30
31
32
33
# File 'lib/tweemux/action.rb', line 30

def tmux_S args, why
  cmd = %W(tmux -S #{SOCK}) + args
  explained_run cmd, why
end

Instance Method Details

#callObject



7
8
9
# File 'lib/tweemux/action.rb', line 7

def call
  run @args
end

#explained_run(what, why) ⇒ Object

Hrm. These are kinda gross, but I feel like it cleans up the subclasses



18
19
20
# File 'lib/tweemux/action.rb', line 18

def explained_run what, why
  self.class.explained_run what, why
end

#explained_run_as(who, what, why) ⇒ Object



21
22
23
# File 'lib/tweemux/action.rb', line 21

def explained_run_as who, what, why
  self.class.explained_run_as who, what, why
end

#run(args) ⇒ Object



15
# File 'lib/tweemux/action.rb', line 15

def run args; raise 'Unimplemented' end

#tmux_S(*a) ⇒ Object



24
25
26
# File 'lib/tweemux/action.rb', line 24

def tmux_S *a
  self.class.tmux_S *a
end