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.



153
154
155
156
157
158
# File 'lib/filecon.rb', line 153

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/filecon.rb', line 113

def build str, id
  cmd = ""
  while str[/\%(name|dir|file|str)/]
    @user.draw( cmd += $` )
    case $&
      when '%name'; cmd += ?"+( 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



55
56
57
58
59
# File 'lib/filecon.rb', line 55

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

#cmd_historyObject



149
150
151
# File 'lib/filecon.rb', line 149

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

#flyObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/filecon.rb', line 160

def fly
  loop do
    draw
    draw_hints :rows
    key = Typr.read :key
    @user.reset
    case key
      when KEY_ESCAPE; exit
      else
        id = send( key )
        open_file id, true if id.is_a? Integer
    end
  end
end

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/filecon.rb', line 81

def open_file id=nil, default=false
  return unless id ||= @selected[:rows].empty? ?
    @user.ask( open:[self, :row] ) : @selected[:rows]
  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_sym) ).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.start_with? ?|
    fork { io = run( cmd, true )
      until io.eof?
        @user.draw io.readline.strip.scrub
        sleep 0.1
      end
    }
  else run( cmd, cmd[0..2] != '!cd' ) end
  draw
end

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/filecon.rb', line 131

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 Filecon.cmd_history_path, @cmd_history[0..Filecon.config["max_history"]].join($/)
  end
  return io
end

#switch_to(id = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/filecon.rb', line 61

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