Skip to content

Commit

Permalink
Cleaned up some lingering old references.
Browse files Browse the repository at this point in the history
  • Loading branch information
Troy Spradling committed Jul 14, 2023
1 parent e0f594e commit 0bdc5d5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ xontrib load xlsd

### Registering an icon

In xlsd, icons are registered using a name. The name is then used by the different rules to get an icon for an `os.DirEntry`.
In xlsd, icons are registered using a name. The name is then used by the different rules to get an icon for an `PathEntry`.

You can view the built-in icons in [xlsd/icons.py](xlsd/icons.py#L99).

Expand Down Expand Up @@ -162,7 +162,7 @@ All the built-in columns are used in this config.

### Writing your own column

A column is a function taking for only argument an `os.DirEntry` and that outputs a string.
A column is a function taking for only argument an `PathEntry` and that outputs a string.

A simple filename column could be registered like this:
```python
Expand Down
2 changes: 1 addition & 1 deletion xlsd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def xlsd_sort_directories_first(entries: list[PathEntry]) -> list[PathEntry]:
directories.append(entry)
else:
files.append(entry)
except OSError: # Probably circular symbolic link
except OSError: # Probably circular symbolic link
directories.append(entry)

directories.sort(key=_direntry_lowercase_name)
Expand Down
6 changes: 3 additions & 3 deletions xlsd/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

class PathEntry:
"""
FileEntry is a utility class that represents a filesystem entry at a given path. The entry can represent a file,
PathEntry is a utility class that represents a filesystem entry at a given path. The entry can represent a file,
directory, or symbolic link. The class provides methods for checking the type of the entry and obtaining file
statistics for it.
The class can be used to mirror the behavior of os.DirEntry, because os.DirEntry objects returned by os.scandir()
The class can be used to mirror the behavior of PathEntry, because PathEntry objects returned by os.scandir()
do not handle individual file paths and do not allow selective control over following symbolic links for
different operations.
"""

def __init__(self, path: str) -> None:
"""
Initialize a new instance of the FileEntry class.
Initialize a new instance of the PathEntry class.
:param path: A string containing a path to a file or directory.
:type path: str
Expand Down
42 changes: 21 additions & 21 deletions xontrib/xlsd.xsh
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ ${...}.register('XLSD_LIST_COLUMNS', validate=is_string_seq, convert=csv_to_list
${...}.register('XLSD_ICON_SOURCES', validate=is_string_seq, convert=csv_to_list,
detype=list_to_csv, default=['extension', 'libmagic'])

XlsdColumn = Callable[[os.DirEntry],str]
XlsdColumn = Callable[[PathEntry],str]

#TODO see with xonsh devs, imo shouldn't crash
#_XLSD_COLUMNS: Dict[str, Tuple[XlsdColumn, ColumnAlignment]] = {}
Expand All @@ -148,7 +148,7 @@ def xlsd_register_column(name: str, alignment: ColumnAlignment):
return func
return decorator

XlsdIconSource = Callable[[os.DirEntry], Optional[str]]
XlsdIconSource = Callable[[PathEntry], Optional[str]]

#TODO see with xonsh devs, imo shouldn't crash
#_XLSD_ICON_SOURCES: Dict[str, XlsdIconSource] = {}
Expand Down Expand Up @@ -209,7 +209,7 @@ def _format_size(size: int) -> str:
# the 'magic' lib might only be included in arch linux, it doesn't seem to work
# on macos.
@xlsd_register_icon_source('libmagic')
def _xlsd_icon_source_libmagic(direntry: os.DirEntry) -> Optional[str]:
def _xlsd_icon_source_libmagic(direntry: PathEntry) -> Optional[str]:
"""
Return the icon for a direntry using the file's mimetype.
"""
Expand All @@ -232,7 +232,7 @@ def _xlsd_icon_source_libmagic(direntry: os.DirEntry) -> Optional[str]:


@xlsd_register_icon_source('extension')
def _xlsd_icon_source_extension(direntry: os.DirEntry) -> Optional[str]:
def _xlsd_icon_source_extension(direntry: PathEntry) -> Optional[str]:
"""
Return the emoji for a direntry using the file extension.
"""
Expand All @@ -254,7 +254,7 @@ def _xlsd_icon_source_extension(direntry: os.DirEntry) -> Optional[str]:
# /Icon sources #
#################

def _icon_for_direntry(entry: os.DirEntry) -> str:
def _icon_for_direntry(entry: PathEntry) -> str:
"""
Return the icon for a direntry.
"""
Expand All @@ -270,7 +270,7 @@ def _icon_for_direntry(entry: os.DirEntry) -> str:
return icons.LS_ICONS.get(name)


def _get_color_for_direntry(entry: os.DirEntry) -> str:
def _get_color_for_direntry(entry: PathEntry) -> str:
"""Return one or multiple xonsh colors for the entry using $LS_COLORS."""
colors = []

Expand Down Expand Up @@ -315,11 +315,11 @@ def _get_color_for_direntry(entry: os.DirEntry) -> str:
return "".join("{"+color+"}" for color in colors)


def _format_direntry_name(entry: os.DirEntry, show_target: bool = True) -> str:
def _format_direntry_name(entry: PathEntry, show_target: bool = True) -> str:
"""
Return a string containing a bunch of ainsi escape codes as well as the "width" of the new name.
"""
path = entry.path if not entry.is_symlink() else os.readlink(entry.path)
# path = entry.path if not entry.is_symlink() else os.readlink(entry.path)
name = entry.name

# Show the icon
Expand Down Expand Up @@ -349,18 +349,18 @@ def _format_direntry_name(entry: os.DirEntry, show_target: bool = True) -> str:

def _scan_dir(path: str):
"""
Scan the directory with the given path and yield FileEntry instances for each entry.
Scan the directory with the given path and yield PathEntry instances for each entry.
:param path: A string containing a path to a directory.
:type path: str
:yield: FileEntry instance for each entry in path.
:rtype: Iterator[FileEntry]
:yield: PathEntry instance for each entry in path.
:rtype: Iterator[PathEntry]
"""
for entry in os.scandir(path):
yield PathEntry(entry.path)


def _direntry_lowercase_name(entry: os.DirEntry) -> str:
def _direntry_lowercase_name(entry: PathEntry) -> str:
"""
Return the lowercase name for a DirEntry.
Expand All @@ -369,9 +369,9 @@ def _direntry_lowercase_name(entry: os.DirEntry) -> str:
return entry.name.lower()


def _get_entries(path: str, show_hidden: bool) -> List[os.DirEntry]:
def _get_entries(path: str, show_hidden: bool) -> List[PathEntry]:
"""
Return the list of DirEntrys for a path, sorted by name, directories first.
Return the list of PathEntry's for a path, sorted by name, directories first.
"""
entries = []

Expand Down Expand Up @@ -524,7 +524,7 @@ def _show_table(columns: List[List[str]], column_alignments: List[ColumnAlignmen
################

@xlsd_register_column('mode', ColumnAlignment.LEFT)
def _xlsd_column_mode(direntry: os.DirEntry) -> str:
def _xlsd_column_mode(direntry: PathEntry) -> str:
"""
Format the mode from the stat structure for a file.
"""
Expand All @@ -536,15 +536,15 @@ def _xlsd_column_mode(direntry: os.DirEntry) -> str:


@xlsd_register_column('hardlinks', ColumnAlignment.RIGHT)
def _xlsd_column_hardlinks(direntry: os.DirEntry) -> str:
def _xlsd_column_hardlinks(direntry: PathEntry) -> str:
"""
Show the number of hardlinks for a file.
"""
return str(direntry.stat(follow_symlinks=False).st_nlink)


@xlsd_register_column('uid', ColumnAlignment.LEFT)
def _xlsd_column_uid(direntry: os.DirEntry) -> str:
def _xlsd_column_uid(direntry: PathEntry) -> str:
"""
Show the owner (user) of the file.
"""
Expand All @@ -553,7 +553,7 @@ def _xlsd_column_uid(direntry: os.DirEntry) -> str:


@xlsd_register_column('gid', ColumnAlignment.LEFT)
def _xlsd_column_gid(direntry: os.DirEntry) -> str:
def _xlsd_column_gid(direntry: PathEntry) -> str:
"""
Show the group that owns the file.
"""
Expand All @@ -562,23 +562,23 @@ def _xlsd_column_gid(direntry: os.DirEntry) -> str:


@xlsd_register_column('size', ColumnAlignment.RIGHT)
def _xlsd_column_size(direntry: os.DirEntry) -> str:
def _xlsd_column_size(direntry: PathEntry) -> str:
"""
Format the size of a file.
"""
return _format_size(direntry.stat().st_size)


@xlsd_register_column('mtime', ColumnAlignment.LEFT)
def _xlsd_column_mtime(direntry: os.DirEntry) -> str:
def _xlsd_column_mtime(direntry: PathEntry) -> str:
"""
Format the last modification date for a direntry.
"""
return time.strftime("%x %X", time.gmtime(direntry.stat().st_mtime))


@xlsd_register_column('name', ColumnAlignment.LEFT)
def _xlsd_column_name(direntry: os.DirEntry) -> str:
def _xlsd_column_name(direntry: PathEntry) -> str:
"""
Simply format the filename of the direntry.
"""
Expand Down

0 comments on commit 0bdc5d5

Please sign in to comment.