Class: TTY::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/atk/set_command.rb,
lib/atk/console.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#verboseObject

Returns the value of attribute verbose.



21
22
23
# File 'lib/atk/console.rb', line 21

def verbose
  @verbose
end

Instance Method Details

#_save_argsObject



22
23
24
25
26
27
28
29
# File 'lib/atk/console.rb', line 22

def _save_args
    if @args == nil
        @args = []
        for each in ARGV
            @args << each
        end
    end
end

#argsObject



31
32
33
34
# File 'lib/atk/console.rb', line 31

def args
    self._save_args()
    return @args
end

#as_shell_argument(argument) ⇒ Object



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
101
102
103
104
105
106
107
108
109
# File 'lib/atk/console.rb', line 57

def as_shell_argument(argument)
    argument = argument.to_s
    if OS.is?(:unix)
        # use single quotes to perfectly escape any string
        return " '"+argument.gsub(/'/, "'\"'\"'")+"'"
    else
        # *sigh* Windows
        # this problem is unsovleable
        # see: https://superuser.com/questions/182454/using-backslash-to-escape-characters-in-cmd-exe-runas-command-as-example
        #       "The fact is, there's nothing that will escape " within quotes for argument passing. 
        #        You can brood over this for a couple of years and arrive at no solution. 
        #        This is just some of the inherent limitations of cmd scripting.
        #        However, the good news is that you'll most likely never come across a situation whereby you need to do so.
        #        Sure, there's no way to get echo """ & echo 1 to work, but that's not such a big deal because it's simply
        #        a contrived problem which you'll likely never encounter.
        #        For example, consider runas. It works fine without needing to escape " within quotes
        #        because runas knew that there's no way to do so and made internal adjustments to work around it.
        #        runas invented its own parsing rules (runas /flag "anything even including quotes") and does not
        #        interpret cmd arguments the usual way.
        #        Official documentation for these special syntax is pretty sparse (or non-existent).
        #        Aside from /? and help, it's mostly trial-and-error."
        # 
        
        
        # according to Microsoft see: https://docs.microsoft.com/en-us/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way
        # the best possible (but still broken) implementation is to quote things 
        # in accordance with their default C++ argument parser
        # so thats what this function does
        
        # users are going to have to manually escape things like ^, !, % etc depending on the context they're used in
        
        simple_char = "[a-zA-Z0-9_.,;`=-*?\\/\\[\\]]"
        
        # if its a simple argument just pass it on
        if argument =~ /\A(#{simple_char})*\z/
            return " #{argument}"
        # if it is complicated, then quote it and escape quotes
        else
            # find any backslashes that come before a double quote or the ending of the argument
            # then double the number of slashes
            escaped = argument.gsub(/(?<slashes>\/+)(?="|\z)/) do |each_match|
                "\/" * (each_match['slashes'].size * 2)
            end
            
            # then find all the double quotes and escape them
            escaped.gsub!(/"/, '\\"')
            
            # all of the remaining escapes are up to Windows user's/devs

            return " \"#{escaped}\""
        end
    end
end

#command_sourcesObject

returns the locations where commands are stored from highest to lowest priority



119
120
121
122
123
124
125
# File 'lib/atk/console.rb', line 119

def command_sources()
    if OS.is?('unix')
        return ENV['PATH'].split(':')
    else
        return ENV['PATH'].split(';')
    end
end

#has_command(name_of_executable) ⇒ Object Also known as: has_command?



52
53
54
# File 'lib/atk/console.rb', line 52

def has_command(name_of_executable)
    return OS.has_command(name_of_executable)
end

#make_arguments_appendable(arguments) ⇒ Object



111
112
113
114
115
116
# File 'lib/atk/console.rb', line 111

def make_arguments_appendable(arguments)
    safe_arguments = arguments.map do |each|
        Console.as_shell_argument(each)
    end
    return safe_arguments.join('')
end

#path_for(name_of_executable) ⇒ Object



48
49
50
# File 'lib/atk/console.rb', line 48

def path_for(name_of_executable)
    return OS.path_for_executable(name_of_executable)
end

#require_superuserObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/atk/console.rb', line 127

def require_superuser()
    if OS.is?('unix')
        system("sudo echo 'permissions acquired'")
    else
        # check if already admin
        # $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
        # $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
        # FUTURE: add a check here and raise an error if not admin
        puts "(in the future this will be an automatic check)"
        puts "(if you're unsure, then the answer is probably no)"
        if Console.yes?("Are you running this \"as an Administrator\"?\n(caution: incorrectly saying 'yes' can cause broken systems)")
            puts "assuming permissions are acquired"
        else
            puts "                \n                You'll need to \n                - close the current program\n                - reopen it \"as Administrator\"\n                - redo whatever steps you did to get here\n                \n            HEREDOC\n            Console.keypress(\"Press enter to end the current process\", keys: [:return])\n            exit\n        end\n    end\nend\n".remove_indent

#set_command(name, code) ⇒ Object



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
# File 'lib/atk/set_command.rb', line 12

def set_command(name, code)
    if OS.is?("unix")
        exec_path = "#{Atk.paths[:commands]}/#{name}"
        local_place = Atk.temp_path(name)
        # add the hash bang
        hash_bang = "#!#{Atk.paths[:ruby]}\n"
        # create the file
        FS.write(hash_bang+code, to: local_place)
        # copy to command folder
        system("sudo", "cp", local_place, exec_path)
        system("sudo", "chmod", "ugo+x", exec_path)
    elsif OS.is?("windows")
        # check for invalid file paths, see https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
        if name =~ /[><:"\/\\|?*]/
            raise "                \n                \n                When using the ATK Console.set_command(name)\n                The name: \#{name}\n                is not a valid file path on windows\n                which means it cannot be a command\n            HEREDOC\n        end\n        exec_path = \"\#{Atk.paths[:commands]}\\\\\#{name}\"\n        \n        # create the code\n        IO.write(exec_path+\".rb\", code)\n        # create an executable to call the code\n        IO.write(exec_path+\".bat\", \"@echo off\\nruby \\\"\#{exec_path}.rb\\\" %*\")\n    end\nend\n".remove_indent

#stdinObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/atk/console.rb', line 36

def stdin
    # save arguments before clearing them
    self._save_args()
    # must clear arguments in order to get stdin
    ARGV.clear
    # check if there is a stdin
    if !(STDIN.tty?)
        @stdin = $stdin.read
    end
    return @stdin
end