Class: Filecon::App

Inherits:
Typr::Browser
  • Object
show all
Includes:
Typr
Defined in:
lib/filecon.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ App

Returns a new instance of App.



132
133
134
135
136
137
138
# File 'lib/filecon.rb', line 132

def initialize args={}
  super
  @user.bindings.insert 1, "[o]pen"
  @user.bindings.insert 7, "[c]ommands"
  @user.draw
  
end

Instance Method Details

#build(str, id) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/filecon.rb', line 80

def build str, id
  cmd = ""
  while str[/\%(name|dir|file|str)/]
    cmd += $`
    case $&
      when '%name'; cmd += ?"+( id.nil? ? @directory :
        id.is_a?( Array ) ? id.map{ |id|
        @directory + @data[id][NAME] }.join('" "') :
        @directory + @data[id][NAME] )+?"
      when '%dir';  cmd += ?"+(pick :directory)+?"
      when '%file'; cmd += ?"+(pick :file)+?"
      when '%str';  cmd += Typr.read :line
    end
    str = str[($`+$&).size..-1]
  end
  cmd += $' || str
  return cmd
end

#cd(dir, append = false) ⇒ Object



19
20
21
22
23
# File 'lib/filecon.rb', line 19

def cd dir, append=false; super
  File.write @dir_history_path,
  	@dir_history[0..@max_history].join($/)
  return
end

#cmd_historyObject



117
118
119
# File 'lib/filecon.rb', line 117

def cmd_history; run popup( { input: @cmd_history, align:[:left], left:left,
  colors: { columns: [ @colors[:cmd_history] ] } } ).pick(:row), true
end

#flyObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/filecon.rb', line 140

def fly
  loop do
    draw
    draw_hints :rows
    key = Typr.read :key
    next unless key
    case key
      when KEY_ESCAPE; exit
      when ?`; open_file nil, false, ["directory", "inode"] - [??] + ["default"]
      else
        id = send( key )
        open_file id, true if id.is_a? Integer
        @user.reset
    end
  end
end

#helpObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/filecon.rb', line 121

def help
  info = "\nFILECON - a terminal file manager\n\n" \
    "HINTS: press a number (1-9,0) to open that file with its default action.\n" \
    "       TAB cycles pages of hints.\n\n" \
    "CONFIG: ~/.config/filecon/config.yaml\n\n" \
    "KEYBINDINGS:\n\n" + @user.bindings.join(?\n)
  Text.new( input: info, top:2, left:3, right: -3, bottom: -3, header: "HELP",
    colors: { header: [:black,:white], border:[:white,:grey10] },
    border: :light ).pick :none
end

#open_file(id = nil, default = false, types = nil) ⇒ Object



45
46
47
48
49
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
# File 'lib/filecon.rb', line 45

def open_file id=nil, default=false, types=nil
  unless id || types
    return unless id ||= @selected[:rows].empty? ?
      @user.ask( open:[self, :row] ) : @selected[:rows]
  end
  types ||= if id.is_a? Array
    eval id.map{ |id| @data[id].values_at(SUBTYPE,TYPE).to_s }.join(?&)
  else @data[id].values_at(SUBTYPE, TYPE)
  end - [??] + ["default"]

  commands = @commands.values_at( *types ).map(&:to_a).flatten 1
  cmd = if default; commands.first.last
  else move left+1,top+1
    if id.is_a?(Array); show "#{id.count.to_s} files: (#{ 
      types.join ?, })".ljust width
    else print id end
    return unless cmd = Grid.new( left:1, top:3, right: -1,
    input:commands, border: :light, format: [:min, :max],
    borders: { top: nil }, alternate: false,
    colors: {columns: [:green, :red] }  ).pick( :row )
    commands[ cmd ].last
  end
  cmd = build cmd, id
  if cmd[0] == ?|
    display = cmd[1..-1]
    io = run( cmd, true )
    output = io.read.to_s.scrub.strip
    @user.draw "> #{display} : #{output.lines.last}"
    Typr.read :key
  else
    run( cmd, cmd[0..2] != '!cd' )
    draw
  end
end

#run(cmd = nil, store = false) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/filecon.rb', line 99

def run cmd=nil, store=false
  return unless cmd
  cmd = @cmd_history[cmd] if cmd.is_a? Integer
  case cmd[0]
    when ?! then io = StringIO.new eval( cmd[1..-1] ).to_s
    when ?| then io = open( cmd + " 2>&1" )
    when ?+ then io = open( ?| + @term +' '+ cmd[1..-1] + " 2>&1" )
    when ?= then Text.new(top: 2, right:-1, bottom:-2, border: :light,
      input:open( ?|+cmd[1..-1]+" 2>&1" ) ).pick :none
  end
  if store
    @cmd_history.unshift cmd
    @cmd_history.uniq!
    File.write @cmd_history_path, @cmd_history[0..@max_history].join($/)
  end
  return io
end

#switch_to(id = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/filecon.rb', line 25

def switch_to id=nil
  return unless id
  id = @views.keys[id] if id.is_a? Integer
  view = @views[id]
  return super if view.is_a? Array
  data = @data.map.with_index{ |row, id| row + view.values.map{ |cmd|
    next unless cmd.is_a?(String)
    built = build cmd, id
    io = built[0] == ?! ?
      StringIO.new( eval( built[1..-1] ).to_s ) :
      open( ?| + built + " 2>&1" )
    io.read(width).to_s.scrub.gsub(/[\n\t]/, ?|)
  }.compact }
  @sequence = [ NAME ] + view.keys.map{ |head|
    @header.index(head.to_s) || (@header += [head.to_s]; @header.count-1) }
  clear
  self << data
  @current = id
end