highline-sgonyea 1.6.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.gitignore +2 -0
  2. data/AUTHORS +3 -0
  3. data/CHANGELOG +308 -0
  4. data/COPYING +340 -0
  5. data/INSTALL +55 -0
  6. data/LICENSE +7 -0
  7. data/README.rdoc +63 -0
  8. data/Rakefile +50 -0
  9. data/TODO +6 -0
  10. data/examples/ansi_colors.rb +36 -0
  11. data/examples/asking_for_arrays.rb +16 -0
  12. data/examples/basic_usage.rb +73 -0
  13. data/examples/color_scheme.rb +30 -0
  14. data/examples/limit.rb +10 -0
  15. data/examples/menus.rb +63 -0
  16. data/examples/overwrite.rb +17 -0
  17. data/examples/page_and_wrap.rb +320 -0
  18. data/examples/password.rb +5 -0
  19. data/examples/repeat_entry.rb +19 -0
  20. data/examples/trapping_eof.rb +20 -0
  21. data/examples/using_readline.rb +15 -0
  22. data/highline.gemspec +36 -0
  23. data/lib/highline.rb +1000 -0
  24. data/lib/highline/color_scheme.rb +134 -0
  25. data/lib/highline/compatibility.rb +16 -0
  26. data/lib/highline/import.rb +41 -0
  27. data/lib/highline/menu.rb +398 -0
  28. data/lib/highline/question.rb +472 -0
  29. data/lib/highline/simulate.rb +48 -0
  30. data/lib/highline/string_extensions.rb +131 -0
  31. data/lib/highline/style.rb +181 -0
  32. data/lib/highline/system_extensions.rb +186 -0
  33. data/setup.rb +1360 -0
  34. data/site/.cvsignore +1 -0
  35. data/site/highline.css +65 -0
  36. data/site/images/logo.png +0 -0
  37. data/site/index.html +58 -0
  38. data/test/string_methods.rb +32 -0
  39. data/test/tc_color_scheme.rb +96 -0
  40. data/test/tc_highline.rb +1027 -0
  41. data/test/tc_import.rb +52 -0
  42. data/test/tc_menu.rb +427 -0
  43. data/test/tc_string_extension.rb +20 -0
  44. data/test/tc_string_highline.rb +38 -0
  45. data/test/tc_style.rb +567 -0
  46. data/test/ts_all.rb +16 -0
  47. metadata +118 -0
@@ -0,0 +1,320 @@
1
+ # page_and_wrap.rb
2
+ #
3
+ # Created by James Edward Gray II on 2005-05-07.
4
+ # Copyright 2005 Gray Productions. All rights reserved.
5
+
6
+ require "rubygems"
7
+ require "highline/import"
8
+
9
+ $terminal.wrap_at = 80
10
+ $terminal.page_at = 22
11
+
12
+ say(<<END)
13
+ THE UNITED STATES CONSTITUTION
14
+
15
+ We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.
16
+
17
+ Article. I.
18
+
19
+ Section 1.
20
+
21
+ All legislative Powers herein granted shall be vested in a Congress of the United States, which shall consist of a Senate and House of Representatives.
22
+
23
+ Section. 2.
24
+
25
+ Clause 1: The House of Representatives shall be composed of Members chosen every second Year by the People of the several States, and the Electors in each State shall have the Qualifications requisite for Electors of the most numerous Branch of the State Legislature.
26
+
27
+ Clause 2: No Person shall be a Representative who shall not have attained to the Age of twenty five Years, and been seven Years a Citizen of the United States, and who shall not, when elected, be an Inhabitant of that State in which he shall be chosen.
28
+
29
+ Clause 3: Representatives and direct Taxes shall be apportioned among the several States which may be included within this Union, according to their respective Numbers, which shall be determined by adding to the whole Number of free Persons, including those bound to Service for a Term of Years, and excluding Indians not taxed, three fifths of all other Persons. (See Note 2) The actual Enumeration shall be made within three Years after the first Meeting of the Congress of the United States, and within every subsequent Term of ten Years, in such Manner as they shall by Law direct. The Number of Representatives shall not exceed one for every thirty Thousand, but each State shall have at Least one Representative; and until such enumeration shall be made, the State of New Hampshire shall be entitled to chuse three, Massachusetts eight, Rhode-Island and Providence Plantations one, Connecticut five, New-York six, New Jersey four, Pennsylvania eight, Delaware one, Maryland six, Virginia ten, North Carolina five, South Carolina five, and Georgia three.
30
+
31
+ Clause 4: When vacancies happen in the Representation from any State, the Executive Authority thereof shall issue Writs of Election to fill such Vacancies.
32
+
33
+ Clause 5: The House of Representatives shall chuse their Speaker and other Officers; and shall have the sole Power of Impeachment.
34
+
35
+ Section. 3.
36
+
37
+ Clause 1: The Senate of the United States shall be composed of two Senators from each State, chosen by the Legislature thereof, (See Note 3) for six Years; and each Senator shall have one Vote.
38
+
39
+ Clause 2: Immediately after they shall be assembled in Consequence of the first Election, they shall be divided as equally as may be into three Classes. The Seats of the Senators of the first Class shall be vacated at the Expiration of the second Year, of the second Class at the Expiration of the fourth Year, and of the third Class at the Expiration of the sixth Year, so that one third may be chosen every second Year; and if Vacancies happen by Resignation, or otherwise, during the Recess of the Legislature of any State, the Executive thereof may make temporary Appointments until the next Meeting of the Legislature, which shall then fill such Vacancies. (See Note 4)
40
+
41
+ Clause 3: No Person shall be a Senator who shall not have attained to the Age of thirty Years, and been nine Years a Citizen of the United States, and who shall not, when elected, be an Inhabitant of that State for which he shall be chosen.
42
+
43
+ Clause 4: The Vice President of the United States shall be President of the Senate, but shall have no Vote, unless they be equally divided.
44
+
45
+ Clause 5: The Senate shall chuse their other Officers, and also a President pro tempore, in the Absence of the Vice President, or when he shall exercise the Office of President of the United States.
46
+
47
+ Clause 6: The Senate shall have the sole Power to try all Impeachments. When sitting for that Purpose, they shall be on Oath or Affirmation. When the President of the United States is tried, the Chief Justice shall preside: And no Person shall be convicted without the Concurrence of two thirds of the Members present.
48
+
49
+ Clause 7: Judgment in Cases of Impeachment shall not extend further than to removal from Office, and disqualification to hold and enjoy any Office of honor, Trust or Profit under the United States: but the Party convicted shall nevertheless be liable and subject to Indictment, Trial, Judgment and Punishment, according to Law.
50
+
51
+ Section. 4.
52
+
53
+ Clause 1: The Times, Places and Manner of holding Elections for Senators and Representatives, shall be prescribed in each State by the Legislature thereof; but the Congress may at any time by Law make or alter such Regulations, except as to the Places of chusing Senators.
54
+
55
+ Clause 2: The Congress shall assemble at least once in every Year, and such Meeting shall be on the first Monday in December, (See Note 5) unless they shall by Law appoint a different Day.
56
+
57
+ Section. 5.
58
+
59
+ Clause 1: Each House shall be the Judge of the Elections, Returns and Qualifications of its own Members, and a Majority of each shall constitute a Quorum to do Business; but a smaller Number may adjourn from day to day, and may be authorized to compel the Attendance of absent Members, in such Manner, and under such Penalties as each House may provide.
60
+
61
+ Clause 2: Each House may determine the Rules of its Proceedings, punish its Members for disorderly Behaviour, and, with the Concurrence of two thirds, expel a Member.
62
+
63
+ Clause 3: Each House shall keep a Journal of its Proceedings, and from time to time publish the same, excepting such Parts as may in their Judgment require Secrecy; and the Yeas and Nays of the Members of either House on any question shall, at the Desire of one fifth of those Present, be entered on the Journal.
64
+
65
+ Clause 4: Neither House, during the Session of Congress, shall, without the Consent of the other, adjourn for more than three days, nor to any other Place than that in which the two Houses shall be sitting.
66
+
67
+ Section. 6.
68
+
69
+ Clause 1: The Senators and Representatives shall receive a Compensation for their Services, to be ascertained by Law, and paid out of the Treasury of the United States. (See Note 6) They shall in all Cases, except Treason, Felony and Breach of the Peace, beprivileged from Arrest during their Attendance at the Session of their respective Houses, and in going to and returning from the same; and for any Speech or Debate in either House, they shall not be questioned in any other Place.
70
+
71
+ Clause 2: No Senator or Representative shall, during the Time for which he was elected, be appointed to any civil Office under the Authority of the United States, which shall have been created, or the Emoluments whereof shall have been encreased during such time; and no Person holding any Office under the United States, shall be a Member of either House during his Continuance in Office.
72
+
73
+ Section. 7.
74
+
75
+ Clause 1: All Bills for raising Revenue shall originate in the House of Representatives; but the Senate may propose or concur with Amendments as on other Bills.
76
+
77
+ Clause 2: Every Bill which shall have passed the House of Representatives and the Senate, shall, before it become a Law, be presented to the President of the United States; If he approve he shall sign it, but if not he shall return it, with his Objections to that House in which it shall have originated, who shall enter the Objections at large on their Journal, and proceed to reconsider it. If after such Reconsideration two thirds of that House shall agree to pass the Bill, it shall be sent, together with the Objections, to the other House, by which it shall likewise be reconsidered, and if approved by two thirds of that House, it shall become a Law. But in all such Cases the Votes of both Houses shall be determined by yeas and Nays, and the Names of the Persons voting for and against the Bill shall be entered on the Journal of each House respectively. If any Bill shall not be returned by the President within ten Days (Sundays excepted) after it shall have been presented to him, the Same shall be a Law, in like Manner as if he had signed it, unless the Congress by their Adjournment prevent its Return, in which Case it shall not be a Law.
78
+
79
+ Clause 3: Every Order, Resolution, or Vote to which the Concurrence of the Senate and House of Representatives may be necessary (except on a question of Adjournment) shall be presented to the President of the United States; and before the Same shall take Effect, shall be approved by him, or being disapproved by him, shall be repassed by two thirds of the Senate and House of Representatives, according to the Rules and Limitations prescribed in the Case of a Bill.
80
+
81
+ Section. 8.
82
+
83
+ Clause 1: The Congress shall have Power To lay and collect Taxes, Duties, Imposts and Excises, to pay the Debts and provide for the common Defence and general Welfare of the United States; but all Duties, Imposts and Excises shall be uniform throughout the United States;
84
+
85
+ Clause 2: To borrow Money on the credit of the United States;
86
+
87
+ Clause 3: To regulate Commerce with foreign Nations, and among the several States, and with the Indian Tribes;
88
+
89
+ Clause 4: To establish an uniform Rule of Naturalization, and uniform Laws on the subject of Bankruptcies throughout the United States;
90
+
91
+ Clause 5: To coin Money, regulate the Value thereof, and of foreign Coin, and fix the Standard of Weights and Measures;
92
+
93
+ Clause 6: To provide for the Punishment of counterfeiting the Securities and current Coin of the United States;
94
+
95
+ Clause 7: To establish Post Offices and post Roads;
96
+
97
+ Clause 8: To promote the Progress of Science and useful Arts, by securing for limited Times to Authors and Inventors the exclusive Right to their respective Writings and Discoveries;
98
+
99
+ Clause 9: To constitute Tribunals inferior to the supreme Court;
100
+
101
+ Clause 10: To define and punish Piracies and Felonies committed on the high Seas, and Offences against the Law of Nations;
102
+
103
+ Clause 11: To declare War, grant Letters of Marque and Reprisal, and make Rules concerning Captures on Land and Water;
104
+
105
+ Clause 12: To raise and support Armies, but no Appropriation of Money to that Use shall be for a longer Term than two Years;
106
+
107
+ Clause 13: To provide and maintain a Navy;
108
+
109
+ Clause 14: To make Rules for the Government and Regulation of the land and naval Forces;
110
+
111
+ Clause 15: To provide for calling forth the Militia to execute the Laws of the Union, suppress Insurrections and repel Invasions;
112
+
113
+ Clause 16: To provide for organizing, arming, and disciplining, the Militia, and for governing such Part of them as may be employed in the Service of the United States, reserving to the States respectively, the Appointment of the Officers, and the Authority of training the Militia according to the discipline prescribed by Congress;
114
+
115
+ Clause 17: To exercise exclusive Legislation in all Cases whatsoever, over such District (not exceeding ten Miles square) as may, byCession of particular States, and the Acceptance of Congress, become the Seat of the Government of the United States, and to exercise like Authority over all Places purchased by the Consent of the Legislature of the State in which the Same shall be, for the Erection of Forts, Magazines, Arsenals, dock-Yards, and other needful Buildings;--And
116
+
117
+ Clause 18: To make all Laws which shall be necessary and proper for carrying into Execution the foregoing Powers, and all other Powers vested by this Constitution in the Government of the United States, or in any Department or Officer thereof.
118
+
119
+ Section. 9.
120
+
121
+ Clause 1: The Migration or Importation of such Persons as any of the States now existing shall think proper to admit, shall not be prohibited by the Congress prior to the Year one thousand eight hundred and eight, but a Tax or duty may be imposed on such Importation, not exceeding ten dollars for each Person.
122
+
123
+ Clause 2: The Privilege of the Writ of Habeas Corpus shall not be suspended, unless when in Cases of Rebellion or Invasion the public Safety may require it.
124
+
125
+ Clause 3: No Bill of Attainder or ex post facto Law shall be passed.
126
+
127
+ Clause 4: No Capitation, or other direct, Tax shall be laid, unless in Proportion to the Census or Enumeration herein before directed to be taken. (See Note 7)
128
+
129
+ Clause 5: No Tax or Duty shall be laid on Articles exported from any State.
130
+
131
+ Clause 6: No Preference shall be given by any Regulation of Commerce or Revenue to the Ports of one State over those of another: nor shall Vessels bound to, or from, one State, be obliged to enter, clear, or pay Duties in another.
132
+
133
+ Clause 7: No Money shall be drawn from the Treasury, but in Consequence of Appropriations made by Law; and a regular Statement and Account of the Receipts and Expenditures of all public Money shall be published from time to time.
134
+
135
+ Clause 8: No Title of Nobility shall be granted by the United States: And no Person holding any Office of Profit or Trust under them, shall, without the Consent of the Congress, accept of any present, Emolument, Office, or Title, of any kind whatever, from any King, Prince, or foreign State.
136
+
137
+ Section. 10.
138
+
139
+ Clause 1: No State shall enter into any Treaty, Alliance, or Confederation; grant Letters of Marque and Reprisal; coin Money; emit Bills of Credit; make any Thing but gold and silver Coin a Tender in Payment of Debts; pass any Bill of Attainder, ex post facto Law, or Law impairing the Obligation of Contracts, or grant any Title of Nobility.
140
+
141
+ Clause 2: No State shall, without the Consent of the Congress, lay any Imposts or Duties on Imports or Exports, except what may be absolutely necessary for executing it's inspection Laws: and the net Produce of all Duties and Imposts, laid by any State on Imports or Exports, shall be for the Use of the Treasury of the United States; and all such Laws shall be subject to the Revision and Controul of the Congress.
142
+
143
+ Clause 3: No State shall, without the Consent of Congress, lay any Duty of Tonnage, keep Troops, or Ships of War in time of Peace, enter into any Agreement or Compact with another State, or with a foreign Power, or engage in War, unless actually invaded, or in such imminent Danger as will not admit of delay.
144
+
145
+ Article. II.
146
+
147
+ Section. 1.
148
+
149
+ Clause 1: The executive Power shall be vested in a President of the United States of America. He shall hold his Office during the Term of four Years, and, together with the Vice President, chosen for the same Term, be elected, as follows
150
+
151
+ Clause 2: Each State shall appoint, in such Manner as the Legislature thereof may direct, a Number of Electors, equal to the whole Number of Senators and Representatives to which the State may be entitled in the Congress: but no Senator or Representative, or Person holding an Office of Trust or Profit under the United States, shall be appointed an Elector.
152
+
153
+ Clause 3: The Electors shall meet in their respective States, and vote by Ballot for two Persons, of whom one at least shall not be an Inhabitant of the same State with themselves. And they shall make a List of all the Persons voted for, and of the Number of Votes for each; which List they shall sign and certify, and transmit sealed to the Seat of the Government of the United States, directed to the President of the Senate. The President of the Senate shall, in the Presence of the Senate and House of Representatives, open all the Certificates, and the Votes shall then be counted. The Person having the greatest Number of Votes shall be the President, if such Number be a Majority of the whole Number of Electors appointed; and if there be more than one who have such Majority, and have an equal Number of Votes, then the House of Representatives shall immediately chuse by Ballot one of them for President; and if no Person have a Majority, then from the five highest on the List the said House shall in like Manner chuse the President. But in chusing the President, the Votes shall be taken by States, the Representation from each State having one Vote; A quorum for this Purpose shall consist of a Member or Members from two thirds of the States, and a Majority of all the States shall be necessary to a Choice. In every Case, after the Choice of the President, the Person having the greatest Number of Votes of the Electors shall be the Vice President. But if there should remain two or more who have equal Votes, the Senate shall chuse from them by Ballot the Vice President. (See Note 8)
154
+
155
+ Clause 4: The Congress may determine the Time of chusing the Electors, and the Day on which they shall give their Votes; which Day shall be the same throughout the United States.
156
+
157
+ Clause 5: No Person except a natural born Citizen, or a Citizen of the United States, at the time of the Adoption of this Constitution, shall be eligible to the Office of President; neither shall any Person be eligible to that Office who shall not have attained to the Age of thirty five Years, and been fourteen Years a Resident within the United States.
158
+
159
+ Clause 6: In Case of the Removal of the President from Office, or of his Death, Resignation, or Inability to discharge the Powers and Duties of the said Office, (See Note 9) the Same shall devolve on the VicePresident, and the Congress may by Law provide for the Case of Removal, Death, Resignation or Inability, both of the President and Vice President, declaring what Officer shall then act as President, and such Officer shall act accordingly, until the Disability be removed, or a President shall be elected.
160
+
161
+ Clause 7: The President shall, at stated Times, receive for his Services, a Compensation, which shall neither be encreased nor diminished during the Period for which he shall have been elected, and he shall not receive within that Period any other Emolument from the United States, or any of them.
162
+
163
+ Clause 8: Before he enter on the Execution of his Office, he shall take the following Oath or Affirmation:--"I do solemnly swear (or affirm) that I will faithfully execute the Office of President of the United States, and will to the best of my Ability, preserve, protect and defend the Constitution of the United States."
164
+
165
+ Section. 2.
166
+
167
+ Clause 1: The President shall be Commander in Chief of the Army and Navy of the United States, and of the Militia of the several States, when called into the actual Service of the United States; he may require the Opinion, in writing, of the principal Officer in each of the executive Departments, upon any Subject relating to the Duties of their respective Offices, and he shall have Power to grant Reprieves and Pardons for Offences against the United States, except in Cases of Impeachment.
168
+
169
+ Clause 2: He shall have Power, by and with the Advice and Consent of the Senate, to make Treaties, provided two thirds of the Senators present concur; and he shall nominate, and by and with the Advice and Consent of the Senate, shall appoint Ambassadors, other public Ministers and Consuls, Judges of the supreme Court, and all other Officers of the United States, whose Appointments are not herein otherwise provided for, and which shall be established by Law: but the Congress may by Law vest the Appointment of such inferior Officers, as they think proper, in the President alone, in the Courts of Law, or in the Heads of Departments.
170
+
171
+ Clause 3: The President shall have Power to fill up all Vacancies that may happen during the Recess of the Senate, by granting Commissions which shall expire at the End of their next Session.
172
+
173
+ Section. 3.
174
+
175
+ He shall from time to time give to the Congress Information of the State of the Union, and recommend to their Consideration such Measures as he shall judge necessary and expedient; he may, on extraordinary Occasions, convene both Houses, or either of them, and in Case of Disagreement between them, with Respect to the Time of Adjournment, he may adjourn them to such Time as he shall think proper; he shall receive Ambassadors and other public Ministers; he shall take Care that the Laws be faithfully executed, and shall Commission all the Officers of the United States.
176
+
177
+ Section. 4.
178
+
179
+ The President, Vice President and all civil Officers of the United States, shall be removed from Office on Impeachment for, and Conviction of, Treason, Bribery, or other high Crimes and Misdemeanors.
180
+
181
+ Article. III.
182
+
183
+ Section. 1.
184
+
185
+ The judicial Power of the United States, shall be vested in one supreme Court, and in such inferior Courts as the Congress may from time to time ordain and establish. The Judges, both of the supreme and inferior Courts, shall hold their Offices during good Behaviour, and shall, at stated Times, receive for their Services, a Compensation, which shall not be diminished during their Continuance in Office.
186
+
187
+ Section. 2.
188
+
189
+ Clause 1: The judicial Power shall extend to all Cases, in Law and Equity, arising under this Constitution, the Laws of the United States, and Treaties made, or which shall be made, under their Authority;--to all Cases affecting Ambassadors, other public Ministers and Consuls;--to all Cases of admiralty and maritime Jurisdiction;--to Controversies to which the United States shall be a Party;--to Controversies between two or more States;--between a State and Citizens of another State; (See Note 10)--between Citizens of different States, --between Citizens of the same State claiming Lands under Grants of different States, and between a State, or the Citizens thereof, and foreign States, Citizens or Subjects.
190
+
191
+ Clause 2: In all Cases affecting Ambassadors, other public Ministers and Consuls, and those in which a State shall be Party, the supreme Court shall have original Jurisdiction. In all the other Cases before mentioned, the supreme Court shall have appellate Jurisdiction, both as to Law and Fact, with such Exceptions, and under such Regulations as the Congress shall make.
192
+
193
+ Clause 3: The Trial of all Crimes, except in Cases of Impeachment, shall be by Jury; and such Trial shall be held in the State where the said Crimes shall have been committed; but when not committed within any State, the Trial shall be at such Place or Places as the Congress may by Law have directed.
194
+
195
+ Section. 3.
196
+
197
+ Clause 1: Treason against the United States, shall consist only in levying War against them, or in adhering to their Enemies, giving them Aid and Comfort. No Person shall be convicted of Treason unless on the Testimony of two Witnesses to the same overt Act, or on Confession in open Court.
198
+
199
+ Clause 2: The Congress shall have Power to declare the Punishment of Treason, but no Attainder of Treason shall work Corruption of Blood, or Forfeiture except during the Life of the Person attainted.
200
+
201
+ Article. IV.
202
+
203
+ Section. 1.
204
+
205
+ Full Faith and Credit shall be given in each State to the public Acts, Records, and judicial Proceedings of every other State. And the Congress may by general Laws prescribe the Manner in which such Acts, Records and Proceedings shall be proved, and the Effect thereof.
206
+
207
+ Section. 2.
208
+
209
+ Clause 1: The Citizens of each State shall be entitled to all Privileges and Immunities of Citizens in the several States.
210
+
211
+ Clause 2: A Person charged in any State with Treason, Felony, or other Crime, who shall flee from Justice, and be found in another State, shall on Demand of the executive Authority of the State from which he fled, be delivered up, to be removed to the State having Jurisdiction of the Crime.
212
+
213
+ Clause 3: No Person held to Service or Labour in one State, under the Laws thereof, escaping into another, shall, in Consequence of any Law or Regulation therein, be discharged from such Service or Labour, but shall be delivered up on Claim of the Party to whom such Service or Labour may be due. (See Note 11)
214
+
215
+ Section. 3.
216
+
217
+ Clause 1: New States may be admitted by the Congress into this Union; but no new State shall be formed or erected within the Jurisdiction of any other State; nor any State be formed by the Junction of two or more States, or Parts of States, without the Consent of the Legislatures of the States concerned as well as of the Congress.
218
+
219
+ Clause 2: The Congress shall have Power to dispose of and make all needful Rules and Regulations respecting the Territory or other Property belonging to the United States; and nothing in this Constitution shall be so construed as to Prejudice any Claims of the United States, or of any particular State.
220
+
221
+ Section. 4.
222
+
223
+ The United States shall guarantee to every State in this Union a Republican Form of Government, and shall protect each of them against Invasion; and on Application of the Legislature, or of the Executive (when the Legislature cannot be convened) against domestic Violence.
224
+
225
+ Article. V.
226
+
227
+ The Congress, whenever two thirds of both Houses shall deem it necessary, shall propose Amendments to this Constitution, or, on the Application of the Legislatures of two thirds of the several States, shall call a Convention for proposing Amendments, which, in either Case, shall be valid to all Intents and Purposes, as Part of this Constitution, when ratified by the Legislatures of three fourths of the several States, or by Conventions in three fourths thereof, as the one or the other Mode of Ratification may be proposed by the Congress; Provided that no Amendment which may be made prior to the Year One thousand eight hundred and eight shall in any Manner affect the first and fourth Clauses in the Ninth Section of the first Article; and that no State, without its Consent, shall be deprived of its equal Suffrage in the Senate.
228
+
229
+ Article. VI.
230
+
231
+ Clause 1: All Debts contracted and Engagements entered into, before the Adoption of this Constitution, shall be as valid against the United States under this Constitution, as under the Confederation.
232
+
233
+ Clause 2: This Constitution, and the Laws of the United States which shall be made in Pursuance thereof; and all Treaties made, or which shall be made, under the Authority of the United States, shall be the supreme Law of the Land; and the Judges in every State shall be bound thereby, any Thing in the Constitution or Laws of any State to the Contrary notwithstanding.
234
+
235
+ Clause 3: The Senators and Representatives before mentioned, and the Members of the several State Legislatures, and all executive and judicial Officers, both of the United States and of the several States, shall be bound by Oath or Affirmation, to support this Constitution; but no religious Test shall ever be required as a Qualification to any Office or public Trust under the United States.
236
+
237
+ Article. VII.
238
+
239
+ The Ratification of the Conventions of nine States, shall be sufficient for the Establishment of this Constitution between the States so ratifying the Same.
240
+ done in Convention by the Unanimous Consent of the States present the Seventeenth Day of September in the Year of our Lord one thousand seven hundred and Eighty seven and of the Independence of the United States of America the Twelfth In witness whereof We have hereunto subscribed our Names,
241
+
242
+ GO WASHINGTON--Presidt. and deputy from Virginia
243
+
244
+ [Signed also by the deputies of twelve States.]
245
+
246
+ Delaware
247
+
248
+ Geo: Read
249
+ Gunning Bedford jun
250
+ John Dickinson
251
+ Richard Bassett
252
+ Jaco: Broom
253
+
254
+ Maryland
255
+
256
+ James MCHenry
257
+ Dan of ST ThoS. Jenifer
258
+ DanL Carroll.
259
+
260
+ Virginia
261
+
262
+ John Blair--
263
+ James Madison Jr.
264
+
265
+ North Carolina
266
+
267
+ WM Blount
268
+ RichD. Dobbs Spaight.
269
+ Hu Williamson
270
+
271
+ South Carolina
272
+
273
+ J. Rutledge
274
+ Charles 1ACotesworth Pinckney
275
+ Charles Pinckney
276
+ Pierce Butler.
277
+
278
+ Georgia
279
+
280
+ William Few
281
+ Abr Baldwin
282
+
283
+ New Hampshire
284
+
285
+ John Langdon
286
+ Nicholas Gilman
287
+
288
+ Massachusetts
289
+
290
+ Nathaniel Gorham
291
+ Rufus King
292
+
293
+ Connecticut
294
+ WM. SamL. Johnson
295
+ Roger Sherman
296
+
297
+ New York
298
+
299
+ Alexander Hamilton
300
+
301
+ New Jersey
302
+
303
+ Wil: Livingston
304
+ David Brearley.
305
+ WM. Paterson.
306
+ Jona: Dayton
307
+
308
+ Pennsylvania
309
+
310
+ B Franklin
311
+ Thomas Mifflin
312
+ RobT Morris
313
+ Geo. Clymer
314
+ ThoS. FitzSimons
315
+ Jared Ingersoll
316
+ James Wilson.
317
+ Gouv Morris
318
+
319
+ Attest William Jackson Secretary
320
+ END
@@ -0,0 +1,5 @@
1
+ require "rubygems"
2
+ require "highline/import"
3
+
4
+ pass = ask("Enter your password: ") { |q| q.echo = false }
5
+ puts "Your password is #{pass}!"
@@ -0,0 +1,19 @@
1
+ require "rubygems"
2
+ require "highline/import"
3
+
4
+ tounge_twister = ask("... try saying that three times fast") do |q|
5
+ q.gather = 3
6
+ q.verify_match = true
7
+ q.responses[:mismatch] = "Nope, those don't match. Try again."
8
+ end
9
+
10
+ puts "Ok, you did it."
11
+
12
+ pass = ask("Enter your password: ") do |q|
13
+ q.echo = '*'
14
+ q.verify_match = true
15
+ q.gather = {"Enter a password" => '',
16
+ "Please type it again for verification" => ''}
17
+ end
18
+
19
+ puts "Your password is now #{pass}!"
@@ -0,0 +1,20 @@
1
+ # trapping_eof.rb
2
+ #
3
+ # Created by James Edward Gray II on 2006-02-20.
4
+ # Copyright 2006 Gray Productions. All rights reserved.
5
+
6
+ require "rubygems"
7
+ require "highline/import"
8
+
9
+ loop do
10
+ begin
11
+ name = ask("What's your name?")
12
+ break if name == "exit"
13
+ puts "Hello, #{name}!"
14
+ rescue EOFError # HighLine throws this if @input.eof?
15
+ break
16
+ end
17
+ end
18
+
19
+ puts "Goodbye, dear friend."
20
+ exit
@@ -0,0 +1,15 @@
1
+ # using_readline.rb
2
+ #
3
+ # Created by James Edward Gray II on 2005-07-06.
4
+ # Copyright 2005 Gray Productions. All rights reserved.
5
+
6
+ require "rubygems"
7
+ require "highline/import"
8
+
9
+ loop do
10
+ cmd = ask("Enter command: ", %w{save sample load reset quit}) do |q|
11
+ q.readline = true
12
+ end
13
+ say("Executing \"#{cmd}\"...")
14
+ break if cmd == "quit"
15
+ end
data/highline.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ DIR = File.dirname(__FILE__)
2
+ LIB = File.join(DIR, *%w[lib highline.rb])
3
+ GEM_VERSION = open(LIB) { |lib|
4
+ lib.each { |line|
5
+ if v = line[/^\s*VERSION\s*=\s*(['"])(\d+\.\d+\.\d+)\1/, 2]
6
+ break v
7
+ end
8
+ }
9
+ }
10
+
11
+ SPEC = Gem::Specification.new do |spec|
12
+ spec.name = "highline-sgonyea"
13
+ spec.version = GEM_VERSION
14
+ spec.platform = Gem::Platform::RUBY
15
+ spec.summary = "HighLine is a high-level command-line IO library."
16
+ spec.files = `git ls-files`.split("\n")
17
+
18
+ spec.test_files = `git ls-files -- test/*.rb`.split("\n")
19
+ spec.has_rdoc = true
20
+ spec.extra_rdoc_files = %w[README.rdoc INSTALL TODO CHANGELOG LICENSE]
21
+ spec.rdoc_options << '--title' << 'HighLine Documentation' <<
22
+ '--main' << 'README'
23
+
24
+ spec.require_path = 'lib'
25
+
26
+ spec.author = "James Edward Gray II"
27
+ spec.email = "james@graysoftinc.com"
28
+ spec.rubyforge_project = "highline"
29
+ spec.homepage = "http://highline.rubyforge.org"
30
+ spec.description = <<END_DESC
31
+ A high-level IO library that provides validation, type conversion, and more for
32
+ command-line interfaces. HighLine also includes a complete menu system that can
33
+ crank out anything from simple list selection to complete shells with just
34
+ minutes of work.
35
+ END_DESC
36
+ end
data/lib/highline.rb ADDED
@@ -0,0 +1,1000 @@
1
+ # highline.rb
2
+ #
3
+ # Created by James Edward Gray II on 2005-04-26.
4
+ # Copyright 2005 Gray Productions. All rights reserved.
5
+ #
6
+ # See HighLine for documentation.
7
+ #
8
+ # This is Free Software. See LICENSE and COPYING for details.
9
+
10
+ require "erb"
11
+ require "optparse"
12
+ require "stringio"
13
+ require "abbrev"
14
+ require "highline/system_extensions"
15
+ require "highline/question"
16
+ require "highline/menu"
17
+ require "highline/color_scheme"
18
+ require "highline/style"
19
+
20
+ #
21
+ # A HighLine object is a "high-level line oriented" shell over an input and an
22
+ # output stream. HighLine simplifies common console interaction, effectively
23
+ # replacing puts() and gets(). User code can simply specify the question to ask
24
+ # and any details about user interaction, then leave the rest of the work to
25
+ # HighLine. When HighLine.ask() returns, you'll have the answer you requested,
26
+ # even if HighLine had to ask many times, validate results, perform range
27
+ # checking, convert types, etc.
28
+ #
29
+ class HighLine
30
+ # The version of the installed library.
31
+ VERSION = "1.6.12".freeze
32
+
33
+ # An internal HighLine error. User code does not need to trap this.
34
+ class QuestionError < StandardError
35
+ # do nothing, just creating a unique error type
36
+ end
37
+
38
+ # The setting used to disable color output.
39
+ @@use_color = true
40
+
41
+ # Pass +false+ to _setting_ to turn off HighLine's color escapes.
42
+ def self.use_color=( setting )
43
+ @@use_color = setting
44
+ end
45
+
46
+ # Returns true if HighLine is currently using color escapes.
47
+ def self.use_color?
48
+ @@use_color
49
+ end
50
+
51
+ # For checking if the current version of HighLine supports RGB colors
52
+ # Usage: HighLine.supports_rgb_color? rescue false # rescue for compatibility with older versions
53
+ # Note: color usage also depends on HighLine.use_color being set
54
+ def self.supports_rgb_color?
55
+ true
56
+ end
57
+
58
+ # The setting used to disable EOF tracking.
59
+ @@track_eof = true
60
+
61
+ # Pass +false+ to _setting_ to turn off HighLine's EOF tracking.
62
+ def self.track_eof=( setting )
63
+ @@track_eof = setting
64
+ end
65
+
66
+ # Returns true if HighLine is currently tracking EOF for input.
67
+ def self.track_eof?
68
+ @@track_eof
69
+ end
70
+
71
+ # The setting used to control color schemes.
72
+ @@color_scheme = nil
73
+
74
+ # Pass ColorScheme to _setting_ to turn set a HighLine color scheme.
75
+ def self.color_scheme=( setting )
76
+ @@color_scheme = setting
77
+ end
78
+
79
+ # Returns the current color scheme.
80
+ def self.color_scheme
81
+ @@color_scheme
82
+ end
83
+
84
+ # Returns +true+ if HighLine is currently using a color scheme.
85
+ def self.using_color_scheme?
86
+ not @@color_scheme.nil?
87
+ end
88
+
89
+ #
90
+ # Embed in a String to clear all previous ANSI sequences. This *MUST* be
91
+ # done before the program exits!
92
+ #
93
+
94
+ ERASE_LINE_STYLE = Style.new(:name=>:erase_line, :builtin=>true, :code=>"\e[K") # Erase the current line of terminal output
95
+ ERASE_CHAR_STYLE = Style.new(:name=>:erase_char, :builtin=>true, :code=>"\e[P") # Erase the character under the cursor.
96
+ CLEAR_STYLE = Style.new(:name=>:clear, :builtin=>true, :code=>"\e[0m") # Clear color settings
97
+ RESET_STYLE = Style.new(:name=>:reset, :builtin=>true, :code=>"\e[0m") # Alias for CLEAR.
98
+ BOLD_STYLE = Style.new(:name=>:bold, :builtin=>true, :code=>"\e[1m") # Bold; Note: bold + a color works as you'd expect,
99
+ # for example bold black. Bold without a color displays
100
+ # the system-defined bold color (e.g. red on Mac iTerm)
101
+ DARK_STYLE = Style.new(:name=>:dark, :builtin=>true, :code=>"\e[2m") # Dark; support uncommon
102
+ UNDERLINE_STYLE = Style.new(:name=>:underline, :builtin=>true, :code=>"\e[4m") # Underline
103
+ UNDERSCORE_STYLE = Style.new(:name=>:underscore, :builtin=>true, :code=>"\e[4m") # Alias for UNDERLINE
104
+ BLINK_STYLE = Style.new(:name=>:blink, :builtin=>true, :code=>"\e[5m") # Blink; support uncommon
105
+ REVERSE_STYLE = Style.new(:name=>:reverse, :builtin=>true, :code=>"\e[7m") # Reverse foreground and background
106
+ CONCEALED_STYLE = Style.new(:name=>:concealed, :builtin=>true, :code=>"\e[8m") # Concealed; support uncommon
107
+
108
+ STYLES = %w{CLEAR RESET BOLD DARK UNDERLINE UNDERSCORE BLINK REVERSE CONCEALED}
109
+
110
+ # These RGB colors are approximate; see http://en.wikipedia.org/wiki/ANSI_escape_code
111
+ BLACK_STYLE = Style.new(:name=>:black, :builtin=>true, :code=>"\e[30m", :rgb=>[ 0, 0, 0])
112
+ RED_STYLE = Style.new(:name=>:red, :builtin=>true, :code=>"\e[31m", :rgb=>[128, 0, 0])
113
+ GREEN_STYLE = Style.new(:name=>:green, :builtin=>true, :code=>"\e[32m", :rgb=>[ 0,128, 0])
114
+ BLUE_STYLE = Style.new(:name=>:blue, :builtin=>true, :code=>"\e[34m", :rgb=>[ 0, 0,128])
115
+ YELLOW_STYLE = Style.new(:name=>:yellow, :builtin=>true, :code=>"\e[33m", :rgb=>[128,128, 0])
116
+ MAGENTA_STYLE = Style.new(:name=>:magenta, :builtin=>true, :code=>"\e[35m", :rgb=>[128, 0,128])
117
+ CYAN_STYLE = Style.new(:name=>:cyan, :builtin=>true, :code=>"\e[36m", :rgb=>[ 0,128,128])
118
+ # On Mac OSX Terminal, white is actually gray
119
+ WHITE_STYLE = Style.new(:name=>:white, :builtin=>true, :code=>"\e[37m", :rgb=>[192,192,192])
120
+ # Alias for WHITE, since WHITE is actually a light gray on Macs
121
+ GRAY_STYLE = Style.new(:name=>:gray, :builtin=>true, :code=>"\e[37m", :rgb=>[192,192,192])
122
+ # On Mac OSX Terminal, this is black foreground, or bright white background.
123
+ # Also used as base for RGB colors, if available
124
+ NONE_STYLE = Style.new(:name=>:none, :builtin=>true, :code=>"\e[38m", :rgb=>[ 0, 0, 0])
125
+
126
+ BASIC_COLORS = %w{BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE GRAY NONE}
127
+
128
+ colors = BASIC_COLORS.dup
129
+ BASIC_COLORS.each do |color|
130
+ bright_color = "BRIGHT_#{color}"
131
+ colors << bright_color
132
+ const_set bright_color+'_STYLE', const_get(color + '_STYLE').bright
133
+ end
134
+ COLORS = colors
135
+
136
+ colors.each do |color|
137
+ const_set color, const_get("#{color}_STYLE").code
138
+ const_set "ON_#{color}_STYLE", const_get("#{color}_STYLE").on
139
+ const_set "ON_#{color}", const_get("ON_#{color}_STYLE").code
140
+ end
141
+ ON_NONE_STYLE.rgb = [255,255,255] # Override; white background
142
+
143
+ STYLES.each do |style|
144
+ const_set style, const_get("#{style}_STYLE").code
145
+ end
146
+
147
+ # For RGB colors:
148
+ def self.const_missing(name)
149
+ if name.to_s =~ /^(ON_)?(RGB_)([A-F0-9]{6})(_STYLE)?$/ # RGB color
150
+ on = $1
151
+ suffix = $4
152
+ if suffix
153
+ code_name = $1.to_s + $2 + $3
154
+ else
155
+ code_name = name.to_s
156
+ end
157
+ style_name = code_name + '_STYLE'
158
+ style = Style.rgb($3)
159
+ style = style.on if on
160
+ const_set(style_name, style)
161
+ const_set(code_name, style.code)
162
+ if suffix
163
+ style
164
+ else
165
+ style.code
166
+ end
167
+ else
168
+ raise NameError, "Bad color or uninitialized constant #{name}"
169
+ end
170
+ end
171
+
172
+ #
173
+ # Create an instance of HighLine, connected to the streams _input_
174
+ # and _output_.
175
+ #
176
+ def initialize( input = $stdin, output = $stdout,
177
+ wrap_at = nil, page_at = nil )
178
+ @input = input
179
+ @output = output
180
+ if JRUBY
181
+ require 'java'
182
+ java_import 'java.io.OutputStreamWriter'
183
+ java_import 'java.nio.channels.Channels'
184
+ java_import 'jline.ConsoleReader'
185
+ java_import 'jline.Terminal'
186
+
187
+ @java_input = Channels.newInputStream($stdin.to_channel)
188
+ @java_output = OutputStreamWriter.new(Channels.newOutputStream($stdout.to_channel))
189
+ @java_terminal = Terminal.getTerminal
190
+ @java_console = ConsoleReader.new(@java_input, @java_output)
191
+ @java_console.setUseHistory(false)
192
+ @java_console.setBellEnabled(true)
193
+ @java_console.setUsePagination(false)
194
+ end
195
+
196
+ self.wrap_at = wrap_at
197
+ self.page_at = page_at
198
+
199
+ @question = nil
200
+ @answer = nil
201
+ @menu = nil
202
+ @header = nil
203
+ @prompt = nil
204
+ @gather = nil
205
+ @answers = nil
206
+ @key = nil
207
+ end
208
+
209
+ include HighLine::SystemExtensions
210
+
211
+ # The current column setting for wrapping output.
212
+ attr_reader :wrap_at
213
+ # The current row setting for paging output.
214
+ attr_reader :page_at
215
+
216
+ #
217
+ # A shortcut to HighLine.ask() a question that only accepts "yes" or "no"
218
+ # answers ("y" and "n" are allowed) and returns +true+ or +false+
219
+ # (+true+ for "yes"). If provided a +true+ value, _character_ will cause
220
+ # HighLine to fetch a single character response. A block can be provided
221
+ # to further configure the question as in HighLine.ask()
222
+ #
223
+ # Raises EOFError if input is exhausted.
224
+ #
225
+ def agree( yes_or_no_question, character = nil )
226
+ ask(yes_or_no_question, lambda { |yn| yn.downcase[0] == ?y}) do |q|
227
+ q.validate = /\Ay(?:es)?|no?\Z/i
228
+ q.responses[:not_valid] = 'Please enter "yes" or "no".'
229
+ q.responses[:ask_on_error] = :question
230
+ q.character = character
231
+
232
+ yield q if block_given?
233
+ end
234
+ end
235
+
236
+ #
237
+ # This method is the primary interface for user input. Just provide a
238
+ # _question_ to ask the user, the _answer_type_ you want returned, and
239
+ # optionally a code block setting up details of how you want the question
240
+ # handled. See HighLine.say() for details on the format of _question_, and
241
+ # HighLine::Question for more information about _answer_type_ and what's
242
+ # valid in the code block.
243
+ #
244
+ # If <tt>@question</tt> is set before ask() is called, parameters are
245
+ # ignored and that object (must be a HighLine::Question) is used to drive
246
+ # the process instead.
247
+ #
248
+ # Raises EOFError if input is exhausted.
249
+ #
250
+ def ask( question, answer_type = String, &details ) # :yields: question
251
+ @question ||= Question.new(question, answer_type, &details)
252
+
253
+ return gather if @question.gather
254
+
255
+ # readline() needs to handle it's own output, but readline only supports
256
+ # full line reading. Therefore if @question.echo is anything but true,
257
+ # the prompt will not be issued. And we have to account for that now.
258
+ say(@question) unless (@question.readline and @question.echo == true)
259
+ begin
260
+ @answer = @question.answer_or_default(get_response)
261
+ unless @question.valid_answer?(@answer)
262
+ explain_error(:not_valid)
263
+ raise QuestionError
264
+ end
265
+
266
+ @answer = @question.convert(@answer)
267
+
268
+ if @question.in_range?(@answer)
269
+ if @question.confirm
270
+ # need to add a layer of scope to ask a question inside a
271
+ # question, without destroying instance data
272
+ context_change = self.class.new(@input, @output, @wrap_at, @page_at)
273
+ if @question.confirm == true
274
+ confirm_question = "Are you sure? "
275
+ else
276
+ # evaluate ERb under initial scope, so it will have
277
+ # access to @question and @answer
278
+ template = ERB.new(@question.confirm, nil, "%")
279
+ confirm_question = template.result(binding)
280
+ end
281
+ unless context_change.agree(confirm_question)
282
+ explain_error(nil)
283
+ raise QuestionError
284
+ end
285
+ end
286
+
287
+ @answer
288
+ else
289
+ explain_error(:not_in_range)
290
+ raise QuestionError
291
+ end
292
+ rescue QuestionError
293
+ retry
294
+ rescue ArgumentError, NameError => error
295
+ raise if error.is_a?(NoMethodError)
296
+ if error.message =~ /ambiguous/
297
+ # the assumption here is that OptionParser::Completion#complete
298
+ # (used for ambiguity resolution) throws exceptions containing
299
+ # the word 'ambiguous' whenever resolution fails
300
+ explain_error(:ambiguous_completion)
301
+ else
302
+ explain_error(:invalid_type)
303
+ end
304
+ retry
305
+ rescue Question::NoAutoCompleteMatch
306
+ explain_error(:no_completion)
307
+ retry
308
+ ensure
309
+ @question = nil # Reset Question object.
310
+ end
311
+ end
312
+
313
+ #
314
+ # This method is HighLine's menu handler. For simple usage, you can just
315
+ # pass all the menu items you wish to display. At that point, choose() will
316
+ # build and display a menu, walk the user through selection, and return
317
+ # their choice amoung the provided items. You might use this in a case
318
+ # statement for quick and dirty menus.
319
+ #
320
+ # However, choose() is capable of much more. If provided, a block will be
321
+ # passed a HighLine::Menu object to configure. Using this method, you can
322
+ # customize all the details of menu handling from index display, to building
323
+ # a complete shell-like menuing system. See HighLine::Menu for all the
324
+ # methods it responds to.
325
+ #
326
+ # Raises EOFError if input is exhausted.
327
+ #
328
+ def choose( *items, &details )
329
+ @menu = @question = Menu.new(&details)
330
+ @menu.choices(*items) unless items.empty?
331
+
332
+ # Set _answer_type_ so we can double as the Question for ask().
333
+ @menu.answer_type = if @menu.shell
334
+ lambda do |command| # shell-style selection
335
+ first_word = command.to_s.split.first || ""
336
+
337
+ options = @menu.options
338
+ options.extend(OptionParser::Completion)
339
+ answer = options.complete(first_word)
340
+
341
+ if answer.nil?
342
+ raise Question::NoAutoCompleteMatch
343
+ end
344
+
345
+ [answer.last, command.sub(/^\s*#{first_word}\s*/, "")]
346
+ end
347
+ else
348
+ @menu.options # normal menu selection, by index or name
349
+ end
350
+
351
+ # Provide hooks for ERb layouts.
352
+ @header = @menu.header
353
+ @prompt = @menu.prompt
354
+
355
+ if @menu.shell
356
+ selected = ask("Ignored", @menu.answer_type)
357
+ @menu.select(self, *selected)
358
+ else
359
+ selected = ask("Ignored", @menu.answer_type)
360
+ @menu.select(self, selected)
361
+ end
362
+ end
363
+
364
+ #
365
+ # This method provides easy access to ANSI color sequences, without the user
366
+ # needing to remember to CLEAR at the end of each sequence. Just pass the
367
+ # _string_ to color, followed by a list of _colors_ you would like it to be
368
+ # affected by. The _colors_ can be HighLine class constants, or symbols
369
+ # (:blue for BLUE, for example). A CLEAR will automatically be embedded to
370
+ # the end of the returned String.
371
+ #
372
+ # This method returns the original _string_ unchanged if HighLine::use_color?
373
+ # is +false+.
374
+ #
375
+ def self.color( string, *colors )
376
+ return string unless self.use_color?
377
+ Style(*colors).color(string)
378
+ end
379
+
380
+ # In case you just want the color code, without the embedding and the CLEAR
381
+ def self.color_code(*colors)
382
+ Style(*colors).code
383
+ end
384
+
385
+ # Works as an instance method, same as the class method
386
+ def color_code(*colors)
387
+ self.class.color_code(*colors)
388
+ end
389
+
390
+ # Works as an instance method, same as the class method
391
+ def color(*args)
392
+ self.class.color(*args)
393
+ end
394
+
395
+ # Remove color codes from a string
396
+ def self.uncolor(string)
397
+ Style.uncolor(string)
398
+ end
399
+
400
+ # Works as an instance method, same as the class method
401
+ def uncolor(string)
402
+ self.class.uncolor(string)
403
+ end
404
+
405
+ #
406
+ # This method is a utility for quickly and easily laying out lists. It can
407
+ # be accessed within ERb replacements of any text that will be sent to the
408
+ # user.
409
+ #
410
+ # The only required parameter is _items_, which should be the Array of items
411
+ # to list. A specified _mode_ controls how that list is formed and _option_
412
+ # has different effects, depending on the _mode_. Recognized modes are:
413
+ #
414
+ # <tt>:columns_across</tt>:: _items_ will be placed in columns,
415
+ # flowing from left to right. If given,
416
+ # _option_ is the number of columns to be
417
+ # used. When absent, columns will be
418
+ # determined based on _wrap_at_ or a
419
+ # default of 80 characters.
420
+ # <tt>:columns_down</tt>:: Identical to <tt>:columns_across</tt>,
421
+ # save flow goes down.
422
+ # <tt>:uneven_columns_across</tt>:: Like <tt>:columns_across</tt> but each
423
+ # column is sized independently.
424
+ # <tt>:uneven_columns_down</tt>:: Like <tt>:columns_down</tt> but each
425
+ # column is sized independently.
426
+ # <tt>:inline</tt>:: All _items_ are placed on a single line.
427
+ # The last two _items_ are separated by
428
+ # _option_ or a default of " or ". All
429
+ # other _items_ are separated by ", ".
430
+ # <tt>:rows</tt>:: The default mode. Each of the _items_ is
431
+ # placed on it's own line. The _option_
432
+ # parameter is ignored in this mode.
433
+ #
434
+ # Each member of the _items_ Array is passed through ERb and thus can contain
435
+ # their own expansions. Color escape expansions do not contribute to the
436
+ # final field width.
437
+ #
438
+ def list( items, mode = :rows, option = nil )
439
+ items = items.to_ary.map do |item|
440
+ ERB.new(item, nil, "%").result(binding)
441
+ end
442
+
443
+ if items.empty?
444
+ ""
445
+ else
446
+ case mode
447
+ when :inline
448
+ option = " or " if option.nil?
449
+
450
+ if items.size == 1
451
+ items.first
452
+ else
453
+ items[0..-2].join(", ") + "#{option}#{items.last}"
454
+ end
455
+ when :columns_across, :columns_down
456
+ max_length = actual_length(
457
+ items.max { |a, b| actual_length(a) <=> actual_length(b) }
458
+ )
459
+
460
+ if option.nil?
461
+ limit = @wrap_at || 80
462
+ option = (limit + 2) / (max_length + 2)
463
+ end
464
+
465
+ items = items.map do |item|
466
+ pad = max_length + (item.to_s.length - actual_length(item))
467
+ "%-#{pad}s" % item
468
+ end
469
+ row_count = (items.size / option.to_f).ceil
470
+
471
+ if mode == :columns_across
472
+ rows = Array.new(row_count) { Array.new }
473
+ items.each_with_index do |item, index|
474
+ rows[index / option] << item
475
+ end
476
+
477
+ rows.map { |row| row.join(" ") + "\n" }.join
478
+ else
479
+ columns = Array.new(option) { Array.new }
480
+ items.each_with_index do |item, index|
481
+ columns[index / row_count] << item
482
+ end
483
+
484
+ list = ""
485
+ columns.first.size.times do |index|
486
+ list << columns.map { |column| column[index] }.
487
+ compact.join(" ") + "\n"
488
+ end
489
+ list
490
+ end
491
+ when :uneven_columns_across
492
+ if option.nil?
493
+ limit = @wrap_at || 80
494
+ items.size.downto(1) do |column_count|
495
+ row_count = (items.size / column_count.to_f).ceil
496
+ rows = Array.new(row_count) { Array.new }
497
+ items.each_with_index do |item, index|
498
+ rows[index / column_count] << item
499
+ end
500
+
501
+ widths = Array.new(column_count, 0)
502
+ rows.each do |row|
503
+ row.each_with_index do |field, column|
504
+ size = actual_length(field)
505
+ widths[column] = size if size > widths[column]
506
+ end
507
+ end
508
+
509
+ if column_count == 1 or
510
+ widths.inject(0) { |sum, n| sum + n + 2 } <= limit + 2
511
+ return rows.map { |row|
512
+ row.zip(widths).map { |field, i|
513
+ "%-#{i + (field.to_s.length - actual_length(field))}s" % field
514
+ }.join(" ") + "\n"
515
+ }.join
516
+ end
517
+ end
518
+ else
519
+ row_count = (items.size / option.to_f).ceil
520
+ rows = Array.new(row_count) { Array.new }
521
+ items.each_with_index do |item, index|
522
+ rows[index / option] << item
523
+ end
524
+
525
+ widths = Array.new(option, 0)
526
+ rows.each do |row|
527
+ row.each_with_index do |field, column|
528
+ size = actual_length(field)
529
+ widths[column] = size if size > widths[column]
530
+ end
531
+ end
532
+
533
+ return rows.map { |row|
534
+ row.zip(widths).map { |field, i|
535
+ "%-#{i + (field.to_s.length - actual_length(field))}s" % field
536
+ }.join(" ") + "\n"
537
+ }.join
538
+ end
539
+ when :uneven_columns_down
540
+ if option.nil?
541
+ limit = @wrap_at || 80
542
+ items.size.downto(1) do |column_count|
543
+ row_count = (items.size / column_count.to_f).ceil
544
+ columns = Array.new(column_count) { Array.new }
545
+ items.each_with_index do |item, index|
546
+ columns[index / row_count] << item
547
+ end
548
+
549
+ widths = Array.new(column_count, 0)
550
+ columns.each_with_index do |column, i|
551
+ column.each do |field|
552
+ size = actual_length(field)
553
+ widths[i] = size if size > widths[i]
554
+ end
555
+ end
556
+
557
+ if column_count == 1 or
558
+ widths.inject(0) { |sum, n| sum + n + 2 } <= limit + 2
559
+ list = ""
560
+ columns.first.size.times do |index|
561
+ list << columns.zip(widths).map { |column, width|
562
+ field = column[index]
563
+ "%-#{width + (field.to_s.length - actual_length(field))}s" %
564
+ field
565
+ }.compact.join(" ").strip + "\n"
566
+ end
567
+ return list
568
+ end
569
+ end
570
+ else
571
+ row_count = (items.size / option.to_f).ceil
572
+ columns = Array.new(option) { Array.new }
573
+ items.each_with_index do |item, index|
574
+ columns[index / row_count] << item
575
+ end
576
+
577
+ widths = Array.new(option, 0)
578
+ columns.each_with_index do |column, i|
579
+ column.each do |field|
580
+ size = actual_length(field)
581
+ widths[i] = size if size > widths[i]
582
+ end
583
+ end
584
+
585
+ list = ""
586
+ columns.first.size.times do |index|
587
+ list << columns.zip(widths).map { |column, width|
588
+ field = column[index]
589
+ "%-#{width + (field.to_s.length - actual_length(field))}s" % field
590
+ }.compact.join(" ").strip + "\n"
591
+ end
592
+ return list
593
+ end
594
+ else
595
+ items.map { |i| "#{i}\n" }.join
596
+ end
597
+ end
598
+ end
599
+
600
+ #
601
+ # The basic output method for HighLine objects. If the provided _statement_
602
+ # ends with a space or tab character, a newline will not be appended (output
603
+ # will be flush()ed). All other cases are passed straight to Kernel.puts().
604
+ #
605
+ # The _statement_ parameter is processed as an ERb template, supporting
606
+ # embedded Ruby code. The template is evaluated with a binding inside
607
+ # the HighLine instance, providing easy access to the ANSI color constants
608
+ # and the HighLine.color() method.
609
+ #
610
+ def say( statement )
611
+ statement = statement.to_str
612
+ return unless statement.length > 0
613
+
614
+ template = ERB.new(statement, nil, "%")
615
+ statement = template.result(binding)
616
+
617
+ statement = wrap(statement) unless @wrap_at.nil?
618
+ statement = page_print(statement) unless @page_at.nil?
619
+
620
+ if statement[-1, 1] == " " or statement[-1, 1] == "\t"
621
+ @output.print(statement)
622
+ @output.flush
623
+ else
624
+ @output.puts(statement)
625
+ end
626
+ end
627
+
628
+ #
629
+ # Set to an integer value to cause HighLine to wrap output lines at the
630
+ # indicated character limit. When +nil+, the default, no wrapping occurs. If
631
+ # set to <tt>:auto</tt>, HighLine will attempt to determing the columns
632
+ # available for the <tt>@output</tt> or use a sensible default.
633
+ #
634
+ def wrap_at=( setting )
635
+ @wrap_at = setting == :auto ? output_cols : setting
636
+ end
637
+
638
+ #
639
+ # Set to an integer value to cause HighLine to page output lines over the
640
+ # indicated line limit. When +nil+, the default, no paging occurs. If
641
+ # set to <tt>:auto</tt>, HighLine will attempt to determing the rows available
642
+ # for the <tt>@output</tt> or use a sensible default.
643
+ #
644
+ def page_at=( setting )
645
+ @page_at = setting == :auto ? output_rows - 2 : setting
646
+ end
647
+
648
+ #
649
+ # Returns the number of columns for the console, or a default it they cannot
650
+ # be determined.
651
+ #
652
+ def output_cols
653
+ return 80 unless @output.tty?
654
+ terminal_size.first
655
+ rescue
656
+ return 80
657
+ end
658
+
659
+ #
660
+ # Returns the number of rows for the console, or a default if they cannot be
661
+ # determined.
662
+ #
663
+ def output_rows
664
+ return 24 unless @output.tty?
665
+ terminal_size.last
666
+ rescue
667
+ return 24
668
+ end
669
+
670
+ private
671
+
672
+ #
673
+ # A helper method for sending the output stream and error and repeat
674
+ # of the question.
675
+ #
676
+ def explain_error( error )
677
+ say(@question.responses[error]) unless error.nil?
678
+ if @question.responses[:ask_on_error] == :question
679
+ say(@question)
680
+ elsif @question.responses[:ask_on_error]
681
+ say(@question.responses[:ask_on_error])
682
+ end
683
+ end
684
+
685
+ #
686
+ # Collects an Array/Hash full of answers as described in
687
+ # HighLine::Question.gather().
688
+ #
689
+ # Raises EOFError if input is exhausted.
690
+ #
691
+ def gather( )
692
+ original_question = @question
693
+ original_question_string = @question.question
694
+ original_gather = @question.gather
695
+
696
+ verify_match = @question.verify_match
697
+ @question.gather = false
698
+
699
+ begin # when verify_match is set this loop will repeat until unique_answers == 1
700
+ @answers = [ ]
701
+ @gather = original_gather
702
+ original_question.question = original_question_string
703
+
704
+ case @gather
705
+ when Integer
706
+ @answers << ask(@question)
707
+ @gather -= 1
708
+
709
+ original_question.question = ""
710
+ until @gather.zero?
711
+ @question = original_question
712
+ @answers << ask(@question)
713
+ @gather -= 1
714
+ end
715
+ when ::String, Regexp
716
+ @answers << ask(@question)
717
+
718
+ original_question.question = ""
719
+ until (@gather.is_a?(::String) and @answers.last.to_s == @gather) or
720
+ (@gather.is_a?(Regexp) and @answers.last.to_s =~ @gather)
721
+ @question = original_question
722
+ @answers << ask(@question)
723
+ end
724
+
725
+ @answers.pop
726
+ when Hash
727
+ @answers = { }
728
+ @gather.keys.sort.each do |key|
729
+ @question = original_question
730
+ @key = key
731
+ @answers[key] = ask(@question)
732
+ end
733
+ end
734
+
735
+ if verify_match && (unique_answers(@answers).size > 1)
736
+ @question = original_question
737
+ explain_error(:mismatch)
738
+ else
739
+ verify_match = false
740
+ end
741
+
742
+ end while verify_match
743
+
744
+ original_question.verify_match ? @answer : @answers
745
+ end
746
+
747
+ #
748
+ # A helper method used by HighLine::Question.verify_match
749
+ # for finding whether a list of answers match or differ
750
+ # from each other.
751
+ #
752
+ def unique_answers(list = @answers)
753
+ (list.respond_to?(:values) ? list.values : list).uniq
754
+ end
755
+
756
+ #
757
+ # Read a line of input from the input stream and process whitespace as
758
+ # requested by the Question object.
759
+ #
760
+ # If Question's _readline_ property is set, that library will be used to
761
+ # fetch input. *WARNING*: This ignores the currently set input stream.
762
+ #
763
+ # Raises EOFError if input is exhausted.
764
+ #
765
+ def get_line( )
766
+ if @question.readline
767
+ require "readline" # load only if needed
768
+
769
+ # capture say()'s work in a String to feed to readline()
770
+ old_output = @output
771
+ @output = StringIO.new
772
+ say(@question)
773
+ question = @output.string
774
+ @output = old_output
775
+
776
+ # prep auto-completion
777
+ Readline.completion_proc = lambda do |string|
778
+ @question.selection.grep(/\A#{Regexp.escape(string)}/)
779
+ end
780
+
781
+ # work-around ugly readline() warnings
782
+ old_verbose = $VERBOSE
783
+ $VERBOSE = nil
784
+ raw_answer = Readline.readline(question, true)
785
+ if raw_answer.nil?
786
+ if @@track_eof
787
+ raise EOFError, "The input stream is exhausted."
788
+ else
789
+ raw_answer = String.new # Never return nil
790
+ end
791
+ end
792
+ answer = @question.change_case(
793
+ @question.remove_whitespace(raw_answer))
794
+ $VERBOSE = old_verbose
795
+
796
+ answer
797
+ else
798
+ if JRUBY
799
+ enable_echo_afterwards = @java_terminal.isEchoEnabled
800
+ @java_terminal.disableEcho
801
+ begin
802
+ raw_answer = @java_console.readLine(nil, nil)
803
+ ensure
804
+ @java_terminal.enableEcho if enable_echo_afterwards
805
+ end
806
+ else
807
+ raise EOFError, "The input stream is exhausted." if @@track_eof and
808
+ @input.eof?
809
+ raw_answer = @input.gets
810
+ end
811
+
812
+ @question.change_case(@question.remove_whitespace(raw_answer))
813
+ end
814
+ end
815
+
816
+ def get_single_character(is_stty)
817
+ if JRUBY
818
+ @java_console.readVirtualKey
819
+ elsif is_stty
820
+ @input.getbyte
821
+ else
822
+ get_character(@input)
823
+ end
824
+ end
825
+
826
+ #
827
+ # Return a line or character of input, as requested for this question.
828
+ # Character input will be returned as a single character String,
829
+ # not an Integer.
830
+ #
831
+ # This question's _first_answer_ will be returned instead of input, if set.
832
+ #
833
+ # Raises EOFError if input is exhausted.
834
+ #
835
+ def get_response( )
836
+ return @question.first_answer if @question.first_answer?
837
+
838
+ stty = (CHARACTER_MODE == "stty")
839
+
840
+ if @question.character.nil?
841
+ if @question.echo == true and @question.limit.nil?
842
+ get_line
843
+ else
844
+ if JRUBY
845
+ enable_echo_afterwards = @java_terminal.isEchoEnabled
846
+ @java_terminal.disableEcho
847
+ elsif stty
848
+ raw_no_echo_mode
849
+ end
850
+
851
+ line = ""
852
+ backspace_limit = 0
853
+ begin
854
+
855
+ while character = get_single_character(stty)
856
+ # honor backspace and delete
857
+ if character == 127 or character == 8
858
+ line.slice!(-1, 1)
859
+ backspace_limit -= 1
860
+ else
861
+ line << character.chr
862
+ backspace_limit = line.size
863
+ end
864
+ # looking for carriage return (decimal 13) or
865
+ # newline (decimal 10) in raw input
866
+ break if character == 13 or character == 10
867
+ if @question.echo != false
868
+ if character == 127 or character == 8
869
+ # only backspace if we have characters on the line to
870
+ # eliminate, otherwise we'll tromp over the prompt
871
+ if backspace_limit >= 0 then
872
+ @output.print("\b#{HighLine.Style(:erase_char).code}")
873
+ else
874
+ # do nothing
875
+ end
876
+ else
877
+ if @question.echo == true
878
+ @output.print(character.chr)
879
+ else
880
+ @output.print(@question.echo)
881
+ end
882
+ end
883
+ @output.flush
884
+ end
885
+ break if @question.limit and line.size == @question.limit
886
+ end
887
+ ensure
888
+ if JRUBY
889
+ @java_terminal.enableEcho if enable_echo_afterwards
890
+ elsif stty
891
+ restore_mode
892
+ end
893
+ end
894
+ if @question.overwrite
895
+ @output.print("\r#{HighLine.Style(:erase_line).code}")
896
+ @output.flush
897
+ else
898
+ say("\n")
899
+ end
900
+
901
+ @question.change_case(@question.remove_whitespace(line))
902
+ end
903
+ else
904
+ if JRUBY
905
+ enable_echo_afterwards = @java_terminal.isEchoEnabled
906
+ @java_terminal.disableEcho
907
+ end
908
+ begin
909
+ if @question.character == :getc
910
+ response = get_single_character(true).chr
911
+ else
912
+ response = get_single_character(stty).chr
913
+ if @question.overwrite
914
+ @output.print("\r#{HighLine.Style(:erase_line).code}")
915
+ @output.flush
916
+ else
917
+ echo = if @question.echo == true
918
+ response
919
+ elsif @question.echo != false
920
+ @question.echo
921
+ else
922
+ ""
923
+ end
924
+ say("#{echo}\n")
925
+ end
926
+ end
927
+ ensure
928
+ if JRUBY
929
+ @java_terminal.enableEcho if enable_echo_afterwards
930
+ end
931
+ end
932
+ @question.change_case(response)
933
+ end
934
+ end
935
+
936
+ #
937
+ # Page print a series of at most _page_at_ lines for _output_. After each
938
+ # page is printed, HighLine will pause until the user presses enter/return
939
+ # then display the next page of data.
940
+ #
941
+ # Note that the final page of _output_ is *not* printed, but returned
942
+ # instead. This is to support any special handling for the final sequence.
943
+ #
944
+ def page_print( output )
945
+ lines = output.scan(/[^\n]*\n?/)
946
+ while lines.size > @page_at
947
+ @output.puts lines.slice!(0...@page_at).join
948
+ @output.puts
949
+ # Return last line if user wants to abort paging
950
+ return (["...\n"] + lines.slice(-2,1)).join unless continue_paging?
951
+ end
952
+ return lines.join
953
+ end
954
+
955
+ #
956
+ # Ask user if they wish to continue paging output. Allows them to type "q" to
957
+ # cancel the paging process.
958
+ #
959
+ def continue_paging?
960
+ command = HighLine.new(@input, @output).ask(
961
+ "-- press enter/return to continue or q to stop -- "
962
+ ) { |q| q.character = true }
963
+ command !~ /\A[qQ]\Z/ # Only continue paging if Q was not hit.
964
+ end
965
+
966
+ #
967
+ # Wrap a sequence of _lines_ at _wrap_at_ characters per line. Existing
968
+ # newlines will not be affected by this process, but additional newlines
969
+ # may be added.
970
+ #
971
+ def wrap( text )
972
+ wrapped = [ ]
973
+ text.each_line do |line|
974
+ while line =~ /([^\n]{#{@wrap_at + 1},})/
975
+ search = $1.dup
976
+ replace = $1.dup
977
+ if index = replace.rindex(" ", @wrap_at)
978
+ replace[index, 1] = "\n"
979
+ replace.sub!(/\n[ \t]+/, "\n")
980
+ line.sub!(search, replace)
981
+ else
982
+ line[$~.begin(1) + @wrap_at, 0] = "\n"
983
+ end
984
+ end
985
+ wrapped << line
986
+ end
987
+ return wrapped.join
988
+ end
989
+
990
+ #
991
+ # Returns the length of the passed +string_with_escapes+, minus and color
992
+ # sequence escapes.
993
+ #
994
+ def actual_length( string_with_escapes )
995
+ string_with_escapes.to_s.gsub(/\e\[\d{1,2}m/, "").length
996
+ end
997
+ end
998
+
999
+ require "highline/string_extensions"
1000
+