Files
.config/ranger/plugins/ranger_devicons/devicons.py
2025-09-08 10:44:09 +02:00

421 lines
12 KiB
Python

#!/usr/bin/env python3
# coding=UTF-8
"""Mappings and helpers for displaying developer icons in ranger."""
# These glyphs, and the mapping of file extensions to glyphs
# has been copied from the vimscript code that is present in
# https://github.com/ryanoasis/vim-devicons
import os
import importlib
import locale
# Get the XDG_USER_DIRS directory names from environment variables
xdgs_dirs = {
os.path.basename(os.getenv(key).rstrip('/')): icon
for key, icon in (
('XDG_DOCUMENTS_DIR', ''),
('XDG_DOWNLOAD_DIR', ''),
('XDG_CONFIG_DIR', ''),
('XDG_MUSIC_DIR', ''),
('XDG_PICTURES_DIR', ''),
('XDG_PUBLICSHARE_DIR', ''),
('XDG_TEMPLATES_DIR', ''),
('XDG_VIDEOS_DIR', ''),
)
if os.getenv(key)
}
# all those glyphs will show as weird squares if you don't have the correct patched font
# My advice is to use NerdFonts which can be found here:
# https://github.com/ryanoasis/nerd-fonts
file_node_extensions = {
'7z' : '',
'a' : '',
'ai' : '',
'apk' : '',
'asm' : '',
'asp' : '',
'aup' : '',
'avi' : '',
'awk' : '',
'bash' : '',
'bat' : '',
'bmp' : '',
'bz2' : '',
'c' : '',
'c++' : '',
'cab' : '',
'cbr' : '',
'cbz' : '',
'cc' : '',
'class' : '',
'clj' : '',
'cljc' : '',
'cljs' : '',
'cmake' : '',
'coffee' : '',
'conf' : '',
'cp' : '',
'cpio' : '',
'cpp' : '',
'cs' : '󰌛',
'csh' : '',
'css' : '',
'cue' : '',
'cvs' : '',
'cxx' : '',
'd' : '',
'dart' : '',
'db' : '',
'deb' : '',
'diff' : '',
'dll' : '',
'wps' : '',
'wpt' : '',
'doc' : '',
'docx' : '',
'docm' : '',
'dotx' : '',
'dotm' : '',
'dump' : '',
'edn' : '',
'eex' : '',
'efi' : '',
'ejs' : '',
'elf' : '',
'elm' : '',
'epub' : '',
'erl' : '',
'ex' : '',
'exe' : '',
'exs' : '',
'f#' : '',
'fifo' : '󰟥',
'fish' : '',
'flac' : '',
'flv' : '',
'fs' : '',
'fsi' : '',
'fsscript' : '',
'fsx' : '',
'gem' : '',
'gemspec' : '',
'gif' : '',
'go' : '',
'gz' : '',
'gzip' : '',
'h' : '',
'haml' : '',
'hbs' : '',
'hh' : '',
'hpp' : '',
'hrl' : '',
'hs' : '',
'htaccess' : '',
'htm' : '',
'html' : '',
'htpasswd' : '',
'hxx' : '',
'ico' : '',
'img' : '',
'ini' : '',
'ipynb' : '',
'iso' : '',
'jar' : '',
'java' : '',
'jl' : '',
'jpeg' : '',
'jpg' : '',
'js' : '',
'json' : '',
'jsonc' : '',
'jsx' : '',
'key' : '',
'ksh' : '',
'leex' : '',
'less' : '',
'lha' : '',
'lhs' : '',
'log' : '',
'lua' : '',
'lz' : '',
'lzh' : '',
'lzma' : '',
'm4a' : '',
'm4v' : '',
'markdown' : '',
'md' : '',
'mdx' : '',
'mjs' : '',
'mka' : '',
'mkv' : '',
'ml' : 'λ',
'mli' : 'λ',
'mov' : '',
'mp3' : '',
'mp4' : '',
'mpeg' : '',
'mpg' : '',
'msi' : '',
'mustache' : '',
'nix' : '',
'o' : '',
'ogg' : '',
'opus' : '',
'part' : '',
'pdf' : '',
'php' : '',
'pl' : '',
'pm' : '',
'png' : '',
'pp' : '',
'dps' : '',
'dpt' : '',
'ppt' : '',
'pptx' : '',
'pptm' : '',
'pot' : '',
'potx' : '',
'potm' : '',
'pps' : '',
'ppsx' : '',
'ppsm' : '',
'ps1' : '',
'psb' : '',
'psd' : '',
'pub' : '',
'py' : '',
'pyc' : '',
'pyd' : '',
'pyo' : '',
'r' : '󰟔',
'rake' : '',
'rar' : '',
'rb' : '',
'rc' : '',
'rlib' : '',
'rmd' : '',
'rom' : '',
'rpm' : '',
'rproj' : '󰗆',
'rs' : '',
'rss' : '',
'rtf' : '',
's' : '',
'sass' : '',
'scala' : '',
'scss' : '',
'sh' : '',
'slim' : '',
'sln' : '',
'so' : '',
'sql' : '',
'styl' : '',
'suo' : '',
'svelte' : '',
'swift' : '',
't' : '',
'tar' : '',
'tex' : '󰙩',
'tgz' : '',
'toml' : '',
'torrent' : '',
'ts' : '',
'tsx' : '',
'twig' : '',
'vim' : '',
'vimrc' : '',
'vue' : '󰡄',
'wav' : '',
'webm' : '',
'webmanifest' : '',
'webp' : '',
'xbps' : '',
'xcplayground' : '',
'xhtml' : '',
'et' : '󰈛',
'ett' : '󰈛',
'xls' : '󰈛',
'xlt' : '󰈛',
'xlsx' : '󰈛',
'xlsm' : '󰈛',
'xlsb' : '󰈛',
'xltx' : '󰈛',
'xltm' : '󰈛',
'xla' : '󰈛',
'xlam' : '󰈛',
'xml' : '',
'xul' : '',
'xz' : '',
'yaml' : '',
'yml' : '',
'zip' : '',
'zsh' : '',
}
# Base mapping for English directory names
dir_node_exact_matches_base = {
'.git' : '',
'Desktop' : '',
'Documents' : '',
'Downloads' : '',
'Dotfiles' : '',
'Dropbox' : '',
'Music' : '',
'Pictures' : '',
'Public' : '',
'Templates' : '',
'Videos' : '',
'anaconda3' : '',
'go' : '',
'workspace' : '',
'OneDrive' : '',
}
# Mapping of localized directory names to their English counterparts.
# Languages are loaded from separate modules in :mod:`ranger_devicons.locales`.
dir_name_translations = {}
def load_translations(lang=None):
"""Load directory name translations for the given language."""
if lang is None:
lang = os.getenv('DEVICONS_LANG')
if not lang:
loc = locale.getdefaultlocale()[0]
if loc:
lang = loc.split('_')[0]
if not lang:
return {}
try:
module = importlib.import_module(f'ranger_devicons.locales.{lang}')
return getattr(module, 'translations', {})
except ModuleNotFoundError:
return {}
# Populate translations for the current locale
dir_name_translations.update(load_translations())
# Working mapping used by the plugin
dir_node_exact_matches = dict(dir_node_exact_matches_base)
def translate_dir_name(name):
"""Translate localized directory names to English."""
return dir_name_translations.get(name, name)
# Python 2.x-3.4 don't support unpacking syntex `{**dict}`
# XDG_USER_DIRS
dir_node_exact_matches.update(xdgs_dirs)
file_node_exact_matches = {
'.bash_aliases' : '',
'.bash_history' : '',
'.bash_logout' : '',
'.bash_profile' : '',
'.bashprofile' : '',
'.bashrc' : '',
'.dmrc' : '',
'.DS_Store' : '',
'.fasd' : '',
'.fehbg' : '',
'.gitattributes' : '',
'.gitconfig' : '',
'.gitignore' : '',
'.gitlab-ci.yml' : '',
'.gvimrc' : '',
'.inputrc' : '',
'.jack-settings' : '',
'.mime.types' : '',
'.ncmpcpp' : '',
'.nvidia-settings-rc' : '',
'.pam_environment' : '',
'.profile' : '',
'.recently-used' : '',
'.selected_editor' : '',
'.vim' : '',
'.viminfo' : '',
'.vimrc' : '',
'.Xauthority' : '',
'.Xdefaults' : '',
'.xinitrc' : '',
'.xinputrc' : '',
'.Xresources' : '',
'.zshrc' : '',
'_gvimrc' : '',
'_vimrc' : '',
'a.out' : '',
'authorized_keys' : '',
'bspwmrc' : '',
'cmakelists.txt' : '',
'config' : '',
'config.ac' : '',
'config.m4' : '',
'config.mk' : '',
'config.ru' : '',
'configure' : '',
'docker-compose.yml' : '',
'dockerfile' : '',
'Dockerfile' : '',
'dropbox' : '',
'exact-match-case-sensitive-1.txt' : 'X1',
'exact-match-case-sensitive-2' : 'X2',
'favicon.ico' : '',
'gemfile' : '',
'gruntfile.coffee' : '',
'gruntfile.js' : '',
'gruntfile.ls' : '',
'gulpfile.coffee' : '',
'gulpfile.js' : '',
'gulpfile.ls' : '',
'ini' : '',
'known_hosts' : '',
'ledger' : '',
'license' : '',
'LICENSE' : '',
'LICENSE.md' : '',
'LICENSE.txt' : '',
'Makefile' : '',
'makefile' : '',
'Makefile.ac' : '',
'Makefile.in' : '',
'mimeapps.list' : '',
'mix.lock' : '',
'node_modules' : '',
'package-lock.json' : '',
'package.json' : '',
'playlists' : '',
'procfile' : '',
'Rakefile' : '',
'rakefile' : '',
'react.jsx' : '',
'README' : '',
'README.markdown' : '',
'README.md' : '',
'README.rst' : '',
'README.txt' : '',
'sxhkdrc' : '',
'user-dirs.dirs' : '',
'webpack.config.js' : '',
}
def devicon(file):
"""Return the devicon for the given ranger file object."""
if file.is_directory:
dir_name = translate_dir_name(file.relative_path)
return dir_node_exact_matches.get(dir_name, '')
return file_node_exact_matches.get(
os.path.basename(file.relative_path),
file_node_extensions.get(file.extension, ''),
)