summary refs log tree commit diff stats
path: root/code/action.rb
blob: 34a072429b3115fc58858c3474530c35166c0acb (plain) (blame)
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
module Action
	extend self

	def close_interface
		closei
	end

	def start_interface
		starti
	end

	def copy(files, path)
		files = [files] unless files.is_a? Array
		unless files.empty?
			CopyBar2.new(files, path)
		end
	end

	def move(files, path)
		files = [files] unless files.is_a? Array
		unless files.empty?
			MoveBar2.new(files, path)
		end
	end

	def run(rc = nil)
		rc ||= RunContext.new(Fm.getfiles)
		assert rc, RunContext

		cf       = Fm.currentfile
		
		all      = rc.all.or true
		files    = rc.files.or(all ? Fm.selection : [cf])
		mode     = rc.mode.or 0
		newway   = rc.newway.or false

		return false if files.nil?

		if newway
#			logpp files.first.handler
#			rc = Fm.filehandler(files, struct)
			handler = rc.exec
		else
			handler, wait = Fm.getfilehandler(*files)
		end

		return false unless handler

		wait     = rc.wait.or wait
		new_term = rc.new_term.or false
		detach   = rc.detach.or false

		log handler
		if detach
			run_detached(handler, new_term)
		else
			run_inside(handler, wait)
		end
		return true
	end

	def run_detached(what, new_term)
		if new_term
			p = fork { exec('x-terminal-emulator', '-e', 'bash', '-c', what) }
#			Process.detach(p)
		else
			p = fork { exec "#{what} 2>> /dev/null >> /dev/null" }
			Process.detach(p)
		end
	end

	def run_inside(what, wait)
		close_interface
		system(*what)
		wait_for_enter if wait
		start_interface
	end

	def wait_for_enter
		print "Press [ENTER] to continue..."
		gets
	end
end