Module: Sergeant::Utils
- Included in:
- SergeantApp
- Defined in:
- lib/sergeant/utils.rb
Constant Summary collapse
- ARCHIVE_EXTENSIONS =
%w[.zip .tar .gz .tgz .bz2 .tbz2 .xz .txz .7z .rar .zst .lz .lzma .cab .iso .deb .rpm .apk .jar .war].freeze
- MEDIA_EXTENSIONS =
%w[.png .jpg .jpeg .gif .bmp .svg .webp .ico .tiff .tif .heic .avif .mp4 .mkv .avi .mov .wmv .flv .webm .m4v .mpg .mpeg .mp3 .wav .flac .ogg .aac .m4a .wma .opus].freeze
- CODE_EXTENSIONS =
%w[.rb .py .js .jsx .mjs .cjs .ts .tsx .go .rs .c .cpp .cc .h .hpp .java .kt .kts .swift .php .sh .bash .zsh .fish .lua .pl .pm .r .scala .clj .cljs .ex .exs .erl .hs .ml .mli .sql .html .htm .css .scss .sass .less .json .yaml .yml .toml .xml .vue .svelte].freeze
Instance Method Summary collapse
-
#file_category(item) ⇒ Object
Which color pair (see Sergeant#apply_color_theme) to use for a file, based on its extension, falling back to the executable bit.
- #format_date(time) ⇒ Object
- #format_permissions(mode, is_directory) ⇒ Object
- #format_size(bytes) ⇒ Object
- #fzf_available? ⇒ Boolean
- #get_git_branch ⇒ Object
- #get_owner_info(stat) ⇒ Object
- #glow_available? ⇒ Boolean
- #nano_available? ⇒ Boolean
- #nvim_available? ⇒ Boolean
- #text_file?(file_path) ⇒ Boolean
- #vi_available? ⇒ Boolean
- #vim_available? ⇒ Boolean
Instance Method Details
#file_category(item) ⇒ Object
Which color pair (see Sergeant#apply_color_theme) to use for a file, based on its extension, falling back to the executable bit. Directories are colored separately by draw_screen and never reach this method.
19 20 21 22 23 24 25 26 27 |
# File 'lib/sergeant/utils.rb', line 19 def file_category(item) ext = File.extname(item[:name]).downcase return :archive if ARCHIVE_EXTENSIONS.include?(ext) return :media if MEDIA_EXTENSIONS.include?(ext) return :code if CODE_EXTENSIONS.include?(ext) return :executable if item[:mode] && (item[:mode] & 0o111).nonzero? && ext.empty? :file end |
#format_date(time) ⇒ Object
96 97 98 99 100 |
# File 'lib/sergeant/utils.rb', line 96 def format_date(time) return '' if time.nil? time.strftime('%b %d %H:%M') end |
#format_permissions(mode, is_directory) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/sergeant/utils.rb', line 61 def (mode, is_directory) type = is_directory ? 'd' : '-' owner = '' owner += mode.anybits?(0o400) ? 'r' : '-' owner += mode.anybits?(0o200) ? 'w' : '-' owner += mode.anybits?(0o100) ? 'x' : '-' group = '' group += mode.anybits?(0o040) ? 'r' : '-' group += mode.anybits?(0o020) ? 'w' : '-' group += mode.anybits?(0o010) ? 'x' : '-' other = '' other += mode.anybits?(0o004) ? 'r' : '-' other += mode.anybits?(0o002) ? 'w' : '-' other += mode.anybits?(0o001) ? 'x' : '-' "#{type}#{owner}#{group}#{other}" end |
#format_size(bytes) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sergeant/utils.rb', line 82 def format_size(bytes) return ' ' if bytes.nil? if bytes < 1024 "#{bytes}B".rjust(7) elsif bytes < 1024 * 1024 "#{(bytes / 1024.0).round(1)}K".rjust(7) elsif bytes < 1024 * 1024 * 1024 "#{(bytes / (1024.0 * 1024)).round(1)}M".rjust(7) else "#{(bytes / (1024.0 * 1024 * 1024)).round(1)}G".rjust(7) end end |
#fzf_available? ⇒ Boolean
102 103 104 |
# File 'lib/sergeant/utils.rb', line 102 def fzf_available? system('command -v fzf > /dev/null 2>&1') end |
#get_git_branch ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sergeant/utils.rb', line 29 def get_git_branch return nil unless Dir.exist?(File.join(@current_dir, '.git')) head_file = File.join(@current_dir, '.git', 'HEAD') return nil unless File.exist?(head_file) head_content = File.read(head_file).strip if head_content.start_with?('ref: refs/heads/') head_content.sub('ref: refs/heads/', '') else head_content[0..7] end rescue StandardError nil end |
#get_owner_info(stat) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sergeant/utils.rb', line 45 def get_owner_info(stat) begin user = Etc.getpwuid(stat.uid).name rescue StandardError user = stat.uid.to_s end begin group = Etc.getgrgid(stat.gid).name rescue StandardError group = stat.gid.to_s end "#{user}:#{group}" end |
#glow_available? ⇒ Boolean
106 107 108 |
# File 'lib/sergeant/utils.rb', line 106 def glow_available? system('command -v glow > /dev/null 2>&1') end |
#nano_available? ⇒ Boolean
122 123 124 |
# File 'lib/sergeant/utils.rb', line 122 def nano_available? system('command -v nano > /dev/null 2>&1') end |
#nvim_available? ⇒ Boolean
110 111 112 |
# File 'lib/sergeant/utils.rb', line 110 def nvim_available? system('command -v nvim > /dev/null 2>&1') end |
#text_file?(file_path) ⇒ Boolean
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/sergeant/utils.rb', line 126 def text_file?(file_path) return false unless File.file?(file_path) return false unless File.readable?(file_path) # Check file size - limit to 50MB for safety return false if File.size(file_path) > 50 * 1024 * 1024 # Check if it's a binary file by reading first 8KB File.open(file_path, 'rb') do |f| sample = f.read(8192) || '' # Check for null bytes (common in binary files) !sample.bytes.include?(0) end rescue StandardError false end |
#vi_available? ⇒ Boolean
118 119 120 |
# File 'lib/sergeant/utils.rb', line 118 def vi_available? system('command -v vi > /dev/null 2>&1') end |
#vim_available? ⇒ Boolean
114 115 116 |
# File 'lib/sergeant/utils.rb', line 114 def vim_available? system('command -v vim > /dev/null 2>&1') end |