Class: Textbringer::DiredMode

Inherits:
Mode
  • Object
show all
Defined in:
lib/textbringer/modes/dired_mode.rb

Constant Summary collapse

PERM_BITS =
[
  ["r", 0400], ["w", 0200], ["x", 0100],
  ["r", 0040], ["w", 0020], ["x", 0010],
  ["r", 0004], ["w", 0002], ["x", 0001]
]

Constants inherited from Mode

Mode::DEFAULT_SYNTAX_TABLE

Constants included from Commands

Commands::CLIPBOARD_AVAILABLE, Commands::COMPLETION_POPUP_STATUS, Commands::CTAGS, Commands::EMAIL_REGEXP, Commands::HELP_RING, Commands::ISEARCH_STATUS, Commands::ISPELL_STATUS, Commands::ISPELL_WORD_REGEXP, Commands::KEYBOARD_MACROS, Commands::LSP_DOCUMENT_VERSIONS, Commands::LSP_STATUS, Commands::REGISTERS, Commands::RE_SEARCH_STATUS, Commands::URI_REGEXP

Constants included from Utils

Utils::COMPLETION, Utils::EXPRESSION_COMPLETOR, Utils::EXPRESSION_COMPLETOR_OPTIONS, Utils::HOOKS

Instance Attribute Summary

Attributes inherited from Mode

#buffer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Mode

define_generic_command, define_local_command, define_syntax, inherited, list, #name, #syntax_table

Methods included from Commands

[], #buffer_uri, #command_help, command_table, #completion_popup_done, completion_popup_mode_active?, #completion_popup_pre_command_hook, #completion_popup_start, #current_prefix_arg, define_command, #ensure_ispell_active, #execute_keyboard_macro, #get_tags, #insert_completion, #isearch_done, #isearch_mode, #isearch_mode?, #isearch_pre_command_hook, #isearch_prompt, #isearch_repeat, #isearch_repeat_backward, #isearch_repeat_forward, #isearch_search, #ispell_done, #ispell_forward, #ispell_mode, #keymap_bindings, list, #lsp_after_set_visited_file_name_hook, #lsp_close_signature_window, #lsp_completion_context, #lsp_find_file_hook, #lsp_open_document, #lsp_position, #lsp_setup_buffer_hooks, #lsp_show_signature_window, #lsp_signature_pre_command_hook, #lsp_text_document_sync_kind, #lsp_utf16_length, #match_beginning, #match_end, #match_string, #message_misspelled, #number_prefix_arg, #prefix_numeric_value, #read_input_method_name, #read_keyboard_macro, #read_register, #replace_match, undefine_command, #universal_argument_mode

Methods included from Utils

add_hook, background, complete_for_minibuffer, delete_completions_window, foreground, foreground!, get_hooks, message, read_buffer, read_char, read_command_name, read_encoding, read_event, read_expression, read_file_name, read_from_minibuffer, read_key_sequence, read_object, read_single_char, received_keyboard_quit?, remove_hook, ruby_install_name, run_hooks, run_hooks_in, self_insert_and_exit_minibuffer, set_transient_map, show_exception, sit_for, sleep_for, y_or_n?, yes_or_no?

Constructor Details

#initialize(buffer) ⇒ DiredMode

Returns a new instance of DiredMode.



102
103
104
105
# File 'lib/textbringer/modes/dired_mode.rb', line 102

def initialize(buffer)
  super(buffer)
  buffer.keymap = DIRED_MODE_MAP
end

Class Method Details

.format_permissions(stat) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/textbringer/modes/dired_mode.rb', line 38

def self.format_permissions(stat)
  type = if stat.directory?  then "d"
         elsif stat.symlink? then "l"
         elsif stat.pipe?    then "p"
         elsif stat.socket?  then "s"
         elsif stat.chardev? then "c"
         elsif stat.blockdev? then "b"
         else "-"
         end
  type + PERM_BITS.map { |ch, mask| stat.mode & mask != 0 ? ch : "-" }.join
end

.generate_listing(dir) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/textbringer/modes/dired_mode.rb', line 50

def self.generate_listing(dir)
  entries = []
  Dir.foreach(dir) do |name|
    path = File.join(dir, name)
    begin
      stat = File.lstat(path)
      perms = format_permissions(stat)
      size  = stat.size
      mtime = stat.mtime.strftime("%Y-%m-%d %H:%M")
      if stat.symlink?
        begin
          target = File.readlink(path)
        rescue SystemCallError
          target = "?"
        end
        display = "#{name} -> #{target}"
      elsif stat.directory?
        display = "#{name}/"
      else
        display = name
      end
      entries << {
        name: name,
        display: display,
        perms: perms,
        size: size,
        mtime: mtime,
        directory: stat.directory?
      }
    rescue SystemCallError => e
      entries << {
        name: name,
        display: name,
        perms: "??????????",
        size: 0,
        mtime: "????-??-?? ??:??",
        directory: false,
        error: e.message
      }
    end
  end

  entries.sort_by! { |e| [e[:directory] ? 0 : 1, e[:name].downcase] }

  lines = ["  #{dir}:\n"]
  entries.each do |e|
    line = "  #{e[:perms]}  #{e[:size].to_s.rjust(8)}  #{e[:mtime]}  #{e[:display]}\n"
    lines << line
  end
  lines.join
end