Module: Filechooser

Defined in:
lib/filechooser/api.rb,
lib/filechooser.rb,
lib/filechooser/string-class.rb,
lib/filechooser/highline-module.rb

Overview

require ‘highline’

Defined Under Namespace

Modules: Highline

Constant Summary collapse

STARTFOLDER =
File.expand_path("~")
NO_CHOICE =
"||Abort||"

Class Method Summary collapse

Class Method Details

.dirchooser(startfolder = STARTFOLDER) ⇒ Object

return the directory chosen by the user



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/filechooser/api.rb', line 5

def dirchooser(startfolder=STARTFOLDER) # return the directory chosen by the user
  p0 = Pathname.new(startfolder)
    while true
      case get_answer(p0)
        when /[cC]/ then return p0.to_s
        when /[xX]/ then return ''
        when /[lL]/ then Dir.chdir(p0) do
                           system("ls -d */ ")  # list directories only
                         end
        when /[dD]/ then Highline.cli.say("Subdirectories of '#{p0}':".style(":info"))
                         uvz=choose_uvz(p0) 
                         if uvz==''
                           Highline.cli.say("No subdirectories found.".style(":warning"))
                         else
                           uvz=File.expand_path(uvz,p0)
                           uvz=Pathname.new(uvz)
                           p0=uvz
                         end
        when /[nN]/ then name=Highline.cli.ask("Name of new directory? ".style(":ask_line"))
                         if name.verboten? 
                           Highline.cli.say("Name contains deprecated characters. Aborted.".style(":warning"))
                         else
                           begin
                             Dir.chdir(p0) do
                               if File.directory?(name)
                                 Highline.cli.say("Directory already exists.".style(":warning"))
                               else
                                 Dir.mkdir(name) 
                               end
                             end
                             uvz=File.expand_path(name,p0)
                             uvz=Pathname.new(uvz)
                             p0=uvz
                           rescue
                             Highline.cli.say("Error: Could not create directory.".style(":warning"))
                           end
                         end
        when /[uU]/ then p0=p0.parent
      end
    end
  
end

.filechooser(startfolder = STARTFOLDER) ⇒ Object

return the file chosen by the user



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/filechooser/api.rb', line 49

def filechooser(startfolder=STARTFOLDER)  # return the file chosen by the user
    p0 = Pathname.new(startfolder)
    while true
      case get_answer(p0)
        when /[cC]/ then  Highline.cli.say("Files of #{p0}:".style(":info"))
                          datei=choose_datei(p0) 
                          if datei==''
                            Highline.cli.say("No files found.".style(":warning"))
                          elsif datei==NO_CHOICE
                            # Do nothing
                          else
                            datei=File.expand_path(datei,p0)
                            datei=Pathname.new(datei)
                            return datei.to_s
                          end
        when /[xX]/ then return ''
        when /[lL]/ then system("ls -p '#{p0}'") # List directories with a trailing forward-slash
        when /[dD]/ then Highline.cli.say("Subdirectories of '#{p0}':".style(":info"))
                         uvz=choose_uvz(p0) 
                         if uvz==''
                           Highline.cli.say("No subdirectories found.".style(":warning"))
                         else
                           uvz=File.expand_path(uvz,p0)
                           uvz=Pathname.new(uvz)
                           p0=uvz
                         end
        when /[nN]/ then name=Highline.cli.ask("Name of new file? ".style(":ask_line"))
                         if name.verboten? 
                           Highline.cli.say("Name contains deprecated characters. Aborted.".style(":warning"))
                         else
                           begin
                             Dir.chdir(p0) do
                               if File.file?(name)
                                 Highline.cli.say("File already exists.".style(":warning"))
                               else
                                 if permission_given("Create and choose file '#{name}'")
                                   File.open(name,"a") 
                                   datei=File.expand_path(name,p0)
                                   datei=Pathname.new(datei)
                                   return datei
                                 end
                               end
                             end
                           rescue
                             Highline.cli.say("Error: Could not create file.".style(":warning"))
                           end
                         end
        when /[uU]/ then p0=p0.parent
      end
    end
  
end