summary refs log tree commit diff stats
path: root/ranger/fsobject/file.py
diff options
context:
space:
mode:
Diffstat (limited to 'ranger/fsobject/file.py')
-rw-r--r--ranger/fsobject/file.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/ranger/fsobject/file.py b/ranger/fsobject/file.py
index 86621095..15bfc18b 100644
--- a/ranger/fsobject/file.py
+++ b/ranger/fsobject/file.py
@@ -13,6 +13,31 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+control_characters = set(chr(n) for n in set(range(0, 9)) | set(range(14,32)))
+
 from .fsobject import FileSystemObject as SuperClass
 class File(SuperClass):
 	is_file = True
+
+	@property
+	def first4bytes(self):
+		try:
+			return self._first4bytes
+		except:
+			try:
+				f = open(self.path, 'r')
+				self._first4bytes = f.read(4)
+				f.close()
+				return self._first4bytes
+			except:
+				pass
+
+	def is_binary(self):
+		if not self.first4bytes:
+			return
+		if self.first4bytes == "\x7F\x45\x4C\x46":
+			return True
+		if control_characters & set(self.first4bytes):
+			return True
+		return False
+