summary refs log tree commit diff stats
diff options
context:
space:
mode:
authortoonn <toonn@toonn.io>2019-04-23 23:09:46 +0200
committertoonn <toonn@toonn.io>2019-04-23 23:17:58 +0200
commitfc3a9e97c20572dceeb28df12ad4d06380038ba4 (patch)
tree1b35b4f02e261f3250c3a9b5b187935ca64ed260
parentdfe2ae6592904a4095d8added21f7f237b9a06dc (diff)
downloadranger-fc3a9e97c20572dceeb28df12ad4d06380038ba4.tar.gz
Move helper into ranger/ext
There's already a `human_readable` for size in `ranger/ext`, I figure we
might as well add the new helper there, make it easily accessible in
other code.

Did some renaming, no need to drop the `readable` part to shorten the
names. Similarly I changed one of the mappings to be more intuitive.
-rw-r--r--ranger/config/rc.conf4
-rw-r--r--ranger/container/fsobject.py6
-rw-r--r--ranger/core/linemode.py26
-rw-r--r--ranger/ext/human_readable.py16
4 files changed, 28 insertions, 24 deletions
diff --git a/ranger/config/rc.conf b/ranger/config/rc.conf
index d01f38c1..8913213c 100644
--- a/ranger/config/rc.conf
+++ b/ranger/config/rc.conf
@@ -371,10 +371,10 @@ map <C-p> chain console; eval fm.ui.console.history_move(-1)
 map Mf linemode filename
 map Mi linemode fileinfo
 map Mm linemode mtime
-map Mh linemode humanmtime
+map Mh linemode humanreadablemtime
 map Mp linemode permissions
 map Ms linemode sizemtime
-map Mg linemode sizehumanmtime
+map MH linemode sizehumanreadablemtime
 map Mt linemode metatitle
 
 # Tagging / Marking
diff --git a/ranger/container/fsobject.py b/ranger/container/fsobject.py
index 33633bcf..a82532c9 100644
--- a/ranger/container/fsobject.py
+++ b/ranger/container/fsobject.py
@@ -13,7 +13,7 @@ from time import time
 from ranger.core.linemode import (
     DEFAULT_LINEMODE, DefaultLinemode, TitleLinemode,
     PermissionsLinemode, FileInfoLinemode, MtimeLinemode, SizeMtimeLinemode,
-    HumanMtimeLinemode, SizeHumanMtimeLinemode
+    HumanReadableMtimeLinemode, SizeHumanReadableMtimeLinemode
 )
 from ranger.core.shared import FileManagerAware, SettingsAware
 from ranger.ext.shell_escape import shell_escape
@@ -92,8 +92,8 @@ class FileSystemObject(  # pylint: disable=too-many-instance-attributes,too-many
     linemode_dict = dict(
         (linemode.name, linemode()) for linemode in
         [DefaultLinemode, TitleLinemode, PermissionsLinemode, FileInfoLinemode,
-         MtimeLinemode, SizeMtimeLinemode, HumanMtimeLinemode,
-         SizeHumanMtimeLinemode]
+         MtimeLinemode, SizeMtimeLinemode, HumanReadableMtimeLinemode,
+         SizeHumanReadableMtimeLinemode]
     )
 
     def __init__(self, path, preload=None, path_is_abs=False, basename_is_rel_to=None):
diff --git a/ranger/core/linemode.py b/ranger/core/linemode.py
index d7fd717c..48da3509 100644
--- a/ranger/core/linemode.py
+++ b/ranger/core/linemode.py
@@ -7,7 +7,7 @@ from __future__ import (absolute_import, division, print_function)
 
 from abc import ABCMeta, abstractproperty, abstractmethod
 from datetime import datetime
-from ranger.ext.human_readable import human_readable
+from ranger.ext.human_readable import human_readable, human_readable_time
 from ranger.ext import spawn
 
 DEFAULT_LINEMODE = "filename"
@@ -133,8 +133,8 @@ class SizeMtimeLinemode(LinemodeBase):
                           datetime.fromtimestamp(fobj.stat.st_mtime).strftime("%Y-%m-%d %H:%M"))
 
 
-class HumanMtimeLinemode(LinemodeBase):
-    name = "humanmtime"
+class HumanReadableMtimeLinemode(LinemodeBase):
+    name = "humanreadablemtime"
 
     def filetitle(self, fobj, metadata):
         return fobj.relative_path
@@ -142,11 +142,11 @@ class HumanMtimeLinemode(LinemodeBase):
     def infostring(self, fobj, metadata):
         if fobj.stat is None:
             return '?'
-        return _human_readable_mtime(fobj.stat.st_mtime)
+        return human_readable_time(fobj.stat.st_mtime)
 
 
-class SizeHumanMtimeLinemode(LinemodeBase):
-    name = "sizehumanmtime"
+class SizeHumanReadableMtimeLinemode(LinemodeBase):
+    name = "sizehumanreadablemtime"
 
     def filetitle(self, fobj, metadata):
         return fobj.relative_path
@@ -155,16 +155,4 @@ class SizeHumanMtimeLinemode(LinemodeBase):
         if fobj.stat is None:
             return '?'
         size = human_readable(fobj.size)
-        return "%s %11s" % (size, _human_readable_mtime(fobj.stat.st_mtime))
-
-
-def _human_readable_mtime(file_mtime):
-    file_date = datetime.fromtimestamp(file_mtime)
-    time_diff = datetime.now().date() - file_date.date()
-    if time_diff.days >= 365:
-        return file_date.strftime("%-d %b %Y")
-    if time_diff.days >= 7:
-        return file_date.strftime("%-d %b")
-    if time_diff.days >= 1:
-        return file_date.strftime("%a")
-    return file_date.strftime("%H:%M")
+        return "%s %11s" % (size, human_readable_time(fobj.stat.st_mtime))
diff --git a/ranger/ext/human_readable.py b/ranger/ext/human_readable.py
index 385e56f4..2a5bc81b 100644
--- a/ranger/ext/human_readable.py
+++ b/ranger/ext/human_readable.py
@@ -3,6 +3,7 @@
 
 from __future__ import (absolute_import, division, print_function)
 
+from datetime import datetime
 from ranger.core.shared import SettingsAware
 
 
@@ -54,6 +55,21 @@ def human_readable(byte, separator=' '):  # pylint: disable=too-many-return-stat
     return '>9000'
 
 
+def human_readable_time(timestamp):
+    """Convert a timestamp to an easily readable format.
+    """
+    # Hard to test because it's relative to ``now()``
+    date = datetime.fromtimestamp(timestamp)
+    datediff = datetime.now().date() - date.date()
+    if datediff.days >= 365:
+        return date.strftime("%-d %b %Y")
+    elif datediff.days >= 7:
+        return date.strftime("%-d %b")
+    elif datediff.days >= 1:
+        return date.strftime("%a")
+    return date.strftime("%H:%M")
+
+
 if __name__ == '__main__':
     import doctest
     doctest.testmod()