summary refs log tree commit diff stats
path: root/code/fm.py
blob: 924f6efc8394f07d0a985fcfe1057a401216b5b8 (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
import sys, os
import ui, debug, file, directory, fstype

class FM():
	def __init__(self, environment):
		self.env = environment

	def feed(self, path, ui):
		self.ui = ui
		self.env.path = path
		self.enter_dir(path)

	def enter_dir(self, path):
		# get the absolute path
		path = os.path.normpath(os.path.join(self.env.path, path))

		self.env.path = path
		self.env.pwd = self.env.get_directory(path)

		self.env.pwd.load_content()

		# build the pathway, a tuple of directory objects which lie
		# on the path to the current directory.
		pathway = []
		currentpath = '/'
		for dir in path.split('/'):
			currentpath = os.path.join(currentpath, dir)
			debug.log(currentpath)
			pathway.append(self.env.get_directory(currentpath))
		self.env.pathway = tuple(pathway)

		# set the current file.
		if len(self.env.pwd) > 0:
			self.env.cf = self.env.pwd[0]
		else:
			self.env.cf = None

	def run(self):
		while 1:
			try:
				self.ui.draw()
			except KeyboardInterrupt:
				self.interrupt()
			except:
				raise

			try:
				key = self.ui.get_next_key()
				self.press(key)
			except KeyboardInterrupt:
				self.interrupt()

	def press(self, key):
		if (key == ord('q')):
			raise SystemExit()
		elif (key == ord('h')):
			self.enter_dir('..')
		elif (key == ord('l')):
			self.enter_dir(self.env.cf.path)

	def interrupt(self):
		import time
		self.buffer = ""
		time.sleep(0.2)