Class: TTY::Prompt

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

Overview

Console

see github.com/piotrmurach/tty-prompt TODO: look into piotrmurach.github.io/tty/ to animate the terminal TODO: look at github.com/pazdera/catpix to add an ATK logo in the terminal

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#verboseObject

Returns the value of attribute verbose.



26
27
28
# File 'lib/atk/console.rb', line 26

def verbose
  @verbose
end

Instance Method Details

#_save_argsObject



27
28
29
30
31
32
33
34
# File 'lib/atk/console.rb', line 27

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

#argsObject



36
37
38
39
# File 'lib/atk/console.rb', line 36

def args
    self._save_args()
    return @args
end

#has_command(name_of_executable) ⇒ Object



57
58
59
# File 'lib/atk/console.rb', line 57

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

#make_arguments_appendable(arguments) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/atk/console.rb', line 65

def make_arguments_appendable(arguments)
    # TODO: make sure this works on windows
    safe_arguments = arguments.map do |each|
        " '"+Console.single_quote_escape(each)+"'"
    end
    return safe_arguments.join('')
end

#path_for(name_of_executable) ⇒ Object



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

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

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

def set_command(name, code)
    if OS.is?("unix")
        exec_path = "/usr/local/bin/#{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 =~ /[><:"\/\\|?*]/
            puts "Sorry #{name} isn't a valid file path on windows"
            return ""
        end
        username = FS.username
        exec_path = "C:\\Users\\#{username}\\AppData\\local\\Microsoft\\WindowsApps\\#{name}"
        
        # create the code
        IO.write(exec_path+".rb", code)
        # create an executable to call the code
        IO.write(exec_path+".bat", "@echo off\nruby \"#{exec_path}.rb\" %*")
    end
end

#single_quote_escape(string) ⇒ Object



61
62
63
# File 'lib/atk/console.rb', line 61

def single_quote_escape(string)
    string.gsub(/'/, "'\"'\"'")
end

#stdinObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/atk/console.rb', line 41

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