about summary refs log tree commit diff stats
path: root/stabbertests
diff options
context:
space:
mode:
authorDaniel Santos <dan.git@brilhante.top>2023-05-10 18:52:55 +0100
committerDaniel Santos <dan.git@brilhante.top>2023-07-01 10:08:20 +0100
commit953e3cc1204cde0f59c0d1230694b5ad5682c698 (patch)
treef8c523cae74f5c6186e8d576c0c9209baa394656 /stabbertests
parent439406a0def941b18850685174dde5daaca7dbf0 (diff)
downloadprofani-tty-main.tar.gz
save file as a *-0 instead of *.0 HEAD main
Signed-off-by: Daniel Santos <dan.git@brilhante.top>
Diffstat (limited to 'stabbertests')
0 files changed, 0 insertions, 0 deletions
6cc2f4007e99b6b2be35a821857da'>^
34bfb32e ^

00ec7736 ^
34bfb32e ^



adfea091
34bfb32e ^

adfea091


34bfb32e ^
adfea091
34bfb32e ^
adfea091




34bfb32e ^
adfea091

2cf9236d ^
adfea091




34bfb32e ^
adfea091

34bfb32e ^
8a6f5eab ^
34bfb32e ^
a0de7f95 ^










00ec7736 ^








34bfb32e ^
00ec7736 ^
a0de7f95 ^
622ff1dc ^
34bfb32e ^
adfea091
34bfb32e ^
a0de7f95 ^
34bfb32e ^
adfea091


34bfb32e ^

622ff1dc ^
34bfb32e ^

00ec7736 ^
8a6f5eab ^
34bfb32e ^
adfea091
00ec7736 ^
adfea091






00ec7736 ^
adfea091



46069e7e ^




00ec7736 ^
a0de7f95 ^
adfea091
46069e7e ^
34bfb32e ^





adfea091
d73f76fc ^
adfea091
34bfb32e ^
87e935f7 ^


adfea091
34bfb32e ^
adfea091
46069e7e ^

adfea091
34bfb32e ^
adfea091



00ec7736 ^
adfea091
00ec7736 ^
a0de7f95 ^
00ec7736 ^
34bfb32e ^






a0de7f95 ^


34bfb32e ^





00ec7736 ^
34bfb32e ^
a0de7f95 ^
adfea091















34bfb32e ^
adfea091
a0de7f95 ^
34bfb32e ^




a0de7f95 ^
adfea091

34bfb32e ^
adfea091




622ff1dc ^
adfea091







00ec7736 ^

a0de7f95 ^





adfea091
00ec7736 ^
34bfb32e ^

adfea091
34bfb32e ^
adfea091
34bfb32e ^
adfea091
34bfb32e ^
adfea091

00ec7736 ^
34bfb32e ^

adfea091
adfea091
34bfb32e ^

adfea091

34bfb32e ^








adfea091
34bfb32e ^




adfea091

34bfb32e ^
adfea091

00ec7736 ^
34bfb32e ^






adfea091

34bfb32e ^
adfea091

00ec7736 ^
34bfb32e ^



adfea091
adfea091


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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

                          




                                


         
                   

                          
                   

                                                              
        



                                    
                            

                                   


                                                     
                                 
 
                                                                            




                                   
                                

                                                   
                                                                          




                                                      
                                               

                                  
                                                                     
                                     
 










                                                      








                                    
 
                            
                                              
                               
 
                                                 
                                                               
                                       
                              


                                    

                              
                             

           
                 
                                       
           
 
                






                                              
                        



                            




                                                               
                     
                            
                          
                                                





                                                         
                             
                                      
                                        
                                            


                                           
                           
 
                             

                                          
                                        
                                            



                           
                                        
 
                                                           
 
                                 






                                                             


                                                   





                                                 
                          
                                    
                            















                                                                                  
 
                                 
                                   




                                                      
                             

                                            
                                                                               




                                               
                                  







                                                                

                                                  





                                     
 
                              

                                           
                   
                                                         
 
                                         
 
                               

           
                               

                              
                   
 

                                                         

                   








                                                                       
                            




                                                                      

                           
                          

           
                        






                                                            

                           
                            

           
                        



                                          
                   


           
OPTIONS = {
	'hidden' => false,
	'sort' => :name,
	'dir_first' => true,
	'sort_reverse' => false,
	'color' => true,
	'filepreview' => true,
}

module Fm
	extend self
	COPY_PRIORITY = -2

	COLUMNS = 4
	VI = "vi -c 'map h :quit<CR>' -c 'map q :quit<CR>'" <<
		" -c 'map H :unmap h<CR>:unmap H<CR>' %s"
	
	def self.initialize(pwd=nil)
		@bars = []
		@bars_thread = nil
		
		@buffer = ''
		@pwd = nil
		@search_string = ''
		@copy = []
		@ignore_until = nil
		@trash = File.expand_path('~/.trash')
		pwd ||= Dir.getwd

		# `' and `` are the original PWD unless overwritten by .fmrc
		@memory = {
			'`' => pwd,
			'\'' => pwd
		}

		# Read the .fmrc
		@fmrc = File.expand_path('~/.fmrc')
		if (File.exists?(@fmrc))
			loaded = Marshal.load(File.read(@fmrc)) rescue nil
			if Hash === loaded
				@memory.update(loaded)
			end
		end

		# `0 is always the original PWD
		@memory['0'] = pwd

		# Give me some way to redraw screen while waiting for
		# input from CLI.geti

#		for i in 1..20
#			eval "Signal.trap(#{i}) do
#				log #{i}
#				exit if #{i} == 9 end"
#		end

		boot_up(pwd)
	end

	attr_reader(:dirs, :pwd)

	def pwd() @pwd end

	def refresh()
		begin
			@pwd.refresh
			draw
		rescue
		end
	end

	def boot_up(pwd=nil)
		pwd ||= @pwd.path || Dir.getwd
		Scheduler.reset

		@dirs = Hash.new() do |hash, key|
			hash[key] = newdir = Directory.new(key)
#			newdir.schedule
			newdir
		end

		@path = [@dirs['/']]
		enter_dir(pwd)

		Scheduler.run
	end

	def lines
		CLI::lines - @bars.size
	end

	def dump
		remember_dir
		dumped = Marshal.dump(@memory)
		File.open(@fmrc, 'w') do |f|
			f.write(dumped)
		end
	end

	def on_interrupt
		@buffer = ''
		sleep 0.2
	end

	def terminal_killed?
#		`ps ho tname --pid #{Process.pid}`.strip == '?'
		Process.ppid == 1
	end

	def main_loop
		bool = false
		while true
			exit if terminal_killed?
			if @pwd.size == 0 or @pwd.pos < 0
				@pwd.pos = 0
			elsif @pwd.pos >= @pwd.size - 1
				@pwd.pos = @pwd.size - 1
			end

			begin
				draw()
			rescue Interrupt
				on_interrupt
			rescue Exception
				log($!)
				log(caller)
			end

			begin
				key = geti
				press(key)
			rescue Interrupt
				on_interrupt
			end
		end
	end

	def current_path() @pwd.path end

	def reset_title() set_title("fm: #{@pwd.path}") end

	def enter_dir_safely(dir)
		dir = File.expand_path(dir)
		if File.exists?(dir) and File.directory?(dir)
			olddir = @pwd.path
			begin
				enter_dir(dir)
				return true
			rescue
				log("NIGGER" * 100)
				log($!)
				log(caller)
				enter_dir(olddir)
				return false
			end
		end
	end

	def enter_dir(dir)
		@pwd.restore if @pwd
		@marked = []
		dir = File.expand_path(dir)

		oldpath = @path.dup

		# NOTE: @dirs[unknown] is not nil but Directory.new(unknown)
		@path = [@dirs['/']]
		unless dir == '/'
			dir.slice(0)
			accumulated = '/'
			for part in dir.split('/')
				unless part.empty?
					accumulated = File.join(accumulated, part)
					@path << @dirs[accumulated]
				end
			end
		end

		@pwd = @path.last
		@pwd.pos = @pwd.pos

		@pwd.files_raw.dup.each do |x|
			@dirs[x] if File.directory?(x)
		end

		reset_title()

		if @path.size < oldpath.size
			@pwd.pos = @pwd.files_raw.index(oldpath.last.path) || 0
		end

		i = 0

		@path.each_with_index do |p, i|
			p.schedule
			unless i == @path.size - 1
				p.pointed_file = @path[i+1].path
			end
		end

		Dir.chdir(@pwd.path)
	end

	def currentfile() @pwd.files[@pwd.pos] end
	def selection()
		if @marked.empty?
			[currentfile]
		else
			@marked.dup
		end
	end

	def move_to_trash!(fn)
		unless File.exists?(@trash)
			Dir.mkdir(@trash)
		end
		new_path = File.join(@trash, fn.basename)

		Action.move(fn, new_path)

		return new_path
	end

	def move_to_trash(file)
		unless file
			return
		end

		if String === file
			file = Directory::Entry.new(file)
		end

		if file.exists?
			if file.dir?
				if !file.in?(@trash) and file.size > 0
					return move_to_trash!(file)
				else
					Dir.rmdir(file.path) rescue nil
				end
			elsif file.symlink?
				file.delete!
			else
				if !file.in?(@trash) and file.size > 0
					return move_to_trash!(file)
				else
					file.delete!
				end
			end
		end
		return nil
	end

	def bar_add(bar)
		if @bars.empty?
			# This thread updates the statusbars
			@bars_thread = Thread.new do
				while true
					draw_bars
					sleep 0.5
				end
			end
		end
		@bars << bar
	end

	def bar_del(bar)
		@bars.delete(bar)
		if @bars.empty?
			@bars_thread.kill
			@bars_thread = nil
		end
	end
end