getpass 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012 Peter Dodds
4
+
5
+ Prmission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF R IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ Ruby getpass
2
+ ============
3
+
4
+ Get passwords on the command line and see `*`s.
5
+
6
+ Example
7
+ -------
8
+
9
+ require 'getpass'
10
+ include Getpass
11
+
12
+ print 'Username: '
13
+ username = gets.chomp
14
+ print 'Password: '
15
+ password = getpass
16
+
17
+ # or
18
+
19
+ password = getpass :prompt => 'Password: '
20
+
21
+ Author
22
+ ------
23
+
24
+ * [Peter Dodds](http://pddds.com)
25
+
26
+ Licence
27
+ -------
28
+
29
+ MIT License. See the included LICENCE file.
@@ -0,0 +1,64 @@
1
+ require 'rbconfig'
2
+
3
+ module Getpass
4
+ if Config::CONFIG['host_os'] =~ /mswin|mingw/
5
+ require 'Win32API'
6
+ @@_getch = Win32API.new('crtdll', '_getch', [], 'L')
7
+ @@_kbhit = Win32API.new('crtdll', '_kbhit', [], 'L')
8
+
9
+ def self.pre_get; end
10
+
11
+ def self.getch
12
+ c = @@_getch.Call
13
+ ensure
14
+ # pop the next key off if it's one escaped
15
+ @@_getch.Call if (c == 0 || c == 224) && @@_kbhit.Call == 1
16
+ end
17
+
18
+ def self.post_get; end
19
+ else
20
+ def self.pre_get
21
+ @@_sttySettings = `stty --save`
22
+ system 'stty raw -echo'
23
+ end
24
+
25
+ def self.getch
26
+ STDIN.getc
27
+ end
28
+
29
+ def self.post_get
30
+ system "stty #{@@_sttySettings}"
31
+ end
32
+ end
33
+
34
+ def getpass(opts = {})
35
+ $stdout.print opts[:prompt] unless opts[:prompt].nil?
36
+ echo = (opts[:echo] || '*').to_s
37
+ echoLen = echo.length
38
+ clear = "#{"\b" * echoLen}#{' ' * echoLen}#{"\b" * echoLen}"
39
+
40
+ Getpass.pre_get
41
+
42
+ pass = ''
43
+ loop do
44
+ char = (id = Getpass.getch).chr
45
+
46
+ raise Interrupt if id == 3 # Ctrl_c
47
+ return pass if id == 13 || id == 10 # \r + \n
48
+
49
+ # Only push visible characters
50
+ if id > 31 && id != 127 && id != 224
51
+ $stdout.print echo unless echo.empty?
52
+ pass << char
53
+ elsif id == 8 # backspace
54
+ pass.sub! /.$/, ''
55
+ $stdout.print clear unless echo.empty?
56
+ end
57
+ end
58
+ ensure
59
+ Getpass.post_get
60
+ $stdout.print "\n" unless echo.empty?
61
+ end
62
+
63
+ module_function :getpass
64
+ end
@@ -0,0 +1,30 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class GetpassTest < Test::Unit::TestCase
4
+ include Getpass
5
+
6
+ def test_return
7
+ ungetch "\r"
8
+ assert_equal '', getpass
9
+
10
+ ungetch "\n"
11
+ assert_equal '', getpass
12
+ end
13
+
14
+ def test_interupt
15
+ assert_raise Interrupt do
16
+ ungetch 3
17
+ getpass
18
+ end
19
+ end
20
+
21
+ def test_prompt
22
+ ungetch "\n"
23
+ output = get_stdout { getpass }
24
+ assert_equal "\n", output
25
+
26
+ ungetch "\n"
27
+ output = get_stdout { getpass :prompt => 'Password: ' }
28
+ assert_equal "Password: \n", output
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ require 'test/unit'
2
+ require 'stringio'
3
+ require 'getpass'
4
+
5
+
6
+ class GetpassTest < Test::Unit::TestCase
7
+ def ascii(char)
8
+ char.is_a?(Fixnum) ? char : char[0].ord
9
+ end
10
+
11
+ if Config::CONFIG['host_os'] =~ /mswin|mingw/
12
+ require "Win32API"
13
+ @@_ungetch = Win32API.new('crtdll', '_ungetch', [], 'L')
14
+
15
+ def ungetch(char)
16
+ @@_ungetch.call ascii(char)
17
+ end
18
+
19
+ else
20
+ def ungetch(char)
21
+ STDIN.ungetc ascii(char)
22
+ end
23
+ end
24
+
25
+ def get_stdout
26
+ oldStdout, $stdout = $stdout, StringIO.new
27
+
28
+ yield
29
+
30
+ return $stdout.string
31
+ ensure
32
+ $stdout = oldStdout
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: getpass
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Peter Dodds
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-27 00:00:00.000000000Z
13
+ dependencies: []
14
+ description:
15
+ email: peter@pddds.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.md
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - test/getpass_test.rb
24
+ - test/test_helper.rb
25
+ - lib/getpass.rb
26
+ homepage: https://github.com/m0tive/getpass
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options:
30
+ - --main
31
+ - README.md
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.11
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: simple command line password input
52
+ test_files: []
53
+ has_rdoc: