From 59ef63c122c2af56ea9d5dabcd85346e0d65d8e6 Mon Sep 17 00:00:00 2001 From: brian Date: Fri, 1 May 2015 23:14:41 -0400 Subject: [PATCH 01/27] Init commit --- .gitignore | 22 ++ LICENSE | 674 +++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 4 + 3 files changed, 700 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6cd1df2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..70566f2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ +GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + {one line to give the program's name and a brief idea of what it does.} + Copyright (C) {year} {name of author} + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + {project} Copyright (C) {year} {fullname} + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..089d459 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +termbox-util +============ + +Utility Library for Termbox \ No newline at end of file From d03ef8f013f2dcf7944f420f532a03252505f047 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Fri, 1 May 2015 22:15:36 -0500 Subject: [PATCH 02/27] Initial Commit --- termbox-test/main.go | 78 ++++++++++++++++++++++++ termbox_inputfield.go | 113 +++++++++++++++++++++++++++++++++++ termbox_inputmodal.go | 134 ++++++++++++++++++++++++++++++++++++++++++ termbox_util.go | 57 ++++++++++++++++++ 4 files changed, 382 insertions(+) create mode 100644 termbox-test/main.go create mode 100644 termbox_inputfield.go create mode 100644 termbox_inputmodal.go create mode 100644 termbox_util.go diff --git a/termbox-test/main.go b/termbox-test/main.go new file mode 100644 index 0000000..abc2368 --- /dev/null +++ b/termbox-test/main.go @@ -0,0 +1,78 @@ +package main + +import ( + "github.com/nsf/termbox-go" + "gogs.bullercodeworks.com/brian/termbox-util" + "os" + "syscall" +) + +var keep_running bool + +func main() { + keep_running = true + err := termbox.Init() + if err != nil { + panic(err) + } + defer termbox.Close() + termbox.SetOutputMode(termbox.Output256) + mainLoop() +} + +var input *termbox_util.InputField + +//var input *termbox_util.InputModal + +func layoutAndDrawScreen() { + termbox.Clear(0, termbox.ColorBlack) + drawScreen() + termbox.Flush() +} + +func drawScreen() { + w, h := termbox.Size() + termbox_util.DrawStringAtPoint(termbox_util.AlignText("Termbox Utility Test", w, termbox_util.ALIGN_CENTER), 0, 0, termbox.ColorWhite, termbox.ColorRed) + if input == nil { + // mw, mh := w/4, h/4 + // mx, my := w-(mw/2), h-(mh/2) + mw, mh := w/4, 2 + mx, my := (w/2)-(mw/2), (h/2)-(mh/2) + // input = termbox_util.CreateInputModal("Test Input", mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack) + input = termbox_util.CreateInputField(mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack) + input.SetBordered(true) + } + input.Draw() +} + +func handleKeyEvent(event termbox.Event) bool { + if event.Key == termbox.KeyEsc { + return false + } else { + input.HandleKeyPress(event) + } + return true +} + +func mainLoop() { + layoutAndDrawScreen() + for { + event := termbox.PollEvent() + if event.Type == termbox.EventKey { + if event.Key == termbox.KeyCtrlZ { + process, _ := os.FindProcess(os.Getpid()) + termbox.Close() + process.Signal(syscall.SIGSTOP) + termbox.Init() + } + keep_running = handleKeyEvent(event) + if !keep_running { + break + } + layoutAndDrawScreen() + } + if event.Type == termbox.EventResize { + layoutAndDrawScreen() + } + } +} \ No newline at end of file diff --git a/termbox_inputfield.go b/termbox_inputfield.go new file mode 100644 index 0000000..f551c97 --- /dev/null +++ b/termbox_inputfield.go @@ -0,0 +1,113 @@ +package termbox_util + +import ( + "fmt" + "github.com/nsf/termbox-go" +) + +type InputField struct { + value string + x, y, width, height int + cursor int + fg, bg termbox.Attribute + bordered bool +} + +func CreateInputField(x, y, w, h int, fg, bg termbox.Attribute) *InputField { + i := InputField{x: x, y: y, width: w, height: h, fg: fg, bg: bg} + return &i +} + +func (i *InputField) GetValue() string { return i.value } +func (i *InputField) SetValue(s string) *InputField { + i.value = s + return i +} + +func (i *InputField) GetX() int { return i.x } +func (i *InputField) SetX(x int) *InputField { + i.x = x + return i +} + +func (i *InputField) GetY() int { return i.y } +func (i *InputField) SetY(y int) *InputField { + i.y = y + return i +} + +func (i *InputField) GetWidth() int { return i.width } +func (i *InputField) SetWidth(w int) *InputField { + i.width = w + return i +} + +func (i *InputField) GetHeight() int { return i.height } +func (i *InputField) SetHeight(h int) *InputField { + i.height = h + return i +} + +func (i *InputField) IsBordered() bool { return i.bordered } +func (i *InputField) SetBordered(b bool) *InputField { + i.bordered = b + return i +} + +func (i *InputField) HandleKeyPress(event termbox.Event) bool { + if event.Key == termbox.KeyEnter { + // Done editing + } else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 { + if len(i.value) > 0 { + i.value = i.value[:len(i.value)-1] + } + } else if event.Key == termbox.KeyArrowLeft { + if i.cursor+len(i.value) > 0 { + i.cursor -= 1 + } + } else if event.Key == termbox.KeyArrowRight { + if i.cursor < 0 { + i.cursor += 1 + } + } else { + if i.cursor+len(i.value) == 0 { + i.value = fmt.Sprintf("%s%s", string(event.Ch), i.value) + } else if i.cursor == 0 { + i.value = fmt.Sprintf("%s%s", i.value, string(event.Ch)) + } else { + str_pt_1 := i.value + str_pt_2 := i.value + i.value = fmt.Sprintf("%s%s%s", str_pt_1, string(event.Ch), str_pt_2) + } + } + return true +} + +func (i *InputField) Draw() { + if i.bordered { + DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + } + + var str_pt_1, str_pt_2 string + var cursor_rune rune + if len(i.value) > 0 { + if i.cursor+len(i.value) == 0 { + str_pt_1 = "" + str_pt_2 = i.value[1:] + cursor_rune = rune(i.value[1]) + } else if i.cursor == 0 { + str_pt_1 = i.value + str_pt_2 = "" + cursor_rune = ' ' + } else { + str_pt_1 = i.value[:(len(i.value) + i.cursor)] + str_pt_2 = i.value[(len(i.value)+i.cursor)+1:] + cursor_rune = rune(i.value[len(i.value)+i.cursor]) + } + } else { + str_pt_1, str_pt_2, cursor_rune = "", "", ' ' + } + x, y := DrawStringAtPoint(str_pt_1, i.x+1, i.y+1, i.fg, i.bg) + termbox.SetCell(x, y, cursor_rune, i.bg, i.fg) + DrawStringAtPoint(str_pt_2, x+1, y, i.fg, i.bg) +} diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go new file mode 100644 index 0000000..6aa80e4 --- /dev/null +++ b/termbox_inputmodal.go @@ -0,0 +1,134 @@ +package termbox_util + +import ( + "github.com/nsf/termbox-go" +) + +type InputModal struct { + title string + text string + value string + x, y, width, height int + show_help bool + cursor int + bg, fg termbox.Attribute +} + +func CreateInputModal(text string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal { + i := InputModal{text: text, x: x, y: y, width: width, height: height, fg: fg, bg: bg} + return &i +} + +func (i *InputModal) GetTitle() string { return i.title } +func (i *InputModal) SetTitle(s string) *InputModal { + i.title = s + return i +} + +func (i *InputModal) GetText() string { return i.text } +func (i *InputModal) SetText(s string) *InputModal { + i.text = s + return i +} + +func (i *InputModal) GetValue() string { return i.value } +func (i *InputModal) SetValue(s string) *InputModal { + i.value = s + return i +} + +func (i *InputModal) GetX() int { return i.x } +func (i *InputModal) SetX(x int) *InputModal { + i.x = x + return i +} +func (i *InputModal) GetY() int { return i.y } +func (i *InputModal) SetY(y int) *InputModal { + i.y = y + return i +} + +func (i *InputModal) GetWidth() int { return i.width } +func (i *InputModal) SetWidth(width int) *InputModal { + i.width = width + return i +} + +func (i *InputModal) GetHeight() int { return i.height } +func (i *InputModal) SetHeight(height int) *InputModal { + i.height = height + return i +} + +func (i *InputModal) HelpIsShown() bool { return i.show_help } +func (i *InputModal) ShowHelp(b bool) *InputModal { + i.show_help = b + return i +} + +func (i *InputModal) GetCursorPos() int { return i.cursor } +func (i *InputModal) SetCursorPos(c int) *InputModal { + i.cursor = c + return i +} +func (i *InputModal) MoveCursorLeft() *InputModal { + if len(i.value)+(i.GetCursorPos()) > 0 { + i.cursor = i.GetCursorPos() - 1 + } + return i +} +func (i *InputModal) MoveCursorRight() *InputModal { + if i.GetCursorPos() < 0 { + i.cursor = i.GetCursorPos() + 1 + } + return i +} + +func (i *InputModal) GetBackground() termbox.Attribute { return i.bg } +func (i *InputModal) SetBackground(bg termbox.Attribute) *InputModal { + i.bg = bg + return i +} + +func (i *InputModal) GetForeground() termbox.Attribute { return i.fg } +func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal { + i.fg = fg + return i +} + +func (i *InputModal) HandleKeyPress(event termbox.Event) bool { + if event.Key == termbox.KeyEnter { + // Done editing + } else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 { + i.value = i.value[:len(i.value)-1] + i.cursor -= 1 + } else { + i.value += string(event.Ch) + i.cursor += 1 + } + return true +} +func (i *InputModal) Draw() { + // First blank out the area we'll be putting the modal + FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + // Now draw the border + DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + + DrawBorder(i.x+2, i.y+2, i.x+i.width-2, i.y+4, i.fg, i.bg) + // TODO: Output Cursor at appropriate spot + var output_string_1, output_string_2 string + var cursor_rune rune + if len(i.value) > 0 { + output_string_1 = i.value[:(len(i.value) - 1 + i.cursor)] + output_string_2 = i.value[(len(i.value) - 1 + i.cursor):] + cursor_rune = ' ' + } else { + output_string_1 = "" + output_string_2 = "" + cursor_rune = ' ' + } + + DrawStringAtPoint(output_string_1, i.x+3, i.y+3, i.fg, i.bg) + termbox.SetCell(i.x+len(output_string_1), i.y+3, cursor_rune, i.bg, i.fg) + DrawStringAtPoint(output_string_2, i.x+3+len(output_string_1)+1, i.y+3, i.fg, i.bg) +} diff --git a/termbox_util.go b/termbox_util.go new file mode 100644 index 0000000..3f6c25b --- /dev/null +++ b/termbox_util.go @@ -0,0 +1,57 @@ +package termbox_util + +import ( + "fmt" + "github.com/nsf/termbox-go" + "strings" +) + +type TextAlignment int + +const ( + ALIGN_LEFT = iota + ALIGN_CENTER + ALIGN_RIGHT +) + +/* Basic Output Helpers */ +func DrawStringAtPoint(str string, x int, y int, fg termbox.Attribute, bg termbox.Attribute) (int, int) { + x_pos := x + for _, runeValue := range str { + termbox.SetCell(x_pos, y, runeValue, fg, bg) + x_pos++ + } + return x_pos, y +} + +func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) { + for xx := x1; xx <= x2; xx++ { + for yx := y1; yx <= y2; yx++ { + termbox.SetCell(xx, yx, r, fg, bg) + } + } +} + +func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) { + FillWithChar('|', x1, y1, x1, y2, fg, bg) + FillWithChar('|', x2, y1, x2, y2, fg, bg) + FillWithChar('-', x1, y1, x2, y1, fg, bg) + FillWithChar('-', x1, y2, x2, y2, fg, bg) +} + +func AlignText(txt string, width int, align TextAlignment) string { + num_spaces := width - len(txt) + switch align { + case ALIGN_CENTER: + return fmt.Sprintf("%s%s%s", + strings.Repeat(" ", num_spaces/2), + txt, strings.Repeat(" ", num_spaces/2), + ) + case ALIGN_RIGHT: + return fmt.Sprintf("%s%s", strings.Repeat(" ", num_spaces), txt) + default: + return fmt.Sprintf("%s%s", txt, strings.Repeat(" ", num_spaces)) + } +} + +/* More advanced things are in their respective files*/ From 0fc0c58c6da8a87fcd3c61c6f7caddbf91800839 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sun, 3 May 2015 17:11:50 -0500 Subject: [PATCH 03/27] Input Modal/Field Works --- termbox-test/main.go | 62 ++++++++++++++++++++++++----- termbox_inputfield.go | 6 +-- termbox_inputmodal.go | 91 ++++++++++++++++++++----------------------- 3 files changed, 97 insertions(+), 62 deletions(-) diff --git a/termbox-test/main.go b/termbox-test/main.go index abc2368..855f210 100644 --- a/termbox-test/main.go +++ b/termbox-test/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "github.com/nsf/termbox-go" "gogs.bullercodeworks.com/brian/termbox-util" "os" @@ -20,9 +21,12 @@ func main() { mainLoop() } -var input *termbox_util.InputField - -//var input *termbox_util.InputModal +//var input *termbox_util.InputField +var input *termbox_util.InputModal +var new_key, new_val string +var mode string +var drawY int +var added_stuff []string func layoutAndDrawScreen() { termbox.Clear(0, termbox.ColorBlack) @@ -34,20 +38,57 @@ func drawScreen() { w, h := termbox.Size() termbox_util.DrawStringAtPoint(termbox_util.AlignText("Termbox Utility Test", w, termbox_util.ALIGN_CENTER), 0, 0, termbox.ColorWhite, termbox.ColorRed) if input == nil { - // mw, mh := w/4, h/4 - // mx, my := w-(mw/2), h-(mh/2) - mw, mh := w/4, 2 + mw, mh := w/4, h/4 mx, my := (w/2)-(mw/2), (h/2)-(mh/2) - // input = termbox_util.CreateInputModal("Test Input", mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack) - input = termbox_util.CreateInputField(mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack) - input.SetBordered(true) + input = termbox_util.CreateInputModal("", mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack) + input.Clear() + } + if mode == "bucket" { + if input.IsDone() { + added_stuff = append(added_stuff, fmt.Sprintf("New Bucket %s", input.GetValue())) + input.Clear() + mode = "" + } else { + input.Draw() + } + } else if mode == "pair" { + if input.IsDone() { + if new_key == "" { + new_key = input.GetValue() + input.Clear() + input.SetTitle("Pair Value") + } else { + added_stuff = append(added_stuff, fmt.Sprintf("New Pair %s => %s", new_key, input.GetValue())) + mode = "" + input.Clear() + } + } + if mode == "pair" && !input.IsDone() { + input.Draw() + } + } + if mode == "" { + for i := range added_stuff { + termbox_util.DrawStringAtPoint(added_stuff[i], 1, 3+i, termbox.ColorWhite, termbox.ColorRed) + } } - input.Draw() } func handleKeyEvent(event termbox.Event) bool { if event.Key == termbox.KeyEsc { return false + } else if event.Key == termbox.KeyCtrlB { + mode = "bucket" + new_key = "" + new_val = "" + input.Clear() + input.SetTitle("Bucket Name") + } else if event.Key == termbox.KeyCtrlP { + mode = "pair" + new_key = "" + new_val = "" + input.Clear() + input.SetTitle("Pair Key") } else { input.HandleKeyPress(event) } @@ -55,6 +96,7 @@ func handleKeyEvent(event termbox.Event) bool { } func mainLoop() { + added_stuff = append(added_stuff, "Ctrl+B = Add Bucket; Ctrl+P = Add Pair") layoutAndDrawScreen() for { event := termbox.PollEvent() diff --git a/termbox_inputfield.go b/termbox_inputfield.go index f551c97..9c1454c 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -75,8 +75,8 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool { } else if i.cursor == 0 { i.value = fmt.Sprintf("%s%s", i.value, string(event.Ch)) } else { - str_pt_1 := i.value - str_pt_2 := i.value + str_pt_1 := i.value[:(len(i.value) + i.cursor)] + str_pt_2 := i.value[(len(i.value) + i.cursor):] i.value = fmt.Sprintf("%s%s%s", str_pt_1, string(event.Ch), str_pt_2) } } @@ -94,7 +94,7 @@ func (i *InputField) Draw() { if i.cursor+len(i.value) == 0 { str_pt_1 = "" str_pt_2 = i.value[1:] - cursor_rune = rune(i.value[1]) + cursor_rune = rune(i.value[0]) } else if i.cursor == 0 { str_pt_1 = i.value str_pt_2 = "" diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 6aa80e4..0b52635 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -7,15 +7,19 @@ import ( type InputModal struct { title string text string - value string + input *InputField x, y, width, height int show_help bool cursor int bg, fg termbox.Attribute + is_done bool + value string } -func CreateInputModal(text string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal { - i := InputModal{text: text, x: x, y: y, width: width, height: height, fg: fg, bg: bg} +func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal { + i := InputModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg} + i.input = CreateInputField(i.x+1, i.y+3, i.width-2, 2, i.fg, i.bg) + i.input.bordered = true return &i } @@ -31,12 +35,6 @@ func (i *InputModal) SetText(s string) *InputModal { return i } -func (i *InputModal) GetValue() string { return i.value } -func (i *InputModal) SetValue(s string) *InputModal { - i.value = s - return i -} - func (i *InputModal) GetX() int { return i.x } func (i *InputModal) SetX(x int) *InputModal { i.x = x @@ -66,24 +64,6 @@ func (i *InputModal) ShowHelp(b bool) *InputModal { return i } -func (i *InputModal) GetCursorPos() int { return i.cursor } -func (i *InputModal) SetCursorPos(c int) *InputModal { - i.cursor = c - return i -} -func (i *InputModal) MoveCursorLeft() *InputModal { - if len(i.value)+(i.GetCursorPos()) > 0 { - i.cursor = i.GetCursorPos() - 1 - } - return i -} -func (i *InputModal) MoveCursorRight() *InputModal { - if i.GetCursorPos() < 0 { - i.cursor = i.GetCursorPos() + 1 - } - return i -} - func (i *InputModal) GetBackground() termbox.Attribute { return i.bg } func (i *InputModal) SetBackground(bg termbox.Attribute) *InputModal { i.bg = bg @@ -95,18 +75,33 @@ func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal { i.fg = fg return i } +func (i *InputModal) SetDone(b bool) *InputModal { + i.is_done = b + return i +} +func (i *InputModal) IsDone() bool { + return i.is_done +} +func (i *InputModal) GetValue() string { + return i.value +} +func (i *InputModal) Clear() *InputModal { + i.title = "" + i.text = "" + i.input.SetValue("") + i.is_done = false + return i +} func (i *InputModal) HandleKeyPress(event termbox.Event) bool { if event.Key == termbox.KeyEnter { // Done editing - } else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 { - i.value = i.value[:len(i.value)-1] - i.cursor -= 1 + i.value = i.input.GetValue() + i.is_done = true + return true } else { - i.value += string(event.Ch) - i.cursor += 1 + return i.input.HandleKeyPress(event) } - return true } func (i *InputModal) Draw() { // First blank out the area we'll be putting the modal @@ -114,21 +109,19 @@ func (i *InputModal) Draw() { // Now draw the border DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) - DrawBorder(i.x+2, i.y+2, i.x+i.width-2, i.y+4, i.fg, i.bg) - // TODO: Output Cursor at appropriate spot - var output_string_1, output_string_2 string - var cursor_rune rune - if len(i.value) > 0 { - output_string_1 = i.value[:(len(i.value) - 1 + i.cursor)] - output_string_2 = i.value[(len(i.value) - 1 + i.cursor):] - cursor_rune = ' ' - } else { - output_string_1 = "" - output_string_2 = "" - cursor_rune = ' ' + next_y := i.y + 1 + // The title + if i.title != "" { + DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg) + next_y += 1 + FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg) + next_y += 1 } - - DrawStringAtPoint(output_string_1, i.x+3, i.y+3, i.fg, i.bg) - termbox.SetCell(i.x+len(output_string_1), i.y+3, cursor_rune, i.bg, i.fg) - DrawStringAtPoint(output_string_2, i.x+3+len(output_string_1)+1, i.y+3, i.fg, i.bg) + if i.text != "" { + DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) + next_y += 1 + } + i.input.SetY(next_y) + i.input.Draw() + next_y += 3 } From cf3fa0559d2ae4d6d77d6982146b1cfb2ae161d9 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 4 May 2015 13:52:10 -0500 Subject: [PATCH 04/27] Add Confirm Modal Also update inputfield/inputmodal --- termbox_confirmmodal.go | 131 ++++++++++++++++++++++++++++++++++++++++ termbox_inputfield.go | 3 + termbox_inputmodal.go | 13 ++-- 3 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 termbox_confirmmodal.go diff --git a/termbox_confirmmodal.go b/termbox_confirmmodal.go new file mode 100644 index 0000000..632663d --- /dev/null +++ b/termbox_confirmmodal.go @@ -0,0 +1,131 @@ +package termbox_util + +import ( + "github.com/nsf/termbox-go" +) + +type ConfirmModal struct { + title string + text string + x, y, width, height int + show_help bool + cursor int + bg, fg termbox.Attribute + is_done bool + accepted bool + value string +} + +func CreateConfirmModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *ConfirmModal { + i := ConfirmModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg} + if i.title == "" && i.text == "" { + i.title = "Confirm?" + } + i.show_help = true + return &i +} + +func (i *ConfirmModal) GetTitle() string { return i.title } +func (i *ConfirmModal) SetTitle(s string) *ConfirmModal { + i.title = s + return i +} + +func (i *ConfirmModal) GetText() string { return i.text } +func (i *ConfirmModal) SetText(s string) *ConfirmModal { + i.text = s + return i +} + +func (i *ConfirmModal) GetX() int { return i.x } +func (i *ConfirmModal) SetX(x int) *ConfirmModal { + i.x = x + return i +} +func (i *ConfirmModal) GetY() int { return i.y } +func (i *ConfirmModal) SetY(y int) *ConfirmModal { + i.y = y + return i +} + +func (i *ConfirmModal) GetWidth() int { return i.width } +func (i *ConfirmModal) SetWidth(width int) *ConfirmModal { + i.width = width + return i +} + +func (i *ConfirmModal) GetHeight() int { return i.height } +func (i *ConfirmModal) SetHeight(height int) *ConfirmModal { + i.height = height + return i +} + +func (i *ConfirmModal) HelpIsShown() bool { return i.show_help } +func (i *ConfirmModal) ShowHelp(b bool) *ConfirmModal { + i.show_help = b + return i +} + +func (i *ConfirmModal) GetBackground() termbox.Attribute { return i.bg } +func (i *ConfirmModal) SetBackground(bg termbox.Attribute) *ConfirmModal { + i.bg = bg + return i +} + +func (i *ConfirmModal) GetForeground() termbox.Attribute { return i.fg } +func (i *ConfirmModal) SetForeground(fg termbox.Attribute) *ConfirmModal { + i.fg = fg + return i +} + +func (i *ConfirmModal) IsDone() bool { return i.is_done } +func (i *ConfirmModal) SetDone(b bool) *ConfirmModal { + i.is_done = b + return i +} + +func (i *ConfirmModal) IsAccepted() bool { return i.accepted } + +func (i *ConfirmModal) Clear() *ConfirmModal { + i.title = "" + i.text = "" + i.accepted = false + i.is_done = false + return i +} + +func (i *ConfirmModal) HandleKeyPress(event termbox.Event) bool { + if event.Ch == 'Y' || event.Ch == 'y' { + i.accepted = true + i.is_done = true + return true + } else if event.Ch == 'N' || event.Ch == 'n' { + i.accepted = false + i.is_done = true + return true + } + return false +} +func (i *ConfirmModal) Draw() { + // First blank out the area we'll be putting the modal + FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + // Now draw the border + DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + + next_y := i.y + 1 + // The title + if i.title != "" { + DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg) + next_y += 1 + FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg) + next_y += 1 + } + if i.text != "" { + DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) + next_y += 1 + } + next_y += 3 + if i.show_help { + DrawStringAtPoint("(Y/y) Confirm. (N/n) Reject.", i.x+1, next_y, i.fg, i.bg) + } +} diff --git a/termbox_inputfield.go b/termbox_inputfield.go index 9c1454c..52c258d 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -69,6 +69,9 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool { if i.cursor < 0 { i.cursor += 1 } + } else if event.Key == termbox.KeyCtrlU { + // Ctrl+U Clears the Input + i.value = "" } else { if i.cursor+len(i.value) == 0 { i.value = fmt.Sprintf("%s%s", string(event.Ch), i.value) diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 0b52635..61f911e 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -13,12 +13,12 @@ type InputModal struct { cursor int bg, fg termbox.Attribute is_done bool - value string } func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal { i := InputModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg} i.input = CreateInputField(i.x+1, i.y+3, i.width-2, 2, i.fg, i.bg) + i.show_help = true i.input.bordered = true return &i } @@ -82,9 +82,12 @@ func (i *InputModal) SetDone(b bool) *InputModal { func (i *InputModal) IsDone() bool { return i.is_done } -func (i *InputModal) GetValue() string { - return i.value +func (i *InputModal) GetValue() string { return i.input.GetValue() } +func (i *InputModal) SetValue(s string) *InputModal { + i.input.SetValue(s) + return i } + func (i *InputModal) Clear() *InputModal { i.title = "" i.text = "" @@ -96,7 +99,6 @@ func (i *InputModal) Clear() *InputModal { func (i *InputModal) HandleKeyPress(event termbox.Event) bool { if event.Key == termbox.KeyEnter { // Done editing - i.value = i.input.GetValue() i.is_done = true return true } else { @@ -124,4 +126,7 @@ func (i *InputModal) Draw() { i.input.SetY(next_y) i.input.Draw() next_y += 3 + if i.show_help { + DrawStringAtPoint("(ENTER) to Accept. (ESC) to Cancel.", i.x+1, next_y, i.fg, i.bg) + } } From 6f77695994bcf728ff681d10efe5f1899131a622 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 4 May 2015 17:42:35 -0500 Subject: [PATCH 05/27] Some improvements --- termbox_confirmmodal.go | 6 ++++-- termbox_inputmodal.go | 7 +++++-- termbox_util.go | 14 ++++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/termbox_confirmmodal.go b/termbox_confirmmodal.go index 632663d..61d2483 100644 --- a/termbox_confirmmodal.go +++ b/termbox_confirmmodal.go @@ -124,8 +124,10 @@ func (i *ConfirmModal) Draw() { DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) next_y += 1 } - next_y += 3 + next_y += 2 if i.show_help { - DrawStringAtPoint("(Y/y) Confirm. (N/n) Reject.", i.x+1, next_y, i.fg, i.bg) + help_string := " (Y/y) Confirm. (N/n) Reject. " + help_x := (i.x + i.width) - len(help_string) - 1 + DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) } } diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 61f911e..62aff5a 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -105,6 +105,7 @@ func (i *InputModal) HandleKeyPress(event termbox.Event) bool { return i.input.HandleKeyPress(event) } } + func (i *InputModal) Draw() { // First blank out the area we'll be putting the modal FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) @@ -121,12 +122,14 @@ func (i *InputModal) Draw() { } if i.text != "" { DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) - next_y += 1 + next_y += 2 } i.input.SetY(next_y) i.input.Draw() next_y += 3 if i.show_help { - DrawStringAtPoint("(ENTER) to Accept. (ESC) to Cancel.", i.x+1, next_y, i.fg, i.bg) + help_string := " (ENTER) to Accept. (ESC) to Cancel. " + help_x := (i.x + i.width - len(help_string)) - 1 + DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) } } diff --git a/termbox_util.go b/termbox_util.go index 3f6c25b..01b39d3 100644 --- a/termbox_util.go +++ b/termbox_util.go @@ -33,10 +33,16 @@ func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.A } func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) { - FillWithChar('|', x1, y1, x1, y2, fg, bg) - FillWithChar('|', x2, y1, x2, y2, fg, bg) - FillWithChar('-', x1, y1, x2, y1, fg, bg) - FillWithChar('-', x1, y2, x2, y2, fg, bg) + termbox.SetCell(x1, y1, '┌', fg, bg) + FillWithChar('─', x1+1, y1, x2-1, y1, fg, bg) + termbox.SetCell(x2, y1, '┐', fg, bg) + + FillWithChar('|', x1, y1+1, x1, y2-1, fg, bg) + FillWithChar('|', x2, y1+1, x2, y2-1, fg, bg) + + termbox.SetCell(x1, y2, '└', fg, bg) + FillWithChar('─', x1+1, y2, x2-1, y2, fg, bg) + termbox.SetCell(x2, y2, '┘', fg, bg) } func AlignText(txt string, width int, align TextAlignment) string { From 06420fe4e6112468e91bb6904a6c2adb5acd70db Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 4 May 2015 19:34:48 -0500 Subject: [PATCH 06/27] Pull in Remote Changes --- termbox_inputmodal.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 0b52635..d2d1853 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -82,8 +82,12 @@ func (i *InputModal) SetDone(b bool) *InputModal { func (i *InputModal) IsDone() bool { return i.is_done } +func (i *InputModal) SetValue(s string) *InputModal { + i.input.SetValue(s) + return i +} func (i *InputModal) GetValue() string { - return i.value + return i.input.GetValue() } func (i *InputModal) Clear() *InputModal { i.title = "" From c581974cc0d13f4da5073ffa44199174b35506dc Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Tue, 5 May 2015 15:47:09 -0500 Subject: [PATCH 07/27] Add Show() and Hide() --- termbox_confirmmodal.go | 10 +++++++ termbox_inputmodal.go | 60 +++++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/termbox_confirmmodal.go b/termbox_confirmmodal.go index 61d2483..fc247c1 100644 --- a/termbox_confirmmodal.go +++ b/termbox_confirmmodal.go @@ -14,6 +14,7 @@ type ConfirmModal struct { is_done bool accepted bool value string + is_visible bool } func CreateConfirmModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *ConfirmModal { @@ -84,6 +85,15 @@ func (i *ConfirmModal) SetDone(b bool) *ConfirmModal { return i } +func (i *ConfirmModal) Show() *ConfirmModal { + i.is_visible = true + return i +} +func (i *ConfirmModal) Hide() *ConfirmModal { + i.is_visible = false + return i +} + func (i *ConfirmModal) IsAccepted() bool { return i.accepted } func (i *ConfirmModal) Clear() *ConfirmModal { diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 2f86f68..4928b5a 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -13,6 +13,7 @@ type InputModal struct { cursor int bg, fg termbox.Attribute is_done bool + is_visible bool } func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal { @@ -75,6 +76,16 @@ func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal { i.fg = fg return i } + +func (i *InputModal) Show() *InputModal { + i.is_visible = true + return i +} +func (i *InputModal) Hide() *InputModal { + i.is_visible = false + return i +} + func (i *InputModal) SetDone(b bool) *InputModal { i.is_done = b return i @@ -94,6 +105,7 @@ func (i *InputModal) Clear() *InputModal { i.text = "" i.input.SetValue("") i.is_done = false + i.is_visible = false return i } @@ -108,29 +120,31 @@ func (i *InputModal) HandleKeyPress(event termbox.Event) bool { } func (i *InputModal) Draw() { - // First blank out the area we'll be putting the modal - FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) - // Now draw the border - DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + if i.is_visible { + // First blank out the area we'll be putting the modal + FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + // Now draw the border + DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) - next_y := i.y + 1 - // The title - if i.title != "" { - DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg) - next_y += 1 - FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg) - next_y += 1 - } - if i.text != "" { - DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) - next_y += 2 - } - i.input.SetY(next_y) - i.input.Draw() - next_y += 3 - if i.show_help { - help_string := " (ENTER) to Accept. (ESC) to Cancel. " - help_x := (i.x + i.width - len(help_string)) - 1 - DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) + next_y := i.y + 1 + // The title + if i.title != "" { + DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg) + next_y += 1 + FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg) + next_y += 1 + } + if i.text != "" { + DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) + next_y += 2 + } + i.input.SetY(next_y) + i.input.Draw() + next_y += 3 + if i.show_help { + help_string := " (ENTER) to Accept. (ESC) to Cancel. " + help_x := (i.x + i.width - len(help_string)) - 1 + DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) + } } } From 4b7d3032c8c7bf50e0f6ab262ec9ae6195483415 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Tue, 12 May 2015 16:05:47 -0500 Subject: [PATCH 08/27] Some Tweaking --- termbox_inputmodal.go | 2 +- termbox_util.go | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 4928b5a..239b2a9 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -136,7 +136,7 @@ func (i *InputModal) Draw() { } if i.text != "" { DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) - next_y += 2 + next_y += 1 } i.input.SetY(next_y) i.input.Draw() diff --git a/termbox_util.go b/termbox_util.go index 01b39d3..0041990 100644 --- a/termbox_util.go +++ b/termbox_util.go @@ -49,10 +49,14 @@ func AlignText(txt string, width int, align TextAlignment) string { num_spaces := width - len(txt) switch align { case ALIGN_CENTER: - return fmt.Sprintf("%s%s%s", - strings.Repeat(" ", num_spaces/2), - txt, strings.Repeat(" ", num_spaces/2), - ) + if num_spaces/2 > 0 { + return fmt.Sprintf("%s%s%s", + strings.Repeat(" ", num_spaces/2), + txt, strings.Repeat(" ", num_spaces/2), + ) + } else { + return txt + } case ALIGN_RIGHT: return fmt.Sprintf("%s%s", strings.Repeat(" ", num_spaces), txt) default: From a018159e667a550349a0d4c002888f0227fe4039 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 18 May 2015 08:50:16 -0500 Subject: [PATCH 09/27] Truncate title if it is too long --- termbox_inputmodal.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 239b2a9..e2b5f49 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -123,13 +123,15 @@ func (i *InputModal) Draw() { if i.is_visible { // First blank out the area we'll be putting the modal FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) - // Now draw the border - DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) - next_y := i.y + 1 // The title if i.title != "" { - DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg) + if len(i.title) > i.width { + diff := i.width - len(i.title) + DrawStringAtPoint(i.title[:len(i.title)+diff-1], i.x+1, next_y, i.fg, i.bg) + } else { + DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg) + } next_y += 1 FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg) next_y += 1 @@ -146,5 +148,7 @@ func (i *InputModal) Draw() { help_x := (i.x + i.width - len(help_string)) - 1 DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) } + // Now draw the border + DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) } } From 1f6f263ed9b54392f55c066d7197fb3ebbf301b7 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Wed, 21 Oct 2015 11:51:22 -0500 Subject: [PATCH 10/27] Linted the library --- termbox-test/main.go | 0 termbox_confirmmodal.go | 99 +++++++++++++++++++++++++++----------- termbox_inputfield.go | 81 ++++++++++++++++++++++--------- termbox_inputmodal.go | 103 ++++++++++++++++++++++++++++------------ termbox_util.go | 49 +++++++++++-------- 5 files changed, 229 insertions(+), 103 deletions(-) mode change 100644 => 100755 termbox-test/main.go diff --git a/termbox-test/main.go b/termbox-test/main.go old mode 100644 new mode 100755 diff --git a/termbox_confirmmodal.go b/termbox_confirmmodal.go index fc247c1..2b40187 100644 --- a/termbox_confirmmodal.go +++ b/termbox_confirmmodal.go @@ -1,143 +1,184 @@ -package termbox_util +package termboxUtil import ( "github.com/nsf/termbox-go" ) +// ConfirmModal is a modal with yes/no (or similar) buttons type ConfirmModal struct { title string text string x, y, width, height int - show_help bool + showHelp bool cursor int bg, fg termbox.Attribute - is_done bool + isDone bool accepted bool value string - is_visible bool + isVisible bool } +// CreateConfirmModal Creates a confirmation modal with the specified attributes func CreateConfirmModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *ConfirmModal { i := ConfirmModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg} if i.title == "" && i.text == "" { i.title = "Confirm?" } - i.show_help = true + i.showHelp = true return &i } +// GetTitle returns the current title of the modal func (i *ConfirmModal) GetTitle() string { return i.title } + +// SetTitle sets the current title of the modal to s func (i *ConfirmModal) SetTitle(s string) *ConfirmModal { i.title = s return i } +// GetText returns the current text of the modal func (i *ConfirmModal) GetText() string { return i.text } + +// SetText sets the text of the modal to s func (i *ConfirmModal) SetText(s string) *ConfirmModal { i.text = s return i } +// GetX returns the current x coordinate of the modal func (i *ConfirmModal) GetX() int { return i.x } + +// SetX sets the current x coordinate of the modal to x func (i *ConfirmModal) SetX(x int) *ConfirmModal { i.x = x return i } + +// GetY returns the current y coordinate of the modal func (i *ConfirmModal) GetY() int { return i.y } + +// SetY sets the current y coordinate of the modal to y func (i *ConfirmModal) SetY(y int) *ConfirmModal { i.y = y return i } +// GetWidth returns the current width of the modal func (i *ConfirmModal) GetWidth() int { return i.width } + +// SetWidth sets the current modal width to width func (i *ConfirmModal) SetWidth(width int) *ConfirmModal { i.width = width return i } +// GetHeight returns the current height of the modal func (i *ConfirmModal) GetHeight() int { return i.height } + +// SetHeight set the height of the modal to height func (i *ConfirmModal) SetHeight(height int) *ConfirmModal { i.height = height return i } -func (i *ConfirmModal) HelpIsShown() bool { return i.show_help } +// HelpIsShown returns true or false if the help is displayed +func (i *ConfirmModal) HelpIsShown() bool { return i.showHelp } + +// ShowHelp sets whether or not to display the help text func (i *ConfirmModal) ShowHelp(b bool) *ConfirmModal { - i.show_help = b + i.showHelp = b return i } +// GetBackground returns the current background color func (i *ConfirmModal) GetBackground() termbox.Attribute { return i.bg } + +// SetBackground sets the background color to bg func (i *ConfirmModal) SetBackground(bg termbox.Attribute) *ConfirmModal { i.bg = bg return i } +// GetForeground returns the current foreground color func (i *ConfirmModal) GetForeground() termbox.Attribute { return i.fg } + +// SetForeground sets the current foreground color to fg func (i *ConfirmModal) SetForeground(fg termbox.Attribute) *ConfirmModal { i.fg = fg return i } -func (i *ConfirmModal) IsDone() bool { return i.is_done } +// IsDone returns whether the user has answered the modal +func (i *ConfirmModal) IsDone() bool { return i.isDone } + +// SetDone sets whether the modal has completed it's purpose func (i *ConfirmModal) SetDone(b bool) *ConfirmModal { - i.is_done = b + i.isDone = b return i } +// Show sets the visibility flag of the modal to true func (i *ConfirmModal) Show() *ConfirmModal { - i.is_visible = true - return i -} -func (i *ConfirmModal) Hide() *ConfirmModal { - i.is_visible = false + i.isVisible = true return i } +// Hide sets the visibility flag of the modal to false +func (i *ConfirmModal) Hide() *ConfirmModal { + i.isVisible = false + return i +} + +// IsAccepted returns whether the user accepted the modal func (i *ConfirmModal) IsAccepted() bool { return i.accepted } +// Clear clears all of the non-positional parameters of the modal func (i *ConfirmModal) Clear() *ConfirmModal { i.title = "" i.text = "" i.accepted = false - i.is_done = false + i.isDone = false return i } +// HandleKeyPress handles the termbox event and returns whether it was consumed func (i *ConfirmModal) HandleKeyPress(event termbox.Event) bool { if event.Ch == 'Y' || event.Ch == 'y' { i.accepted = true - i.is_done = true + i.isDone = true return true } else if event.Ch == 'N' || event.Ch == 'n' { i.accepted = false - i.is_done = true + i.isDone = true return true } return false } + +// Draw draws the modal func (i *ConfirmModal) Draw() { // First blank out the area we'll be putting the modal FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) // Now draw the border DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) - next_y := i.y + 1 + nextY := i.y + 1 // The title if i.title != "" { - DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg) - next_y += 1 - FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg) - next_y += 1 + DrawStringAtPoint(i.title, i.x+1, nextY, i.fg, i.bg) + nextY++ + FillWithChar('-', i.x+1, nextY, i.x+i.width-1, nextY, i.fg, i.bg) + nextY++ } if i.text != "" { - DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) - next_y += 1 + DrawStringAtPoint(i.text, i.x+1, nextY, i.fg, i.bg) + nextY++ } - next_y += 2 - if i.show_help { - help_string := " (Y/y) Confirm. (N/n) Reject. " - help_x := (i.x + i.width) - len(help_string) - 1 - DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) + nextY += 2 + if i.showHelp { + helpString := " (Y/y) Confirm. (N/n) Reject. " + helpX := (i.x + i.width) - len(helpString) - 1 + DrawStringAtPoint(helpString, helpX, nextY, i.fg, i.bg) } } diff --git a/termbox_inputfield.go b/termbox_inputfield.go index 52c258d..f597ef8 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -1,10 +1,12 @@ -package termbox_util +package termboxUtil import ( "fmt" + "github.com/nsf/termbox-go" ) +// InputField is a field for inputting text type InputField struct { value string x, y, width, height int @@ -13,47 +15,67 @@ type InputField struct { bordered bool } +// CreateInputField creates an input field at x, y that is w by h func CreateInputField(x, y, w, h int, fg, bg termbox.Attribute) *InputField { i := InputField{x: x, y: y, width: w, height: h, fg: fg, bg: bg} return &i } +// GetValue gets the current text that is in the InputField func (i *InputField) GetValue() string { return i.value } + +// SetValue sets the current text in the InputField to s func (i *InputField) SetValue(s string) *InputField { i.value = s return i } +// GetX returns the x position of the input field func (i *InputField) GetX() int { return i.x } + +// SetX sets the x position of the input field func (i *InputField) SetX(x int) *InputField { i.x = x return i } +// GetY returns the y position of the input field func (i *InputField) GetY() int { return i.y } + +// SetY sets the y position of the input field func (i *InputField) SetY(y int) *InputField { i.y = y return i } +// GetWidth returns the current width of the input field func (i *InputField) GetWidth() int { return i.width } + +// SetWidth sets the current width of the input field func (i *InputField) SetWidth(w int) *InputField { i.width = w return i } +// GetHeight returns the current height of the input field func (i *InputField) GetHeight() int { return i.height } + +// SetHeight sets the current height of the input field func (i *InputField) SetHeight(h int) *InputField { i.height = h return i } +// IsBordered returns true or false if this input field has a border func (i *InputField) IsBordered() bool { return i.bordered } + +// SetBordered sets whether we render a border around the input field func (i *InputField) SetBordered(b bool) *InputField { i.bordered = b return i } +// HandleKeyPress accepts the termbox event and returns whether it was consumed func (i *InputField) HandleKeyPress(event termbox.Event) bool { if event.Key == termbox.KeyEnter { // Done editing @@ -63,54 +85,67 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool { } } else if event.Key == termbox.KeyArrowLeft { if i.cursor+len(i.value) > 0 { - i.cursor -= 1 + i.cursor-- } } else if event.Key == termbox.KeyArrowRight { if i.cursor < 0 { - i.cursor += 1 + i.cursor++ } } else if event.Key == termbox.KeyCtrlU { // Ctrl+U Clears the Input i.value = "" } else { + // Get the rune to add to our value. Space and Tab are special cases where + // we can't use the event's rune directly + var ch string + switch event.Key { + case termbox.KeySpace: + ch = " " + case termbox.KeyTab: + ch = "\t" + default: + ch = string(event.Ch) + } + if i.cursor+len(i.value) == 0 { - i.value = fmt.Sprintf("%s%s", string(event.Ch), i.value) + i.value = fmt.Sprintf("%s%s", ch, i.value) } else if i.cursor == 0 { - i.value = fmt.Sprintf("%s%s", i.value, string(event.Ch)) + i.value = fmt.Sprintf("%s%s", i.value, ch) } else { - str_pt_1 := i.value[:(len(i.value) + i.cursor)] - str_pt_2 := i.value[(len(i.value) + i.cursor):] - i.value = fmt.Sprintf("%s%s%s", str_pt_1, string(event.Ch), str_pt_2) + strPt1 := i.value[:(len(i.value) + i.cursor)] + strPt2 := i.value[(len(i.value) + i.cursor):] + i.value = fmt.Sprintf("%s%s%s", strPt1, ch, strPt2) } } return true } +// Draw outputs the input field on the screen func (i *InputField) Draw() { if i.bordered { DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) } - var str_pt_1, str_pt_2 string - var cursor_rune rune + var strPt1, strPt2 string + var cursorRune rune if len(i.value) > 0 { if i.cursor+len(i.value) == 0 { - str_pt_1 = "" - str_pt_2 = i.value[1:] - cursor_rune = rune(i.value[0]) + strPt1 = "" + strPt2 = i.value[1:] + cursorRune = rune(i.value[0]) } else if i.cursor == 0 { - str_pt_1 = i.value - str_pt_2 = "" - cursor_rune = ' ' + strPt1 = i.value + strPt2 = "" + cursorRune = ' ' } else { - str_pt_1 = i.value[:(len(i.value) + i.cursor)] - str_pt_2 = i.value[(len(i.value)+i.cursor)+1:] - cursor_rune = rune(i.value[len(i.value)+i.cursor]) + strPt1 = i.value[:(len(i.value) + i.cursor)] + strPt2 = i.value[(len(i.value)+i.cursor)+1:] + cursorRune = rune(i.value[len(i.value)+i.cursor]) } } else { - str_pt_1, str_pt_2, cursor_rune = "", "", ' ' + strPt1, strPt2, cursorRune = "", "", ' ' } - x, y := DrawStringAtPoint(str_pt_1, i.x+1, i.y+1, i.fg, i.bg) - termbox.SetCell(x, y, cursor_rune, i.bg, i.fg) - DrawStringAtPoint(str_pt_2, x+1, y, i.fg, i.bg) + x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg) + termbox.SetCell(x, y, cursorRune, i.bg, i.fg) + DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) } diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index e2b5f49..2a0ac23 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -1,152 +1,193 @@ -package termbox_util +package termboxUtil import ( "github.com/nsf/termbox-go" ) +// InputModal A modal for text input type InputModal struct { title string text string input *InputField x, y, width, height int - show_help bool + showHelp bool cursor int bg, fg termbox.Attribute - is_done bool - is_visible bool + isDone bool + isVisible bool } +// CreateInputModal Create an input modal with the given attributes func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *InputModal { i := InputModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg} i.input = CreateInputField(i.x+1, i.y+3, i.width-2, 2, i.fg, i.bg) - i.show_help = true + i.showHelp = true i.input.bordered = true return &i } +// GetTitle Return the title of the modal func (i *InputModal) GetTitle() string { return i.title } + +// SetTitle Sets the title of the modal to s func (i *InputModal) SetTitle(s string) *InputModal { i.title = s return i } +// GetText Return the text of the modal func (i *InputModal) GetText() string { return i.text } + +// SetText Set the text of the modal to s func (i *InputModal) SetText(s string) *InputModal { i.text = s return i } +// GetX Return the x position of the modal func (i *InputModal) GetX() int { return i.x } + +// SetX set the x position of the modal to x func (i *InputModal) SetX(x int) *InputModal { i.x = x return i } + +// GetY Return the y position of the modal func (i *InputModal) GetY() int { return i.y } + +// SetY Set the y position of the modal to y func (i *InputModal) SetY(y int) *InputModal { i.y = y return i } +// GetWidth Return the width of the modal func (i *InputModal) GetWidth() int { return i.width } + +// SetWidth Set the width of the modal to width func (i *InputModal) SetWidth(width int) *InputModal { i.width = width return i } +// GetHeight Return the height of the modal func (i *InputModal) GetHeight() int { return i.height } + +// SetHeight Set the height of the modal to height func (i *InputModal) SetHeight(height int) *InputModal { i.height = height return i } -func (i *InputModal) HelpIsShown() bool { return i.show_help } +// HelpIsShown Returns whether the modal is showing it's help text or not +func (i *InputModal) HelpIsShown() bool { return i.showHelp } + +// ShowHelp Set the "Show Help" flag func (i *InputModal) ShowHelp(b bool) *InputModal { - i.show_help = b + i.showHelp = b return i } +// GetBackground Return the current background color of the modal func (i *InputModal) GetBackground() termbox.Attribute { return i.bg } + +// SetBackground Set the current background color to bg func (i *InputModal) SetBackground(bg termbox.Attribute) *InputModal { i.bg = bg return i } +// GetForeground Return the current foreground color func (i *InputModal) GetForeground() termbox.Attribute { return i.fg } + +// SetForeground Set the foreground color to fg func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal { i.fg = fg return i } +// Show Sets the visibility flag to true func (i *InputModal) Show() *InputModal { - i.is_visible = true + i.isVisible = true return i } + +// Hide Sets the visibility flag to false func (i *InputModal) Hide() *InputModal { - i.is_visible = false + i.isVisible = false return i } +// SetDone Sets the flag that tells whether this modal has completed it's purpose func (i *InputModal) SetDone(b bool) *InputModal { - i.is_done = b + i.isDone = b return i } + +// IsDone Returns the "isDone" flag func (i *InputModal) IsDone() bool { - return i.is_done + return i.isDone } +// GetValue Return the current value of the input func (i *InputModal) GetValue() string { return i.input.GetValue() } + +// SetValue Sets the value of the input to s func (i *InputModal) SetValue(s string) *InputModal { i.input.SetValue(s) return i } +// Clear Resets all non-positional parameters of the modal func (i *InputModal) Clear() *InputModal { i.title = "" i.text = "" i.input.SetValue("") - i.is_done = false - i.is_visible = false + i.isDone = false + i.isVisible = false return i } +// HandleKeyPress Handle the termbox event, return true if it was consumed func (i *InputModal) HandleKeyPress(event termbox.Event) bool { if event.Key == termbox.KeyEnter { // Done editing - i.is_done = true + i.isDone = true return true - } else { - return i.input.HandleKeyPress(event) } + return i.input.HandleKeyPress(event) } +// Draw Draw the modal func (i *InputModal) Draw() { - if i.is_visible { + if i.isVisible { // First blank out the area we'll be putting the modal FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) - next_y := i.y + 1 + nextY := i.y + 1 // The title if i.title != "" { if len(i.title) > i.width { diff := i.width - len(i.title) - DrawStringAtPoint(i.title[:len(i.title)+diff-1], i.x+1, next_y, i.fg, i.bg) + DrawStringAtPoint(i.title[:len(i.title)+diff-1], i.x+1, nextY, i.fg, i.bg) } else { - DrawStringAtPoint(i.title, i.x+1, next_y, i.fg, i.bg) + DrawStringAtPoint(i.title, i.x+1, nextY, i.fg, i.bg) } - next_y += 1 - FillWithChar('-', i.x+1, next_y, i.x+i.width-1, next_y, i.fg, i.bg) - next_y += 1 + nextY++ + FillWithChar('-', i.x+1, nextY, i.x+i.width-1, nextY, i.fg, i.bg) + nextY++ } if i.text != "" { - DrawStringAtPoint(i.text, i.x+1, next_y, i.fg, i.bg) - next_y += 1 + DrawStringAtPoint(i.text, i.x+1, nextY, i.fg, i.bg) + nextY++ } - i.input.SetY(next_y) + i.input.SetY(nextY) i.input.Draw() - next_y += 3 - if i.show_help { - help_string := " (ENTER) to Accept. (ESC) to Cancel. " - help_x := (i.x + i.width - len(help_string)) - 1 - DrawStringAtPoint(help_string, help_x, next_y, i.fg, i.bg) + nextY += 3 + if i.showHelp { + helpString := " (ENTER) to Accept. (ESC) to Cancel. " + helpX := (i.x + i.width - len(helpString)) - 1 + DrawStringAtPoint(helpString, helpX, nextY, i.fg, i.bg) } // Now draw the border DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) diff --git a/termbox_util.go b/termbox_util.go index 0041990..fd69c31 100644 --- a/termbox_util.go +++ b/termbox_util.go @@ -1,29 +1,37 @@ -package termbox_util +package termboxUtil import ( "fmt" - "github.com/nsf/termbox-go" "strings" + + "github.com/nsf/termbox-go" ) +// TextAlignment is an int value for how we're aligning text type TextAlignment int const ( - ALIGN_LEFT = iota - ALIGN_CENTER - ALIGN_RIGHT + // AlignLeft Aligns text to the left + AlignLeft = iota + // AlignCenter Aligns text to the center + AlignCenter + // AlignRight Aligns text to the right + AlignRight ) /* Basic Output Helpers */ + +// DrawStringAtPoint Draw a string of text at x, y with foreground color fg, background color bg func DrawStringAtPoint(str string, x int, y int, fg termbox.Attribute, bg termbox.Attribute) (int, int) { - x_pos := x + xPos := x for _, runeValue := range str { - termbox.SetCell(x_pos, y, runeValue, fg, bg) - x_pos++ + termbox.SetCell(xPos, y, runeValue, fg, bg) + xPos++ } - return x_pos, y + return xPos, y } +// FillWithChar Fills from x1,y1 through x2,y2 with the rune r, foreground color fg, background bg func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) { for xx := x1; xx <= x2; xx++ { for yx := y1; yx <= y2; yx++ { @@ -32,6 +40,7 @@ func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.A } } +// DrawBorder Draw a border around the area inside x1,y1 -> x2, y2 func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) { termbox.SetCell(x1, y1, '┌', fg, bg) FillWithChar('─', x1+1, y1, x2-1, y1, fg, bg) @@ -45,23 +54,23 @@ func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) termbox.SetCell(x2, y2, '┘', fg, bg) } +// AlignText Aligns the text txt within width characters using the specified alignment func AlignText(txt string, width int, align TextAlignment) string { - num_spaces := width - len(txt) + numSpaces := width - len(txt) switch align { - case ALIGN_CENTER: - if num_spaces/2 > 0 { + case AlignCenter: + if numSpaces/2 > 0 { return fmt.Sprintf("%s%s%s", - strings.Repeat(" ", num_spaces/2), - txt, strings.Repeat(" ", num_spaces/2), + strings.Repeat(" ", numSpaces/2), + txt, strings.Repeat(" ", numSpaces/2), ) - } else { - return txt } - case ALIGN_RIGHT: - return fmt.Sprintf("%s%s", strings.Repeat(" ", num_spaces), txt) + return txt + case AlignRight: + return fmt.Sprintf("%s%s", strings.Repeat(" ", numSpaces), txt) default: - return fmt.Sprintf("%s%s", txt, strings.Repeat(" ", num_spaces)) + return fmt.Sprintf("%s%s", txt, strings.Repeat(" ", numSpaces)) } } -/* More advanced things are in their respective files*/ +/* More advanced things are in their respective files */ From da19f61603efef6944f4d9e769bb3d27e7caad21 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 26 Oct 2015 07:39:18 -0500 Subject: [PATCH 11/27] Added ASCIIArt object and Menu Object --- termbox_asciiart.go | 85 +++++++++++++++++ termbox_menu.go | 222 ++++++++++++++++++++++++++++++++++++++++++++ termbox_util.go | 12 +-- 3 files changed, 313 insertions(+), 6 deletions(-) create mode 100644 termbox_asciiart.go create mode 100644 termbox_menu.go diff --git a/termbox_asciiart.go b/termbox_asciiart.go new file mode 100644 index 0000000..9b60d11 --- /dev/null +++ b/termbox_asciiart.go @@ -0,0 +1,85 @@ +package termboxUtil + +import ( + "github.com/nsf/termbox-go" +) + +// ASCIIArt is a []string with more functions +type ASCIIArt struct { + contents []string + x, y int + bg, fg termbox.Attribute +} + +// CreateASCIIArt Create an ASCII art object from a string slice +func CreateASCIIArt(c []string, x, y int, fg, bg termbox.Attribute) *ASCIIArt { + i := ASCIIArt{contents: c, x: x, y: y, fg: fg, bg: bg} + return &i +} + +// GetX Return the x position of the modal +func (i *ASCIIArt) GetX() int { return i.x } + +// SetX set the x position of the modal to x +func (i *ASCIIArt) SetX(x int) *ASCIIArt { + i.x = x + return i +} + +// GetY Return the y position of the modal +func (i *ASCIIArt) GetY() int { return i.y } + +// SetY Set the y position of the modal to y +func (i *ASCIIArt) SetY(y int) *ASCIIArt { + i.y = y + return i +} + +// GetBackground Return the current background color of the modal +func (i *ASCIIArt) GetBackground() termbox.Attribute { return i.bg } + +// SetBackground Set the current background color to bg +func (i *ASCIIArt) SetBackground(bg termbox.Attribute) *ASCIIArt { + i.bg = bg + return i +} + +// GetForeground Return the current foreground color +func (i *ASCIIArt) GetForeground() termbox.Attribute { return i.fg } + +// SetForeground Set the foreground color to fg +func (i *ASCIIArt) SetForeground(fg termbox.Attribute) *ASCIIArt { + i.fg = fg + return i +} + +// Align Align the Ascii art over width width with alignment a +func (i *ASCIIArt) Align(a TextAlignment, width int) *ASCIIArt { + // First get the width of the longest string in the slice + var newContents []string + incomingLength := 0 + for _, line := range i.contents { + if len(line) > incomingLength { + incomingLength = len(line) + } + } + for _, line := range i.contents { + newContents = append(newContents, AlignText(AlignText(line, incomingLength, AlignLeft), width, a)) + } + i.contents = newContents + return i +} + +// HandleKeyPress accepts the termbox event and returns whether it was consumed +func (i *ASCIIArt) HandleKeyPress(event termbox.Event) bool { + return false +} + +// Draw outputs the input field on the screen +func (i *ASCIIArt) Draw() { + drawX, drawY := i.x, i.y + for _, line := range i.contents { + DrawStringAtPoint(line, drawX, drawY, i.fg, i.bg) + drawY++ + } +} diff --git a/termbox_menu.go b/termbox_menu.go new file mode 100644 index 0000000..0c54a1c --- /dev/null +++ b/termbox_menu.go @@ -0,0 +1,222 @@ +package termboxUtil + +import ( + "github.com/nsf/termbox-go" +) + +// Menu is a menu with a list of options +type Menu struct { + title string + options []string + // If height is -1, then it is adaptive to the menu + x, y, width, height int + optionsDisabled []bool + optionsHelp []string + showHelp bool + cursor int + bg, fg termbox.Attribute + selectedBg, selectedFg termbox.Attribute + disabledBg, disabledFg termbox.Attribute + isDone bool + selectedOption int + bordered bool + hasFocus bool +} + +// CreateMenu Creates a menu with the specified attributes +func CreateMenu(title string, options []string, x, y, width, height int, fg, bg termbox.Attribute) *Menu { + i := Menu{title: title, options: options, x: x, y: y, width: width, height: height, fg: fg, bg: bg} + for len(i.optionsDisabled) < len(i.options) { + i.optionsDisabled = append(i.optionsDisabled, false) + } + i.selectedFg = i.bg + i.selectedBg = i.fg + i.disabledFg = i.bg + i.disabledBg = i.bg + return &i +} + +// GetTitle returns the current title of the menu +func (i *Menu) GetTitle() string { return i.title } + +// SetTitle sets the current title of the menu to s +func (i *Menu) SetTitle(s string) *Menu { + i.title = s + return i +} + +// GetOptions returns the current options of the menu +func (i *Menu) GetOptions() []string { + return i.options +} + +// SetOptions set the menu's options to opts +func (i *Menu) SetOptions(opts []string) *Menu { + i.options = opts + return i +} + +// SetOptionDisabled sets an option in the menu to disabled +func (i *Menu) SetOptionDisabled(idx int) *Menu { + if idx > 0 && idx < len(i.options) { + i.optionsDisabled[idx] = true + } + return i +} + +// SetOptionEnabled sets an option to enabled +func (i *Menu) SetOptionEnabled(idx int) *Menu { + if idx >= 0 && idx < len(i.options) { + i.optionsDisabled[idx] = false + } + return i +} + +// GetX returns the current x coordinate of the menu +func (i *Menu) GetX() int { return i.x } + +// SetX sets the current x coordinate of the menu to x +func (i *Menu) SetX(x int) *Menu { + i.x = x + return i +} + +// GetY returns the current y coordinate of the menu +func (i *Menu) GetY() int { return i.y } + +// SetY sets the current y coordinate of the menu to y +func (i *Menu) SetY(y int) *Menu { + i.y = y + return i +} + +// GetWidth returns the current width of the menu +func (i *Menu) GetWidth() int { return i.width } + +// SetWidth sets the current menu width to width +func (i *Menu) SetWidth(width int) *Menu { + i.width = width + return i +} + +// GetHeight returns the current height of the menu +func (i *Menu) GetHeight() int { return i.height } + +// SetHeight set the height of the menu to height +func (i *Menu) SetHeight(height int) *Menu { + i.height = height + return i +} + +// HelpIsShown returns true or false if the help is displayed +func (i *Menu) HelpIsShown() bool { return i.showHelp } + +// ShowHelp sets whether or not to display the help text +func (i *Menu) ShowHelp(b bool) *Menu { + i.showHelp = b + return i +} + +// GetBackground returns the current background color +func (i *Menu) GetBackground() termbox.Attribute { return i.bg } + +// SetBackground sets the background color to bg +func (i *Menu) SetBackground(bg termbox.Attribute) *Menu { + i.bg = bg + return i +} + +// GetForeground returns the current foreground color +func (i *Menu) GetForeground() termbox.Attribute { return i.fg } + +// SetForeground sets the current foreground color to fg +func (i *Menu) SetForeground(fg termbox.Attribute) *Menu { + i.fg = fg + return i +} + +// IsDone returns whether the user has answered the modal +func (i *Menu) IsDone() bool { return i.isDone } + +// SetDone sets whether the modal has completed it's purpose +func (i *Menu) SetDone(b bool) *Menu { + i.isDone = b + return i +} + +// IsBordered returns true or false if this menu has a border +func (i *Menu) IsBordered() bool { return i.bordered } + +// SetBordered sets whether we render a border around the menu +func (i *Menu) SetBordered(b bool) *Menu { + i.bordered = b + return i +} + +// HandleKeyPress handles the termbox event and returns whether it was consumed +func (i *Menu) HandleKeyPress(event termbox.Event) bool { + if i.hasFocus { + if event.Key == termbox.KeyEnter { + i.isDone = true + return true + } + switch event.Key { + case termbox.KeyArrowUp: + if i.selectedOption > 0 { + i.selectedOption-- + return true + } + case termbox.KeyArrowDown: + if i.selectedOption < len(i.options) { + i.selectedOption++ + return true + } + } + } + return false +} + +// Draw draws the modal +func (i *Menu) Draw() { + // First blank out the area we'll be putting the menu + FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + // Now draw the border + optionStartX := i.x + optionStartY := i.y + optionWidth := i.width + optionHeight := i.height + if optionHeight == -1 { + optionHeight = len(i.options) + } + if i.bordered { + if i.height == -1 { + DrawBorder(i.x, i.y, i.x+i.width, i.y+optionHeight+1, i.fg, i.bg) + } else { + DrawBorder(i.x, i.y, i.x+i.width, i.y+optionHeight, i.fg, i.bg) + } + optionStartX = i.x + 1 + optionStartY = i.y + 1 + optionWidth = i.width - 2 + } + + // The title + if i.title != "" { + DrawStringAtPoint(AlignText(i.title, optionWidth, AlignCenter), optionStartX, optionStartY, i.fg, i.bg) + optionStartY++ + if i.bordered { + FillWithChar('-', optionStartX, optionStartY, optionWidth, optionStartY, i.fg, i.bg) + optionStartY++ + } + } + + // Print the options + for idx, opt := range i.options { + if i.optionsDisabled[idx] { + DrawStringAtPoint(opt, optionStartX, optionStartY, i.disabledFg, i.disabledBg) + } else if i.selectedOption == idx { + DrawStringAtPoint(opt, optionStartX, optionStartY, i.selectedFg, i.selectedBg) + } else { + DrawStringAtPoint(opt, optionStartX, optionStartY, i.fg, i.bg) + } + } +} diff --git a/termbox_util.go b/termbox_util.go index fd69c31..67ecb3f 100644 --- a/termbox_util.go +++ b/termbox_util.go @@ -42,16 +42,16 @@ func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.A // DrawBorder Draw a border around the area inside x1,y1 -> x2, y2 func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) { - termbox.SetCell(x1, y1, '┌', fg, bg) - FillWithChar('─', x1+1, y1, x2-1, y1, fg, bg) - termbox.SetCell(x2, y1, '┐', fg, bg) + termbox.SetCell(x1, y1, '+', fg, bg) + FillWithChar('-', x1+1, y1, x2-1, y1, fg, bg) + termbox.SetCell(x2, y1, '+', fg, bg) FillWithChar('|', x1, y1+1, x1, y2-1, fg, bg) FillWithChar('|', x2, y1+1, x2, y2-1, fg, bg) - termbox.SetCell(x1, y2, '└', fg, bg) - FillWithChar('─', x1+1, y2, x2-1, y2, fg, bg) - termbox.SetCell(x2, y2, '┘', fg, bg) + termbox.SetCell(x1, y2, '+', fg, bg) + FillWithChar('-', x1+1, y2, x2-1, y2, fg, bg) + termbox.SetCell(x2, y2, '+', fg, bg) } // AlignText Aligns the text txt within width characters using the specified alignment From 02537f2b96dab8cae8e4044cf2b8fdd81d8ba59e Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 26 Oct 2015 17:38:49 -0500 Subject: [PATCH 12/27] Updated a few things Add a few handy things to asciiart and menu --- termbox_asciiart.go | 24 +++++ termbox_menu.go | 248 ++++++++++++++++++++++++++++++++++---------- termbox_util.go | 5 +- 3 files changed, 221 insertions(+), 56 deletions(-) diff --git a/termbox_asciiart.go b/termbox_asciiart.go index 9b60d11..d86855b 100644 --- a/termbox_asciiart.go +++ b/termbox_asciiart.go @@ -35,6 +35,30 @@ func (i *ASCIIArt) SetY(y int) *ASCIIArt { return i } +// GetHeight Returns the number of strings in the contents slice +func (i *ASCIIArt) GetHeight() int { + return len(i.contents) +} + +// SetContents Sets the contents of i to c +func (i *ASCIIArt) SetContents(c []string) *ASCIIArt { + i.contents = c + return i +} + +// GetContents returns the ascii art +func (i *ASCIIArt) GetContents() []string { + return i.contents +} + +// SetContentLine Sets a specific line of the contents to s +func (i *ASCIIArt) SetContentLine(s string, idx int) *ASCIIArt { + if idx >= 0 && idx < len(i.contents) { + i.contents[idx] = s + } + return i +} + // GetBackground Return the current background color of the modal func (i *ASCIIArt) GetBackground() termbox.Attribute { return i.bg } diff --git a/termbox_menu.go b/termbox_menu.go index 0c54a1c..70b888f 100644 --- a/termbox_menu.go +++ b/termbox_menu.go @@ -1,38 +1,36 @@ package termboxUtil -import ( - "github.com/nsf/termbox-go" -) +import "github.com/nsf/termbox-go" // Menu is a menu with a list of options type Menu struct { title string - options []string + options []MenuOption // If height is -1, then it is adaptive to the menu x, y, width, height int - optionsDisabled []bool - optionsHelp []string showHelp bool cursor int bg, fg termbox.Attribute selectedBg, selectedFg termbox.Attribute disabledBg, disabledFg termbox.Attribute isDone bool - selectedOption int bordered bool - hasFocus bool } // CreateMenu Creates a menu with the specified attributes func CreateMenu(title string, options []string, x, y, width, height int, fg, bg termbox.Attribute) *Menu { - i := Menu{title: title, options: options, x: x, y: y, width: width, height: height, fg: fg, bg: bg} - for len(i.optionsDisabled) < len(i.options) { - i.optionsDisabled = append(i.optionsDisabled, false) + i := Menu{ + title: title, + x: x, y: y, width: width, height: height, + fg: fg, bg: bg, selectedFg: bg, selectedBg: fg, + disabledFg: bg, disabledBg: bg, + } + for _, line := range options { + i.options = append(i.options, MenuOption{text: line}) + } + if len(i.options) > 0 { + i.SetSelectedOption(&i.options[0]) } - i.selectedFg = i.bg - i.selectedBg = i.fg - i.disabledFg = i.bg - i.disabledBg = i.bg return &i } @@ -46,30 +44,23 @@ func (i *Menu) SetTitle(s string) *Menu { } // GetOptions returns the current options of the menu -func (i *Menu) GetOptions() []string { +func (i *Menu) GetOptions() []MenuOption { return i.options } // SetOptions set the menu's options to opts -func (i *Menu) SetOptions(opts []string) *Menu { +func (i *Menu) SetOptions(opts []MenuOption) *Menu { i.options = opts return i } -// SetOptionDisabled sets an option in the menu to disabled -func (i *Menu) SetOptionDisabled(idx int) *Menu { - if idx > 0 && idx < len(i.options) { - i.optionsDisabled[idx] = true +// SetOptionsFromStrings sets the options of this menu from a slice of strings +func (i *Menu) SetOptionsFromStrings(opts []string) *Menu { + var newOpts []MenuOption + for _, v := range opts { + newOpts = append(newOpts, *CreateOptionFromText(v)) } - return i -} - -// SetOptionEnabled sets an option to enabled -func (i *Menu) SetOptionEnabled(idx int) *Menu { - if idx >= 0 && idx < len(i.options) { - i.optionsDisabled[idx] = false - } - return i + return i.SetOptions(newOpts) } // GetX returns the current x coordinate of the menu @@ -108,6 +99,85 @@ func (i *Menu) SetHeight(height int) *Menu { return i } +// GetSelectedOption returns the current selected option +func (i *Menu) GetSelectedOption() *MenuOption { + idx := i.GetSelectedIndex() + if idx != -1 { + return &i.options[idx] + } + return nil +} + +// GetOptionFromIndex Returns the +func (i *Menu) GetOptionFromIndex(idx int) *MenuOption { + if idx >= 0 && idx < len(i.options) { + return &i.options[idx] + } + return nil +} + +// GetOptionFromText Returns the first option with the text v +func (i *Menu) GetOptionFromText(v string) *MenuOption { + for idx := range i.options { + testOption := &i.options[idx] + if testOption.GetText() == v { + return testOption + } + } + return nil +} + +// GetSelectedIndex returns the index of the selected option +// Returns -1 if nothing is selected +func (i *Menu) GetSelectedIndex() int { + for idx := range i.options { + if i.options[idx].IsSelected() { + return idx + } + } + return -1 +} + +// SetSelectedOption sets the current selected option to v (if it's valid) +func (i *Menu) SetSelectedOption(v *MenuOption) *Menu { + for idx := range i.options { + if &i.options[idx] == v { + i.options[idx].Select() + } else { + i.options[idx].Unselect() + } + } + return i +} + +// SelectPrevOption Decrements the selected option (if it can) +func (i *Menu) SelectPrevOption() *Menu { + idx := i.GetSelectedIndex() + for idx >= 0 { + idx-- + testOption := i.GetOptionFromIndex(idx) + if testOption != nil && !testOption.IsDisabled() { + i.SetSelectedOption(testOption) + return i + } + } + return i +} + +// SelectNextOption Increments the selected option (if it can) +func (i *Menu) SelectNextOption() *Menu { + idx := i.GetSelectedIndex() + for idx < len(i.options) { + idx++ + testOption := i.GetOptionFromIndex(idx) + if testOption != nil && !testOption.IsDisabled() { + i.SetSelectedOption(testOption) + return i + } + } + return i +} + // HelpIsShown returns true or false if the help is displayed func (i *Menu) HelpIsShown() bool { return i.showHelp } @@ -155,23 +225,19 @@ func (i *Menu) SetBordered(b bool) *Menu { // HandleKeyPress handles the termbox event and returns whether it was consumed func (i *Menu) HandleKeyPress(event termbox.Event) bool { - if i.hasFocus { - if event.Key == termbox.KeyEnter { - i.isDone = true - return true - } - switch event.Key { - case termbox.KeyArrowUp: - if i.selectedOption > 0 { - i.selectedOption-- - return true - } - case termbox.KeyArrowDown: - if i.selectedOption < len(i.options) { - i.selectedOption++ - return true - } - } + if event.Key == termbox.KeyEnter { + i.isDone = true + return true + } + currentIdx := i.GetSelectedIndex() + switch event.Key { + case termbox.KeyArrowUp: + i.SelectPrevOption() + case termbox.KeyArrowDown: + i.SelectNextOption() + } + if i.GetSelectedIndex() != currentIdx { + return true } return false } @@ -196,7 +262,7 @@ func (i *Menu) Draw() { } optionStartX = i.x + 1 optionStartY = i.y + 1 - optionWidth = i.width - 2 + optionWidth = i.width - 1 } // The title @@ -210,13 +276,85 @@ func (i *Menu) Draw() { } // Print the options - for idx, opt := range i.options { - if i.optionsDisabled[idx] { - DrawStringAtPoint(opt, optionStartX, optionStartY, i.disabledFg, i.disabledBg) - } else if i.selectedOption == idx { - DrawStringAtPoint(opt, optionStartX, optionStartY, i.selectedFg, i.selectedBg) - } else { - DrawStringAtPoint(opt, optionStartX, optionStartY, i.fg, i.bg) + if len(i.options) > 0 { + for idx := range i.options { + currOpt := &i.options[idx] + if currOpt.IsDisabled() { + DrawStringAtPoint(currOpt.GetText(), optionStartX, optionStartY, i.disabledFg, i.disabledBg) + } else if i.GetSelectedOption() == currOpt { + DrawStringAtPoint(AlignText(currOpt.GetText(), optionWidth, AlignLeft), optionStartX, optionStartY, i.selectedFg, i.selectedBg) + } else { + DrawStringAtPoint(currOpt.GetText(), optionStartX, optionStartY, i.fg, i.bg) + } + optionStartY++ } } } + +/* MenuOption Struct & methods */ + +// MenuOption An option in the menu +type MenuOption struct { + text string + selected bool + disabled bool + helpText string +} + +// CreateOptionFromText just returns a MenuOption object +// That only has it's text value set. +func CreateOptionFromText(s string) *MenuOption { + return &MenuOption{text: s} +} + +// SetText Sets the text for this option +func (i *MenuOption) SetText(s string) *MenuOption { + i.text = s + return i +} + +// GetText Returns the text for this option +func (i *MenuOption) GetText() string { return i.text } + +// Disable Sets this option to disabled +func (i *MenuOption) Disable() *MenuOption { + i.disabled = true + return i +} + +// Enable Sets this option to enabled +func (i *MenuOption) Enable() *MenuOption { + i.disabled = false + return i +} + +// IsDisabled returns whether this option is enabled +func (i *MenuOption) IsDisabled() bool { + return i.disabled +} + +// IsSelected Returns whether this option is selected +func (i *MenuOption) IsSelected() bool { + return i.selected +} + +// Select Sets this option to selected +func (i *MenuOption) Select() *MenuOption { + i.selected = true + return i +} + +// Unselect Sets this option to not selected +func (i *MenuOption) Unselect() *MenuOption { + i.selected = false + return i +} + +// SetHelpText Sets this option's help text to s +func (i *MenuOption) SetHelpText(s string) *MenuOption { + i.helpText = s + return i +} + +// GetHelpText Returns the help text for this option +func (i *MenuOption) GetHelpText() string { return i.helpText } diff --git a/termbox_util.go b/termbox_util.go index 67ecb3f..db05a84 100644 --- a/termbox_util.go +++ b/termbox_util.go @@ -69,7 +69,10 @@ func AlignText(txt string, width int, align TextAlignment) string { case AlignRight: return fmt.Sprintf("%s%s", strings.Repeat(" ", numSpaces), txt) default: - return fmt.Sprintf("%s%s", txt, strings.Repeat(" ", numSpaces)) + if numSpaces >= 0 { + return fmt.Sprintf("%s%s", txt, strings.Repeat(" ", numSpaces)) + } + return txt } } From 9ca08880003de21ebab38c879f0bad915c6fda25 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 30 Nov 2015 08:21:22 -0600 Subject: [PATCH 13/27] Add Progress Bars Also, add vim navigation to menus --- termbox_menu.go | 35 +++++++ termbox_progressbar.go | 224 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 259 insertions(+) create mode 100644 termbox_progressbar.go diff --git a/termbox_menu.go b/termbox_menu.go index 70b888f..44e7485 100644 --- a/termbox_menu.go +++ b/termbox_menu.go @@ -15,6 +15,7 @@ type Menu struct { disabledBg, disabledFg termbox.Attribute isDone bool bordered bool + vimMode bool } // CreateMenu Creates a menu with the specified attributes @@ -178,6 +179,20 @@ func (i *Menu) SelectNextOption() *Menu { return i } +// SetOptionDisabled Disables the specified option +func (i *Menu) SetOptionDisabled(idx int) { + if len(i.options) > idx { + i.GetOptionFromIndex(idx).Disable() + } +} + +// SetOptionEnabled Enables the specified option +func (i *Menu) SetOptionEnabled(idx int) { + if len(i.options) > idx { + i.GetOptionFromIndex(idx).Enable() + } +} + // HelpIsShown returns true or false if the help is displayed func (i *Menu) HelpIsShown() bool { return i.showHelp } @@ -223,6 +238,18 @@ func (i *Menu) SetBordered(b bool) *Menu { return i } +// EnableVimMode Enables h,j,k,l navigation +func (i *Menu) EnableVimMode() *Menu { + i.vimMode = true + return i +} + +// DisableVimMode Disables h,j,k,l navigation +func (i *Menu) DisableVimMode() *Menu { + i.vimMode = false + return i +} + // HandleKeyPress handles the termbox event and returns whether it was consumed func (i *Menu) HandleKeyPress(event termbox.Event) bool { if event.Key == termbox.KeyEnter { @@ -236,6 +263,14 @@ func (i *Menu) HandleKeyPress(event termbox.Event) bool { case termbox.KeyArrowDown: i.SelectNextOption() } + if i.vimMode { + switch event.Ch { + case 'j': + i.SelectNextOption() + case 'k': + i.SelectPrevOption() + } + } if i.GetSelectedIndex() != currentIdx { return true } diff --git a/termbox_progressbar.go b/termbox_progressbar.go new file mode 100644 index 0000000..afb0ed8 --- /dev/null +++ b/termbox_progressbar.go @@ -0,0 +1,224 @@ +package termboxUtil + +import "github.com/nsf/termbox-go" + +// ProgressBar Just contains the data needed to display a progress bar +type ProgressBar struct { + total int + progress int + allowOverflow bool + allowUnderflow bool + fullChar rune + emptyChar rune + bordered bool + alignment TextAlignment + + x, y int + width, height int + bg, fg termbox.Attribute +} + +// CreateProgressBar Create a progress bar object +func CreateProgressBar(tot, x, y int, fg, bg termbox.Attribute) *ProgressBar { + i := ProgressBar{total: tot, + fullChar: '#', emptyChar: ' ', + x: x, y: y, height: 1, width: 10, + bordered: true, fg: fg, bg: bg, + alignment: AlignLeft, + } + return &i +} + +// GetProgress returns the curret progress value +func (i *ProgressBar) GetProgress() int { + return i.progress +} + +// SetProgress sets the current progress of the bar +func (i *ProgressBar) SetProgress(p int) *ProgressBar { + if (p <= i.total || i.allowOverflow) || (p >= 0 || i.allowUnderflow) { + i.progress = p + } + return i +} + +// IncrProgress increments the current progress of the bar +func (i *ProgressBar) IncrProgress() *ProgressBar { + if i.progress < i.total || i.allowOverflow { + i.progress++ + } + return i +} + +// DecrProgress decrements the current progress of the bar +func (i *ProgressBar) DecrProgress() *ProgressBar { + if i.progress > 0 || i.allowUnderflow { + i.progress-- + } + return i +} + +// GetPercent returns the percent full of the bar +func (i *ProgressBar) GetPercent() int { + return int(float64(i.progress) / float64(i.total) * 100) +} + +// EnableOverflow Tells the progress bar that it can go over the total +func (i *ProgressBar) EnableOverflow() *ProgressBar { + i.allowOverflow = true + return i +} + +// DisableOverflow Tells the progress bar that it can NOT go over the total +func (i *ProgressBar) DisableOverflow() *ProgressBar { + i.allowOverflow = false + return i +} + +// EnableUnderflow Tells the progress bar that it can go below zero +func (i *ProgressBar) EnableUnderflow() *ProgressBar { + i.allowUnderflow = true + return i +} + +// DisableUnderflow Tells the progress bar that it can NOT go below zero +func (i *ProgressBar) DisableUnderflow() *ProgressBar { + i.allowUnderflow = false + return i +} + +// GetFullChar returns the rune used for 'full' +func (i *ProgressBar) GetFullChar() rune { + return i.fullChar +} + +// SetFullChar sets the rune used for 'full' +func (i *ProgressBar) SetFullChar(f rune) *ProgressBar { + i.fullChar = f + return i +} + +// GetEmptyChar gets the rune used for 'empty' +func (i *ProgressBar) GetEmptyChar() rune { + return i.emptyChar +} + +// SetEmptyChar sets the rune used for 'empty' +func (i *ProgressBar) SetEmptyChar(f rune) *ProgressBar { + i.emptyChar = f + return i +} + +// GetX Return the x position of the Progress Bar +func (i *ProgressBar) GetX() int { return i.x } + +// SetX set the x position of the ProgressBar to x +func (i *ProgressBar) SetX(x int) *ProgressBar { + i.x = x + return i +} + +// GetY Return the y position of the ProgressBar +func (i *ProgressBar) GetY() int { return i.y } + +// SetY Set the y position of the ProgressBar to y +func (i *ProgressBar) SetY(y int) *ProgressBar { + i.y = y + return i +} + +// GetHeight returns the height of the progress bar +// Defaults to 1 (3 if bordered) +func (i *ProgressBar) GetHeight() int { + return i.height +} + +// SetHeight Sets the height of the progress bar +func (i *ProgressBar) SetHeight(h int) *ProgressBar { + i.height = h + return i +} + +// GetWidth returns the width of the progress bar +func (i *ProgressBar) GetWidth() int { + return i.width +} + +// SetWidth Sets the width of the progress bar +func (i *ProgressBar) SetWidth(w int) *ProgressBar { + i.width = w + return i +} + +// GetBackground Return the current background color of the modal +func (i *ProgressBar) GetBackground() termbox.Attribute { return i.bg } + +// SetBackground Set the current background color to bg +func (i *ProgressBar) SetBackground(bg termbox.Attribute) *ProgressBar { + i.bg = bg + return i +} + +// GetForeground Return the current foreground color +func (i *ProgressBar) GetForeground() termbox.Attribute { return i.fg } + +// SetForeground Set the foreground color to fg +func (i *ProgressBar) SetForeground(fg termbox.Attribute) *ProgressBar { + i.fg = fg + return i +} + +// Align Tells which direction the progress bar empties +func (i *ProgressBar) Align(a TextAlignment) *ProgressBar { + i.alignment = a + return i +} + +// HandleKeyPress accepts the termbox event and returns whether it was consumed +func (i *ProgressBar) HandleKeyPress(event termbox.Event) bool { + return false +} + +// Draw outputs the input field on the screen +func (i *ProgressBar) Draw() { + // For now, just draw a [#### ] bar + // TODO: make this more advanced + drawX, drawY := i.x, i.y + fillWidth, fillHeight := i.width-2, i.height + DrawStringAtPoint("[", drawX, drawY, i.fg, i.bg) + numFull := int(float64(fillWidth) * float64(i.progress) / float64(i.total)) + FillWithChar(i.fullChar, drawX+1, drawY, drawX+1+numFull, drawY+(fillHeight-1), i.fg, i.bg) + DrawStringAtPoint("]", drawX+i.width-1, drawY, i.fg, i.bg) + + /* + drawX, drawY := i.x, i.y + drawWidth, drawHeight := i.width, i.height + if i.bordered { + if i.height == 1 && i.width > 2 { + // Just using [ & ] for the border + DrawStringAtPoint("[", drawX, drawY, i.fg, i.bg) + DrawStringAtPoint("]", drawX+i.width-1, drawY, i.fg, i.bg) + drawX++ + drawWidth -= 2 + } else if i.height >= 3 { + DrawBorder(drawX, drawY, drawX+i.width, drawY+i.height, i.fg, i.bg) + drawX++ + drawY++ + drawWidth -= 2 + drawHeight -= 2 + } + } + + // Figure out how many chars are full + numFull := drawWidth * (i.progress / i.total) + switch i.alignment { + case AlignRight: // TODO: Fill from right to left + case AlignCenter: // TODO: Fill from middle out + default: // Fill from left to right + FillWithChar(i.fullChar, drawX, drawY, drawX+numFull, drawY+(drawHeight-1), i.fg, i.bg) + if numFull < drawWidth { + FillWithChar(i.emptyChar, drawX+numFull, drawY, drawX+drawWidth-1, drawY+(drawHeight-1), i.fg, i.bg) + } + } + */ +} From 1c468454f6011c29b62a628c2889f1f815024157 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 17 Dec 2015 21:41:02 -0600 Subject: [PATCH 14/27] Colorized Progress Bar --- termbox_progressbar.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/termbox_progressbar.go b/termbox_progressbar.go index afb0ed8..0cbd3e0 100644 --- a/termbox_progressbar.go +++ b/termbox_progressbar.go @@ -12,6 +12,7 @@ type ProgressBar struct { emptyChar rune bordered bool alignment TextAlignment + colorized bool x, y int width, height int @@ -174,6 +175,16 @@ func (i *ProgressBar) Align(a TextAlignment) *ProgressBar { return i } +// SetColorized sets whether the progress bar should be colored +// depending on how full it is: +// 10% - Red +// 50% - Yellow +// 80% - Green +func (i *ProgressBar) SetColorized(c bool) *ProgressBar { + i.colorized = c + return i +} + // HandleKeyPress accepts the termbox event and returns whether it was consumed func (i *ProgressBar) HandleKeyPress(event termbox.Event) bool { return false @@ -183,11 +194,21 @@ func (i *ProgressBar) HandleKeyPress(event termbox.Event) bool { func (i *ProgressBar) Draw() { // For now, just draw a [#### ] bar // TODO: make this more advanced + useFg := i.fg + if i.colorized { + if i.GetPercent() < 10 { + useFg = termbox.ColorRed + } else if i.GetPercent() < 50 { + useFg = termbox.ColorYellow + } else { + useFg = termbox.ColorGreen + } + } drawX, drawY := i.x, i.y fillWidth, fillHeight := i.width-2, i.height DrawStringAtPoint("[", drawX, drawY, i.fg, i.bg) numFull := int(float64(fillWidth) * float64(i.progress) / float64(i.total)) - FillWithChar(i.fullChar, drawX+1, drawY, drawX+1+numFull, drawY+(fillHeight-1), i.fg, i.bg) + FillWithChar(i.fullChar, drawX+1, drawY, drawX+1+numFull, drawY+(fillHeight-1), useFg, i.bg) DrawStringAtPoint("]", drawX+i.width-1, drawY, i.fg, i.bg) /* From 95d2a88a7c1e26247900227ec1a44747ad593d34 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sat, 26 Dec 2015 10:46:18 -0600 Subject: [PATCH 15/27] Added alpha-numeric input tester --- termbox_util.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/termbox_util.go b/termbox_util.go index db05a84..55d5072 100644 --- a/termbox_util.go +++ b/termbox_util.go @@ -19,6 +19,18 @@ const ( AlignRight ) +/* Basic Input Helpers */ + +// KeyIsAlphaNumeric Returns whether the termbox event is an +// Alpha-Numeric Key Press +func KeyIsAlphaNumeric(event termbox.Event) { + k := event.Ch + if (k >= 'a' && k <= 'z') || (k >= 'A' && k <= 'Z') || (k >= '0' && k <= '9') { + return true + } + return false +} + /* Basic Output Helpers */ // DrawStringAtPoint Draw a string of text at x, y with foreground color fg, background color bg From c5ed6dc0bafb53f246adbc594aa20a17e8252a14 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 28 Dec 2015 22:19:08 -0600 Subject: [PATCH 16/27] Adding Alpha-Numeric Tester --- termbox_util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/termbox_util.go b/termbox_util.go index 55d5072..80d6426 100644 --- a/termbox_util.go +++ b/termbox_util.go @@ -23,7 +23,7 @@ const ( // KeyIsAlphaNumeric Returns whether the termbox event is an // Alpha-Numeric Key Press -func KeyIsAlphaNumeric(event termbox.Event) { +func KeyIsAlphaNumeric(event termbox.Event) bool { k := event.Ch if (k >= 'a' && k <= 'z') || (k >= 'A' && k <= 'Z') || (k >= '0' && k <= '9') { return true From 9f978a3ec006c11e146bc3df47e07048a8c5c319 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Fri, 1 Jan 2016 12:15:09 -0600 Subject: [PATCH 17/27] Add Wrapping Flags to Input Fields --- termbox_inputfield.go | 35 +++++++++++++++++++++++++++++++++++ termbox_inputmodal.go | 6 ++++++ 2 files changed, 41 insertions(+) diff --git a/termbox_inputfield.go b/termbox_inputfield.go index f597ef8..ca072e8 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -13,6 +13,7 @@ type InputField struct { cursor int fg, bg termbox.Attribute bordered bool + wrap bool } // CreateInputField creates an input field at x, y that is w by h @@ -75,6 +76,16 @@ func (i *InputField) SetBordered(b bool) *InputField { return i } +// DoesWrap returns true or false if this input field wraps text +func (i *InputField) DoesWrap() bool { return i.wrap } + +// SetWrap sets whether we wrap the text at width. +// If 'wrap' is set, we automatically increase the height when we need to. +func (i *InputField) SetWrap(b bool) *InputField { + i.wrap = b + return i +} + // HandleKeyPress accepts the termbox event and returns whether it was consumed func (i *InputField) HandleKeyPress(event termbox.Event) bool { if event.Key == termbox.KeyEnter { @@ -145,6 +156,30 @@ func (i *InputField) Draw() { } else { strPt1, strPt2, cursorRune = "", "", ' ' } + // Check if the value is longer than the width + if len(i.value) > i.width { + if i.wrap { + // If we're wrapping the text, figure out how that goes + } else { + // Not wrapping, so figure out what we need to trim + // We have i.width/2 space for each strPt + if len(strPt1) > i.width/2 { + if len(strPt2) > i.width/2 { + // Both sides are too long, center the cursor + } else { + // Just side 1 is too long, figure out how much we can show + tmp := i.width - 1 + tmp -= len(strPt2) + strPt1 = strPt1[tmp:] + } + } else if len(strPt2) > i.width/2 { + // Just side 2 is too long, figure out how much we can show + tmp := i.width - 1 + tmp -= len(strPt1) + strPt2 = strPt2[:tmp] + } + } + } x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg) termbox.SetCell(x, y, cursorRune, i.bg, i.fg) DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 2a0ac23..ce3a92e 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -139,6 +139,12 @@ func (i *InputModal) SetValue(s string) *InputModal { return i } +// SetInputWrap sets whether the input field will wrap long text or not +func (i *InputModal) SetInputWrap(b bool) *InputModal { + i.input.SetWrap(b) + return i +} + // Clear Resets all non-positional parameters of the modal func (i *InputModal) Clear() *InputModal { i.title = "" From 99a0a449a19cc1cf32da70971ae77bafd26559e9 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sun, 3 Jan 2016 22:14:33 -0600 Subject: [PATCH 18/27] Still working on input field overflow --- termbox_inputfield.go | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/termbox_inputfield.go b/termbox_inputfield.go index ca072e8..34658d9 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -156,31 +156,27 @@ func (i *InputField) Draw() { } else { strPt1, strPt2, cursorRune = "", "", ' ' } + // strPt1, strPt2 = all of the text before, after the cursor + // cursorRune is the rune on the cursor // Check if the value is longer than the width - if len(i.value) > i.width { - if i.wrap { - // If we're wrapping the text, figure out how that goes - } else { - // Not wrapping, so figure out what we need to trim - // We have i.width/2 space for each strPt - if len(strPt1) > i.width/2 { - if len(strPt2) > i.width/2 { - // Both sides are too long, center the cursor - } else { - // Just side 1 is too long, figure out how much we can show - tmp := i.width - 1 - tmp -= len(strPt2) - strPt1 = strPt1[tmp:] - } - } else if len(strPt2) > i.width/2 { - // Just side 2 is too long, figure out how much we can show - tmp := i.width - 1 - tmp -= len(strPt1) - strPt2 = strPt2[:tmp] + maxWidth := i.width + if i.bordered { + maxWidth -= 2 + } + cursorRune2 := cursorRune + if len(i.value) > maxWidth { + var chopLeft bool + for len(strPt1)+len(strPt2)+1 > maxWidth { + if chopLeft && len(strPt1) > 0 { + strPt1 = strPt1[1:] + } else if !chopLeft && len(strPt2) > 0 { + strPt2 = strPt2[:len(strPt2)-1] } + chopLeft = !chopLeft } } x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg) - termbox.SetCell(x, y, cursorRune, i.bg, i.fg) + termbox.SetCell(x, y, cursorRune2, i.bg, i.fg) DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) + termbox.SetCell(x, y+1, cursorRune, i.bg, i.fg) } From 31b785172d1e3953a9461aa443979f114f1c6f30 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 4 Jan 2016 11:01:22 -0600 Subject: [PATCH 19/27] Starting work on Wrapping --- termbox_inputfield.go | 49 ++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/termbox_inputfield.go b/termbox_inputfield.go index 34658d9..83ce9f9 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -158,25 +158,50 @@ func (i *InputField) Draw() { } // strPt1, strPt2 = all of the text before, after the cursor // cursorRune is the rune on the cursor - // Check if the value is longer than the width maxWidth := i.width if i.bordered { - maxWidth -= 2 + maxWidth-- } - cursorRune2 := cursorRune - if len(i.value) > maxWidth { - var chopLeft bool + if i.wrap { + // Split the text into maxWidth chunks + x, y := i.x+1, i.y+1 + for len(strPt1) > 0 { + if len(strPt1) > maxWidth { + x, y = DrawStringAtPoint(strPt1[:maxWidth], x, y, i.fg, i.bg) + strPt1 = strPt1[maxWidth+1:] + continue + } + x, y = DrawStringAtPoint(strPt1, x, y, i.fg, i.bg) + } + if maxWidth-len(strPt1) <= 0 { + termbox.SetCell(x, y, cursorRune, i.bg, i.fg) + } + if maxWidth-len(strPt1)-1 > 0 { + DrawStringAtPoint(strPt2[:(maxWidth-len(strPt1)-1)], x+1, y, i.fg, i.bg) + strPt2 = strPt2[(maxWidth - len(strPt1)):] + } + for len(strPt2) > 0 { + if len(strPt2) > maxWidth { + x, y = DrawStringAtPoint(strPt2[:maxWidth], x, y, i.fg, i.bg) + strPt2 = strPt2[maxWidth+1:] + continue + } + x, y = DrawStringAtPoint(strPt2, x, y, i.fg, i.bg) + } + } else { + // Not wrapping, just adjust the viewport for len(strPt1)+len(strPt2)+1 > maxWidth { - if chopLeft && len(strPt1) > 0 { + if len(strPt1) >= len(strPt2) { + if len(strPt1) == 0 { + break + } strPt1 = strPt1[1:] - } else if !chopLeft && len(strPt2) > 0 { + } else { strPt2 = strPt2[:len(strPt2)-1] } - chopLeft = !chopLeft } + x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg) + termbox.SetCell(x, y, cursorRune, i.bg, i.fg) + DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) } - x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg) - termbox.SetCell(x, y, cursorRune2, i.bg, i.fg) - DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) - termbox.SetCell(x, y+1, cursorRune, i.bg, i.fg) } From 9026f1a1ffa0b24a122302c75f91a32f1103b868 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 4 Jan 2016 18:25:07 -0600 Subject: [PATCH 20/27] Working on Wrapping Still --- termbox_inputfield.go | 49 ++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/termbox_inputfield.go b/termbox_inputfield.go index 83ce9f9..17cc4af 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -2,6 +2,8 @@ package termboxUtil import ( "fmt" + "strconv" + "strings" "github.com/nsf/termbox-go" ) @@ -88,9 +90,7 @@ func (i *InputField) SetWrap(b bool) *InputField { // HandleKeyPress accepts the termbox event and returns whether it was consumed func (i *InputField) HandleKeyPress(event termbox.Event) bool { - if event.Key == termbox.KeyEnter { - // Done editing - } else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 { + if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 { if len(i.value) > 0 { i.value = i.value[:len(i.value)-1] } @@ -103,8 +103,8 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool { i.cursor++ } } else if event.Key == termbox.KeyCtrlU { - // Ctrl+U Clears the Input - i.value = "" + // Ctrl+U Clears the Input (before the cursor) + i.value = i.value[i.cursor:] } else { // Get the rune to add to our value. Space and Tab are special cases where // we can't use the event's rune directly @@ -114,6 +114,8 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool { ch = " " case termbox.KeyTab: ch = "\t" + case termbox.KeyEnter: + ch = "\n" default: ch = string(event.Ch) } @@ -162,29 +164,38 @@ func (i *InputField) Draw() { if i.bordered { maxWidth-- } + DrawStringAtPoint(strconv.Itoa(strings.Count(i.value, "\n")), i.x, i.y, i.fg, i.bg) if i.wrap { // Split the text into maxWidth chunks x, y := i.x+1, i.y+1 - for len(strPt1) > 0 { - if len(strPt1) > maxWidth { - x, y = DrawStringAtPoint(strPt1[:maxWidth], x, y, i.fg, i.bg) - strPt1 = strPt1[maxWidth+1:] - continue + nlCount := strings.Count(strPt1, "\n") + for len(strPt1) > maxWidth || nlCount > 0 { + nlIdx := strings.Index(strPt1, "\n") + breakAt := maxWidth + if nlIdx < maxWidth { + breakAt = nlIdx + 1 } - x, y = DrawStringAtPoint(strPt1, x, y, i.fg, i.bg) + x, y = DrawStringAtPoint(strPt1[:breakAt], x, y, i.fg, i.bg) + strPt1 = strPt1[breakAt:] + if len(strPt1) > 0 { + x = i.x + 1 + y++ + } + nlCount = strings.Count(strPt1, "\n") } + x, y = DrawStringAtPoint(strPt1, x, y, i.fg, i.bg) if maxWidth-len(strPt1) <= 0 { termbox.SetCell(x, y, cursorRune, i.bg, i.fg) } - if maxWidth-len(strPt1)-1 > 0 { - DrawStringAtPoint(strPt2[:(maxWidth-len(strPt1)-1)], x+1, y, i.fg, i.bg) - strPt2 = strPt2[(maxWidth - len(strPt1)):] - } - for len(strPt2) > 0 { - if len(strPt2) > maxWidth { + if len(strPt2) > 0 { + if maxWidth-len(strPt1)-1 > 0 { + DrawStringAtPoint(strPt2[:(maxWidth-len(strPt1)-1)], x+1, y, i.fg, i.bg) + strPt2 = strPt2[(maxWidth - len(strPt1)):] + } + nlCount := strings.Count(strPt2, "\n") + for len(strPt2) > maxWidth || nlCount > 0 { x, y = DrawStringAtPoint(strPt2[:maxWidth], x, y, i.fg, i.bg) - strPt2 = strPt2[maxWidth+1:] - continue + strPt2 = strPt2[maxWidth:] } x, y = DrawStringAtPoint(strPt2, x, y, i.fg, i.bg) } From ddda3d345033be317f4bd5e5c5dd8a7ad1e2b8e4 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Wed, 6 Jan 2016 22:05:28 -0600 Subject: [PATCH 21/27] gofmt --- termbox_menu.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/termbox_menu.go b/termbox_menu.go index 44e7485..7d3b0e3 100644 --- a/termbox_menu.go +++ b/termbox_menu.go @@ -15,7 +15,7 @@ type Menu struct { disabledBg, disabledFg termbox.Attribute isDone bool bordered bool - vimMode bool + vimMode bool } // CreateMenu Creates a menu with the specified attributes From 8b0af6e07915d4d9c9065dfbd0ba73579b2dd32d Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sat, 9 Jan 2016 12:12:23 -0600 Subject: [PATCH 22/27] If first option is disabled, start at next Also, accept 'space' as confirmation --- termbox_menu.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/termbox_menu.go b/termbox_menu.go index 44e7485..d509454 100644 --- a/termbox_menu.go +++ b/termbox_menu.go @@ -15,7 +15,7 @@ type Menu struct { disabledBg, disabledFg termbox.Attribute isDone bool bordered bool - vimMode bool + vimMode bool } // CreateMenu Creates a menu with the specified attributes @@ -252,7 +252,7 @@ func (i *Menu) DisableVimMode() *Menu { // HandleKeyPress handles the termbox event and returns whether it was consumed func (i *Menu) HandleKeyPress(event termbox.Event) bool { - if event.Key == termbox.KeyEnter { + if event.Key == termbox.KeyEnter || event.Key == termbox.KeySpace { i.isDone = true return true } @@ -310,6 +310,11 @@ func (i *Menu) Draw() { } } + // If the currently selected option is disabled, move to the next + if i.GetSelectedOption().IsDisabled() { + i.SelectNextOption() + } + // Print the options if len(i.options) > 0 { for idx := range i.options { From e93feb0ddef0f01ad55fa464fd040b3e6b381a7e Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sun, 10 Jan 2016 20:42:51 -0600 Subject: [PATCH 23/27] Text wrapping no newlines though. --- termbox_inputfield.go | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/termbox_inputfield.go b/termbox_inputfield.go index 17cc4af..9541528 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -2,7 +2,6 @@ package termboxUtil import ( "fmt" - "strconv" "strings" "github.com/nsf/termbox-go" @@ -135,8 +134,15 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool { // Draw outputs the input field on the screen func (i *InputField) Draw() { + maxWidth := i.width + x, y := i.x, i.y + startX := i.x if i.bordered { DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + maxWidth-- + x++ + y++ + startX++ } var strPt1, strPt2 string @@ -160,28 +166,14 @@ func (i *InputField) Draw() { } // strPt1, strPt2 = all of the text before, after the cursor // cursorRune is the rune on the cursor - maxWidth := i.width - if i.bordered { - maxWidth-- - } - DrawStringAtPoint(strconv.Itoa(strings.Count(i.value, "\n")), i.x, i.y, i.fg, i.bg) if i.wrap { // Split the text into maxWidth chunks - x, y := i.x+1, i.y+1 - nlCount := strings.Count(strPt1, "\n") - for len(strPt1) > maxWidth || nlCount > 0 { - nlIdx := strings.Index(strPt1, "\n") + for len(strPt1) > maxWidth { //|| nlCount > 0 { breakAt := maxWidth - if nlIdx < maxWidth { - breakAt = nlIdx + 1 - } - x, y = DrawStringAtPoint(strPt1[:breakAt], x, y, i.fg, i.bg) + DrawStringAtPoint(strPt1[:breakAt], x, y, i.fg, i.bg) + x = startX + y++ strPt1 = strPt1[breakAt:] - if len(strPt1) > 0 { - x = i.x + 1 - y++ - } - nlCount = strings.Count(strPt1, "\n") } x, y = DrawStringAtPoint(strPt1, x, y, i.fg, i.bg) if maxWidth-len(strPt1) <= 0 { From e843ea92b99582c67600d8dfabc773f9b2defe5b Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Wed, 13 Jan 2016 21:17:08 -0600 Subject: [PATCH 24/27] Input Field still isn't where I want it. Also added convenience functions to test if termbox events are alpha/numeric/symbol --- termbox_inputfield.go | 206 +++++++++++++++++++++++++++++------------- termbox_util.go | 33 ++++++- 2 files changed, 174 insertions(+), 65 deletions(-) diff --git a/termbox_inputfield.go b/termbox_inputfield.go index 9541528..d3a2d82 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -1,7 +1,6 @@ package termboxUtil import ( - "fmt" "strings" "github.com/nsf/termbox-go" @@ -15,6 +14,7 @@ type InputField struct { fg, bg termbox.Attribute bordered bool wrap bool + multiline bool } // CreateInputField creates an input field at x, y that is w by h @@ -81,12 +81,20 @@ func (i *InputField) SetBordered(b bool) *InputField { func (i *InputField) DoesWrap() bool { return i.wrap } // SetWrap sets whether we wrap the text at width. -// If 'wrap' is set, we automatically increase the height when we need to. func (i *InputField) SetWrap(b bool) *InputField { i.wrap = b return i } +// IsMultiline returns true or false if this field can have multiple lines +func (i *InputField) IsMultiline() bool { return i.multiline } + +// SetMultiline sets whether the field can have multiple lines +func (i *InputField) SetMultiline(b bool) *InputField { + i.multiline = b + return i +} + // HandleKeyPress accepts the termbox event and returns whether it was consumed func (i *InputField) HandleKeyPress(event termbox.Event) bool { if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 { @@ -114,19 +122,23 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool { case termbox.KeyTab: ch = "\t" case termbox.KeyEnter: - ch = "\n" + if i.multiline { + ch = "\n" + } default: - ch = string(event.Ch) + if KeyIsAlphaNumeric(event) || KeyIsSymbol(event) { + ch = string(event.Ch) + } } if i.cursor+len(i.value) == 0 { - i.value = fmt.Sprintf("%s%s", ch, i.value) + i.value = string(ch) + i.value } else if i.cursor == 0 { - i.value = fmt.Sprintf("%s%s", i.value, ch) + i.value = i.value + string(ch) } else { strPt1 := i.value[:(len(i.value) + i.cursor)] strPt2 := i.value[(len(i.value) + i.cursor):] - i.value = fmt.Sprintf("%s%s%s", strPt1, ch, strPt2) + i.value = strPt1 + string(ch) + strPt2 } } return true @@ -135,76 +147,142 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool { // Draw outputs the input field on the screen func (i *InputField) Draw() { maxWidth := i.width + maxHeight := i.height x, y := i.x, i.y startX := i.x + startY := i.y if i.bordered { DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) maxWidth-- + maxHeight-- x++ y++ startX++ + startY++ } - var strPt1, strPt2 string - var cursorRune rune - if len(i.value) > 0 { - if i.cursor+len(i.value) == 0 { - strPt1 = "" - strPt2 = i.value[1:] - cursorRune = rune(i.value[0]) - } else if i.cursor == 0 { - strPt1 = i.value - strPt2 = "" - cursorRune = ' ' - } else { - strPt1 = i.value[:(len(i.value) + i.cursor)] - strPt2 = i.value[(len(i.value)+i.cursor)+1:] - cursorRune = rune(i.value[len(i.value)+i.cursor]) - } + var valSplit []string //, cursorLine []string + if i.multiline { + valSplit = strings.Split(i.value, "\n") } else { - strPt1, strPt2, cursorRune = "", "", ' ' + if i.wrap { + var j int + for j < len(i.value) { + l, h := j, j+i.width + //if l >= i.cursor && h <= i.cursor { + // cursorLine = append(cursorLine, i.value[l:i.cursor]) + // cursorLine = append(cursorLine, i.value[i.cursor]...) + // cursorLine = append(cursorLine, i.value[i.cursor:h]) + //} else { + valSplit = append(valSplit, i.value[l:h]) + //} + j = h + } + } } - // strPt1, strPt2 = all of the text before, after the cursor - // cursorRune is the rune on the cursor - if i.wrap { - // Split the text into maxWidth chunks - for len(strPt1) > maxWidth { //|| nlCount > 0 { - breakAt := maxWidth - DrawStringAtPoint(strPt1[:breakAt], x, y, i.fg, i.bg) - x = startX - y++ - strPt1 = strPt1[breakAt:] - } - x, y = DrawStringAtPoint(strPt1, x, y, i.fg, i.bg) - if maxWidth-len(strPt1) <= 0 { - termbox.SetCell(x, y, cursorRune, i.bg, i.fg) - } - if len(strPt2) > 0 { - if maxWidth-len(strPt1)-1 > 0 { - DrawStringAtPoint(strPt2[:(maxWidth-len(strPt1)-1)], x+1, y, i.fg, i.bg) - strPt2 = strPt2[(maxWidth - len(strPt1)):] - } - nlCount := strings.Count(strPt2, "\n") - for len(strPt2) > maxWidth || nlCount > 0 { - x, y = DrawStringAtPoint(strPt2[:maxWidth], x, y, i.fg, i.bg) - strPt2 = strPt2[maxWidth:] - } - x, y = DrawStringAtPoint(strPt2, x, y, i.fg, i.bg) - } - } else { - // Not wrapping, just adjust the viewport - for len(strPt1)+len(strPt2)+1 > maxWidth { - if len(strPt1) >= len(strPt2) { - if len(strPt1) == 0 { - break + for j := range valSplit { + DrawStringAtPoint(valSplit[j], x, y+j, i.fg, i.bg) + } + //var valSplit []string + // if it's not multiline, new lines aren't allowed in the input + /* + multiSplit := strings.Split(i.value, "\n") + var cursCount int + for j := range multiSplit { + for k := range multiSplit[j] { + if cursCount == i.cursor { + termbox.SetCell(x+k, y+j, rune(multiSplit[j][k]), i.fg, i.bg) + } else { + termbox.SetCell(x+k, y+j, rune(multiSplit[j][k]), i.bg, i.fg) } - strPt1 = strPt1[1:] - } else { - strPt2 = strPt2[:len(strPt2)-1] + cursCount++ } } - x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg) - termbox.SetCell(x, y, cursorRune, i.bg, i.fg) - DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) - } + */ + /* + if i.wrap { + // Automatically wrap the text + for j := range multiSplit { + for len(multiSplit[j]) > maxWidth { + valSplit = append(valSplit, multiSplit[j][:maxWidth]) + multiSplit[j] = multiSplit[j][maxWidth:] + } + } + } else { + valSplit = multiSplit + } + for j := range valSplit { + DrawStringAtPoint(valSplit[j], x, y+j, i.fg, i.bg) + } + + /* + var strPt1, strPt2 string + var cursorRune rune + if len(i.value) > 0 { + if i.cursor+len(i.value) == 0 { + strPt1 = "" + strPt2 = i.value[1:] + cursorRune = rune(i.value[0]) + } else if i.cursor == 0 { + strPt1 = i.value + strPt2 = "" + cursorRune = ' ' + } else { + strPt1 = i.value[:(len(i.value) + i.cursor)] + strPt2 = i.value[(len(i.value)+i.cursor)+1:] + cursorRune = rune(i.value[len(i.value)+i.cursor]) + } + } else { + strPt1, strPt2, cursorRune = "", "", ' ' + } + // strPt1, strPt2 = all of the text before, after the cursor + // cursorRune is the rune on the cursor + if i.wrap { + // Split the text into maxWidth chunks + for len(strPt1) > maxWidth { + breakAt := maxWidth + DrawStringAtPoint(strPt1[:breakAt], x, y, i.fg, i.bg) + x = startX + y++ + strPt1 = strPt1[breakAt:] + } + x, y = DrawStringAtPoint(strPt1, x, y, i.fg, i.bg) + if x >= maxWidth { + y++ + x = startX + } + termbox.SetCell(x, y, cursorRune, i.bg, i.fg) + x++ + if len(strPt2) > 0 { + lenLeft := maxWidth - len(strPt1) - 1 + if lenLeft > 0 && len(strPt2) > lenLeft { + DrawStringAtPoint(strPt2[:lenLeft], x+1, y, i.fg, i.bg) + strPt2 = strPt2[lenLeft:] + } + for len(strPt2) > maxWidth { + breakAt := maxWidth + DrawStringAtPoint(strPt2[:breakAt], x, y, i.fg, i.bg) + x = startX + y++ + strPt2 = strPt2[breakAt:] + } + x, y = DrawStringAtPoint(strPt2, x, y, i.fg, i.bg) + } + } else { + // Not wrapping, just adjust the viewport + for len(strPt1)+len(strPt2)+1 > maxWidth { + if len(strPt1) >= len(strPt2) { + if len(strPt1) == 0 { + break + } + strPt1 = strPt1[1:] + } else { + strPt2 = strPt2[:len(strPt2)-1] + } + } + x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg) + termbox.SetCell(x, y, cursorRune, i.bg, i.fg) + DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) + } + */ } diff --git a/termbox_util.go b/termbox_util.go index 80d6426..40e6a24 100644 --- a/termbox_util.go +++ b/termbox_util.go @@ -24,13 +24,44 @@ const ( // KeyIsAlphaNumeric Returns whether the termbox event is an // Alpha-Numeric Key Press func KeyIsAlphaNumeric(event termbox.Event) bool { + return KeyIsAlpha(event) || KeyIsNumeric(event) +} + +// KeyIsAlpha Returns whether the termbox event is a +// alphabetic Key press +func KeyIsAlpha(event termbox.Event) bool { k := event.Ch - if (k >= 'a' && k <= 'z') || (k >= 'A' && k <= 'Z') || (k >= '0' && k <= '9') { + if (k >= 'a' && k <= 'z') || (k >= 'A' && k <= 'Z') { return true } return false } +// KeyIsNumeric Returns whether the termbox event is a +// numeric Key press +func KeyIsNumeric(event termbox.Event) bool { + k := event.Ch + if k >= '0' && k <= '9' { + return true + } + return false +} + +// KeyIsSymbol Returns whether the termbox event is a +// symbol Key press +func KeyIsSymbol(event termbox.Event) bool { + symbols := []rune{'!', '@', '#', '$', '%', '^', '&', '*', + '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '|', + ';', ':', '"', '\'', ',', '<', '.', '>', '/', '?', '`', '~'} + k := event.Ch + for i := range symbols { + if k == symbols[i] { + return true + } + } + return false +} + /* Basic Output Helpers */ // DrawStringAtPoint Draw a string of text at x, y with foreground color fg, background color bg From b2f731cbbb077ff5afcf008791e9ce4bf7be1808 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 14 Jan 2016 09:48:46 -0600 Subject: [PATCH 25/27] Adjusting input field --- termbox_inputfield.go | 205 ++++++++++++++++-------------------------- 1 file changed, 75 insertions(+), 130 deletions(-) diff --git a/termbox_inputfield.go b/termbox_inputfield.go index d3a2d82..8a5c785 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -1,10 +1,6 @@ package termboxUtil -import ( - "strings" - - "github.com/nsf/termbox-go" -) +import "github.com/nsf/termbox-go" // InputField is a field for inputting text type InputField struct { @@ -121,10 +117,12 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool { ch = " " case termbox.KeyTab: ch = "\t" - case termbox.KeyEnter: - if i.multiline { - ch = "\n" - } + /* Multiline is disabled right now + case termbox.KeyEnter: + if i.multiline { + ch = "\n" + } + */ default: if KeyIsAlphaNumeric(event) || KeyIsSymbol(event) { ch = string(event.Ch) @@ -161,128 +159,75 @@ func (i *InputField) Draw() { startY++ } - var valSplit []string //, cursorLine []string - if i.multiline { - valSplit = strings.Split(i.value, "\n") - } else { - if i.wrap { - var j int - for j < len(i.value) { - l, h := j, j+i.width - //if l >= i.cursor && h <= i.cursor { - // cursorLine = append(cursorLine, i.value[l:i.cursor]) - // cursorLine = append(cursorLine, i.value[i.cursor]...) - // cursorLine = append(cursorLine, i.value[i.cursor:h]) - //} else { - valSplit = append(valSplit, i.value[l:h]) - //} - j = h - } - } - } - for j := range valSplit { - DrawStringAtPoint(valSplit[j], x, y+j, i.fg, i.bg) - } - //var valSplit []string - // if it's not multiline, new lines aren't allowed in the input - /* - multiSplit := strings.Split(i.value, "\n") - var cursCount int - for j := range multiSplit { - for k := range multiSplit[j] { - if cursCount == i.cursor { - termbox.SetCell(x+k, y+j, rune(multiSplit[j][k]), i.fg, i.bg) - } else { - termbox.SetCell(x+k, y+j, rune(multiSplit[j][k]), i.bg, i.fg) - } - cursCount++ - } - } - */ - /* - if i.wrap { - // Automatically wrap the text - for j := range multiSplit { - for len(multiSplit[j]) > maxWidth { - valSplit = append(valSplit, multiSplit[j][:maxWidth]) - multiSplit[j] = multiSplit[j][maxWidth:] - } - } + var strPt1, strPt2 string + var cursorRune rune + if len(i.value) > 0 { + if i.cursor+len(i.value) == 0 { + strPt1 = "" + strPt2 = i.value[1:] + cursorRune = rune(i.value[0]) + } else if i.cursor == 0 { + strPt1 = i.value + strPt2 = "" + cursorRune = ' ' } else { - valSplit = multiSplit + strPt1 = i.value[:(len(i.value) + i.cursor)] + strPt2 = i.value[(len(i.value)+i.cursor)+1:] + cursorRune = rune(i.value[len(i.value)+i.cursor]) } - for j := range valSplit { - DrawStringAtPoint(valSplit[j], x, y+j, i.fg, i.bg) - } - - /* - var strPt1, strPt2 string - var cursorRune rune - if len(i.value) > 0 { - if i.cursor+len(i.value) == 0 { - strPt1 = "" - strPt2 = i.value[1:] - cursorRune = rune(i.value[0]) - } else if i.cursor == 0 { - strPt1 = i.value - strPt2 = "" - cursorRune = ' ' - } else { - strPt1 = i.value[:(len(i.value) + i.cursor)] - strPt2 = i.value[(len(i.value)+i.cursor)+1:] - cursorRune = rune(i.value[len(i.value)+i.cursor]) - } - } else { - strPt1, strPt2, cursorRune = "", "", ' ' - } - // strPt1, strPt2 = all of the text before, after the cursor - // cursorRune is the rune on the cursor - if i.wrap { - // Split the text into maxWidth chunks - for len(strPt1) > maxWidth { - breakAt := maxWidth - DrawStringAtPoint(strPt1[:breakAt], x, y, i.fg, i.bg) - x = startX - y++ - strPt1 = strPt1[breakAt:] - } - x, y = DrawStringAtPoint(strPt1, x, y, i.fg, i.bg) - if x >= maxWidth { - y++ - x = startX - } - termbox.SetCell(x, y, cursorRune, i.bg, i.fg) - x++ - if len(strPt2) > 0 { - lenLeft := maxWidth - len(strPt1) - 1 - if lenLeft > 0 && len(strPt2) > lenLeft { - DrawStringAtPoint(strPt2[:lenLeft], x+1, y, i.fg, i.bg) - strPt2 = strPt2[lenLeft:] - } - for len(strPt2) > maxWidth { - breakAt := maxWidth - DrawStringAtPoint(strPt2[:breakAt], x, y, i.fg, i.bg) - x = startX - y++ - strPt2 = strPt2[breakAt:] - } - x, y = DrawStringAtPoint(strPt2, x, y, i.fg, i.bg) - } - } else { - // Not wrapping, just adjust the viewport - for len(strPt1)+len(strPt2)+1 > maxWidth { - if len(strPt1) >= len(strPt2) { - if len(strPt1) == 0 { - break - } - strPt1 = strPt1[1:] - } else { - strPt2 = strPt2[:len(strPt2)-1] - } - } - x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg) - termbox.SetCell(x, y, cursorRune, i.bg, i.fg) - DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) - } + } else { + strPt1, strPt2, cursorRune = "", "", ' ' + } + // Original: + /* + x, y = DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg) + termbox.SetCell(x, y, cursorRune, i.bg, i.fg) + DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) */ + if i.wrap { + // Split the text into maxWidth chunks + for len(strPt1) > maxWidth { + breakAt := maxWidth + DrawStringAtPoint(strPt1[:breakAt], x, y, i.fg, i.bg) + x = startX + y++ + strPt1 = strPt1[breakAt:] + } + x, y = DrawStringAtPoint(strPt1, x, y, i.fg, i.bg) + if x >= maxWidth { + y++ + x = startX + } + termbox.SetCell(x, y, cursorRune, i.bg, i.fg) + x++ + if len(strPt2) > 0 { + lenLeft := maxWidth - len(strPt1) - 1 + if lenLeft > 0 && len(strPt2) > lenLeft { + DrawStringAtPoint(strPt2[:lenLeft], x+1, y, i.fg, i.bg) + strPt2 = strPt2[lenLeft:] + } + for len(strPt2) > maxWidth { + breakAt := maxWidth + DrawStringAtPoint(strPt2[:breakAt], x, y, i.fg, i.bg) + x = startX + y++ + strPt2 = strPt2[breakAt:] + } + x, y = DrawStringAtPoint(strPt2, x, y, i.fg, i.bg) + } + } else { + for len(strPt1)+len(strPt2)+1 > maxWidth { + if len(strPt1) >= len(strPt2) { + if len(strPt1) == 0 { + break + } + strPt1 = strPt1[1:] + } else { + strPt2 = strPt2[:len(strPt2)-1] + } + } + x, y = DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg) + termbox.SetCell(x, y, cursorRune, i.bg, i.fg) + DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) + } } From 3cf01a010f22a110c531103f4c83f50401ec233f Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sat, 16 Jan 2016 23:04:01 -0600 Subject: [PATCH 26/27] Started interfacing everything Building a scroll frame too. --- termbox_asciiart.go | 59 +++++++++++---- termbox_confirmmodal.go | 39 ++++------ termbox_inputfield.go | 24 +++---- termbox_inputmodal.go | 45 ++++-------- termbox_menu.go | 72 +++++++------------ termbox_progressbar.go | 51 +++++-------- termbox_scrollframe.go | 155 ++++++++++++++++++++++++++++++++++++++++ termbox_util.go | 13 ++++ 8 files changed, 290 insertions(+), 168 deletions(-) create mode 100644 termbox_scrollframe.go diff --git a/termbox_asciiart.go b/termbox_asciiart.go index d86855b..4f91a40 100644 --- a/termbox_asciiart.go +++ b/termbox_asciiart.go @@ -1,6 +1,8 @@ package termboxUtil import ( + "strings" + "github.com/nsf/termbox-go" ) @@ -21,18 +23,16 @@ func CreateASCIIArt(c []string, x, y int, fg, bg termbox.Attribute) *ASCIIArt { func (i *ASCIIArt) GetX() int { return i.x } // SetX set the x position of the modal to x -func (i *ASCIIArt) SetX(x int) *ASCIIArt { +func (i *ASCIIArt) SetX(x int) { i.x = x - return i } // GetY Return the y position of the modal func (i *ASCIIArt) GetY() int { return i.y } // SetY Set the y position of the modal to y -func (i *ASCIIArt) SetY(y int) *ASCIIArt { +func (i *ASCIIArt) SetY(y int) { i.y = y - return i } // GetHeight Returns the number of strings in the contents slice @@ -40,10 +40,45 @@ func (i *ASCIIArt) GetHeight() int { return len(i.contents) } +// SetHeight truncates lines from the bottom of the ascii art +func (i *ASCIIArt) SetHeight(h int) { + if len(i.contents) > h { + i.contents = i.contents[:h] + } else { + for j := len(i.contents); j < h; j++ { + i.contents = append(i.contents, "") + } + } +} + +// GetWidth Returns the number of strings in the contents slice +func (i *ASCIIArt) GetWidth() int { + // Find the longest line + var ret int + for j := range i.contents { + if len(i.contents[j]) > ret { + ret = len(i.contents[j]) + } + } + return ret +} + +// SetWidth Sets all lines in the contents to width w +func (i *ASCIIArt) SetWidth(w int) { + // Find the longest line + for j := range i.contents { + mkUp := w - len(i.contents[j]) + if mkUp > 0 { + i.contents[j] = i.contents[j] + strings.Repeat(" ", mkUp) + } else { + i.contents[j] = i.contents[j][:w] + } + } +} + // SetContents Sets the contents of i to c -func (i *ASCIIArt) SetContents(c []string) *ASCIIArt { +func (i *ASCIIArt) SetContents(c []string) { i.contents = c - return i } // GetContents returns the ascii art @@ -52,33 +87,30 @@ func (i *ASCIIArt) GetContents() []string { } // SetContentLine Sets a specific line of the contents to s -func (i *ASCIIArt) SetContentLine(s string, idx int) *ASCIIArt { +func (i *ASCIIArt) SetContentLine(s string, idx int) { if idx >= 0 && idx < len(i.contents) { i.contents[idx] = s } - return i } // GetBackground Return the current background color of the modal func (i *ASCIIArt) GetBackground() termbox.Attribute { return i.bg } // SetBackground Set the current background color to bg -func (i *ASCIIArt) SetBackground(bg termbox.Attribute) *ASCIIArt { +func (i *ASCIIArt) SetBackground(bg termbox.Attribute) { i.bg = bg - return i } // GetForeground Return the current foreground color func (i *ASCIIArt) GetForeground() termbox.Attribute { return i.fg } // SetForeground Set the foreground color to fg -func (i *ASCIIArt) SetForeground(fg termbox.Attribute) *ASCIIArt { +func (i *ASCIIArt) SetForeground(fg termbox.Attribute) { i.fg = fg - return i } // Align Align the Ascii art over width width with alignment a -func (i *ASCIIArt) Align(a TextAlignment, width int) *ASCIIArt { +func (i *ASCIIArt) Align(a TextAlignment, width int) { // First get the width of the longest string in the slice var newContents []string incomingLength := 0 @@ -91,7 +123,6 @@ func (i *ASCIIArt) Align(a TextAlignment, width int) *ASCIIArt { newContents = append(newContents, AlignText(AlignText(line, incomingLength, AlignLeft), width, a)) } i.contents = newContents - return i } // HandleKeyPress accepts the termbox event and returns whether it was consumed diff --git a/termbox_confirmmodal.go b/termbox_confirmmodal.go index 2b40187..59d419c 100644 --- a/termbox_confirmmodal.go +++ b/termbox_confirmmodal.go @@ -32,114 +32,101 @@ func CreateConfirmModal(title string, x, y, width, height int, fg, bg termbox.At func (i *ConfirmModal) GetTitle() string { return i.title } // SetTitle sets the current title of the modal to s -func (i *ConfirmModal) SetTitle(s string) *ConfirmModal { +func (i *ConfirmModal) SetTitle(s string) { i.title = s - return i } // GetText returns the current text of the modal func (i *ConfirmModal) GetText() string { return i.text } // SetText sets the text of the modal to s -func (i *ConfirmModal) SetText(s string) *ConfirmModal { +func (i *ConfirmModal) SetText(s string) { i.text = s - return i } // GetX returns the current x coordinate of the modal func (i *ConfirmModal) GetX() int { return i.x } // SetX sets the current x coordinate of the modal to x -func (i *ConfirmModal) SetX(x int) *ConfirmModal { +func (i *ConfirmModal) SetX(x int) { i.x = x - return i } // GetY returns the current y coordinate of the modal func (i *ConfirmModal) GetY() int { return i.y } // SetY sets the current y coordinate of the modal to y -func (i *ConfirmModal) SetY(y int) *ConfirmModal { +func (i *ConfirmModal) SetY(y int) { i.y = y - return i } // GetWidth returns the current width of the modal func (i *ConfirmModal) GetWidth() int { return i.width } // SetWidth sets the current modal width to width -func (i *ConfirmModal) SetWidth(width int) *ConfirmModal { +func (i *ConfirmModal) SetWidth(width int) { i.width = width - return i } // GetHeight returns the current height of the modal func (i *ConfirmModal) GetHeight() int { return i.height } // SetHeight set the height of the modal to height -func (i *ConfirmModal) SetHeight(height int) *ConfirmModal { +func (i *ConfirmModal) SetHeight(height int) { i.height = height - return i } // HelpIsShown returns true or false if the help is displayed func (i *ConfirmModal) HelpIsShown() bool { return i.showHelp } // ShowHelp sets whether or not to display the help text -func (i *ConfirmModal) ShowHelp(b bool) *ConfirmModal { +func (i *ConfirmModal) ShowHelp(b bool) { i.showHelp = b - return i } // GetBackground returns the current background color func (i *ConfirmModal) GetBackground() termbox.Attribute { return i.bg } // SetBackground sets the background color to bg -func (i *ConfirmModal) SetBackground(bg termbox.Attribute) *ConfirmModal { +func (i *ConfirmModal) SetBackground(bg termbox.Attribute) { i.bg = bg - return i } // GetForeground returns the current foreground color func (i *ConfirmModal) GetForeground() termbox.Attribute { return i.fg } // SetForeground sets the current foreground color to fg -func (i *ConfirmModal) SetForeground(fg termbox.Attribute) *ConfirmModal { +func (i *ConfirmModal) SetForeground(fg termbox.Attribute) { i.fg = fg - return i } // IsDone returns whether the user has answered the modal func (i *ConfirmModal) IsDone() bool { return i.isDone } // SetDone sets whether the modal has completed it's purpose -func (i *ConfirmModal) SetDone(b bool) *ConfirmModal { +func (i *ConfirmModal) SetDone(b bool) { i.isDone = b - return i } // Show sets the visibility flag of the modal to true -func (i *ConfirmModal) Show() *ConfirmModal { +func (i *ConfirmModal) Show() { i.isVisible = true - return i } // Hide sets the visibility flag of the modal to false -func (i *ConfirmModal) Hide() *ConfirmModal { +func (i *ConfirmModal) Hide() { i.isVisible = false - return i } // IsAccepted returns whether the user accepted the modal func (i *ConfirmModal) IsAccepted() bool { return i.accepted } // Clear clears all of the non-positional parameters of the modal -func (i *ConfirmModal) Clear() *ConfirmModal { +func (i *ConfirmModal) Clear() { i.title = "" i.text = "" i.accepted = false i.isDone = false - return i } // HandleKeyPress handles the termbox event and returns whether it was consumed diff --git a/termbox_inputfield.go b/termbox_inputfield.go index d3a2d82..2480b51 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -27,72 +27,64 @@ func CreateInputField(x, y, w, h int, fg, bg termbox.Attribute) *InputField { func (i *InputField) GetValue() string { return i.value } // SetValue sets the current text in the InputField to s -func (i *InputField) SetValue(s string) *InputField { +func (i *InputField) SetValue(s string) { i.value = s - return i } // GetX returns the x position of the input field func (i *InputField) GetX() int { return i.x } // SetX sets the x position of the input field -func (i *InputField) SetX(x int) *InputField { +func (i *InputField) SetX(x int) { i.x = x - return i } // GetY returns the y position of the input field func (i *InputField) GetY() int { return i.y } // SetY sets the y position of the input field -func (i *InputField) SetY(y int) *InputField { +func (i *InputField) SetY(y int) { i.y = y - return i } // GetWidth returns the current width of the input field func (i *InputField) GetWidth() int { return i.width } // SetWidth sets the current width of the input field -func (i *InputField) SetWidth(w int) *InputField { +func (i *InputField) SetWidth(w int) { i.width = w - return i } // GetHeight returns the current height of the input field func (i *InputField) GetHeight() int { return i.height } // SetHeight sets the current height of the input field -func (i *InputField) SetHeight(h int) *InputField { +func (i *InputField) SetHeight(h int) { i.height = h - return i } // IsBordered returns true or false if this input field has a border func (i *InputField) IsBordered() bool { return i.bordered } // SetBordered sets whether we render a border around the input field -func (i *InputField) SetBordered(b bool) *InputField { +func (i *InputField) SetBordered(b bool) { i.bordered = b - return i } // DoesWrap returns true or false if this input field wraps text func (i *InputField) DoesWrap() bool { return i.wrap } // SetWrap sets whether we wrap the text at width. -func (i *InputField) SetWrap(b bool) *InputField { +func (i *InputField) SetWrap(b bool) { i.wrap = b - return i } // IsMultiline returns true or false if this field can have multiple lines func (i *InputField) IsMultiline() bool { return i.multiline } // SetMultiline sets whether the field can have multiple lines -func (i *InputField) SetMultiline(b bool) *InputField { +func (i *InputField) SetMultiline(b bool) { i.multiline = b - return i } // HandleKeyPress accepts the termbox event and returns whether it was consumed diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index ce3a92e..50ca646 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -30,99 +30,87 @@ func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attr func (i *InputModal) GetTitle() string { return i.title } // SetTitle Sets the title of the modal to s -func (i *InputModal) SetTitle(s string) *InputModal { +func (i *InputModal) SetTitle(s string) { i.title = s - return i } // GetText Return the text of the modal func (i *InputModal) GetText() string { return i.text } // SetText Set the text of the modal to s -func (i *InputModal) SetText(s string) *InputModal { +func (i *InputModal) SetText(s string) { i.text = s - return i } // GetX Return the x position of the modal func (i *InputModal) GetX() int { return i.x } // SetX set the x position of the modal to x -func (i *InputModal) SetX(x int) *InputModal { +func (i *InputModal) SetX(x int) { i.x = x - return i } // GetY Return the y position of the modal func (i *InputModal) GetY() int { return i.y } // SetY Set the y position of the modal to y -func (i *InputModal) SetY(y int) *InputModal { +func (i *InputModal) SetY(y int) { i.y = y - return i } // GetWidth Return the width of the modal func (i *InputModal) GetWidth() int { return i.width } // SetWidth Set the width of the modal to width -func (i *InputModal) SetWidth(width int) *InputModal { +func (i *InputModal) SetWidth(width int) { i.width = width - return i } // GetHeight Return the height of the modal func (i *InputModal) GetHeight() int { return i.height } // SetHeight Set the height of the modal to height -func (i *InputModal) SetHeight(height int) *InputModal { +func (i *InputModal) SetHeight(height int) { i.height = height - return i } // HelpIsShown Returns whether the modal is showing it's help text or not func (i *InputModal) HelpIsShown() bool { return i.showHelp } // ShowHelp Set the "Show Help" flag -func (i *InputModal) ShowHelp(b bool) *InputModal { +func (i *InputModal) ShowHelp(b bool) { i.showHelp = b - return i } // GetBackground Return the current background color of the modal func (i *InputModal) GetBackground() termbox.Attribute { return i.bg } // SetBackground Set the current background color to bg -func (i *InputModal) SetBackground(bg termbox.Attribute) *InputModal { +func (i *InputModal) SetBackground(bg termbox.Attribute) { i.bg = bg - return i } // GetForeground Return the current foreground color func (i *InputModal) GetForeground() termbox.Attribute { return i.fg } // SetForeground Set the foreground color to fg -func (i *InputModal) SetForeground(fg termbox.Attribute) *InputModal { +func (i *InputModal) SetForeground(fg termbox.Attribute) { i.fg = fg - return i } // Show Sets the visibility flag to true -func (i *InputModal) Show() *InputModal { +func (i *InputModal) Show() { i.isVisible = true - return i } // Hide Sets the visibility flag to false -func (i *InputModal) Hide() *InputModal { +func (i *InputModal) Hide() { i.isVisible = false - return i } // SetDone Sets the flag that tells whether this modal has completed it's purpose -func (i *InputModal) SetDone(b bool) *InputModal { +func (i *InputModal) SetDone(b bool) { i.isDone = b - return i } // IsDone Returns the "isDone" flag @@ -134,25 +122,22 @@ func (i *InputModal) IsDone() bool { func (i *InputModal) GetValue() string { return i.input.GetValue() } // SetValue Sets the value of the input to s -func (i *InputModal) SetValue(s string) *InputModal { +func (i *InputModal) SetValue(s string) { i.input.SetValue(s) - return i } // SetInputWrap sets whether the input field will wrap long text or not -func (i *InputModal) SetInputWrap(b bool) *InputModal { +func (i *InputModal) SetInputWrap(b bool) { i.input.SetWrap(b) - return i } // Clear Resets all non-positional parameters of the modal -func (i *InputModal) Clear() *InputModal { +func (i *InputModal) Clear() { i.title = "" i.text = "" i.input.SetValue("") i.isDone = false i.isVisible = false - return i } // HandleKeyPress Handle the termbox event, return true if it was consumed diff --git a/termbox_menu.go b/termbox_menu.go index d509454..c9a5ad2 100644 --- a/termbox_menu.go +++ b/termbox_menu.go @@ -39,9 +39,8 @@ func CreateMenu(title string, options []string, x, y, width, height int, fg, bg func (i *Menu) GetTitle() string { return i.title } // SetTitle sets the current title of the menu to s -func (i *Menu) SetTitle(s string) *Menu { +func (i *Menu) SetTitle(s string) { i.title = s - return i } // GetOptions returns the current options of the menu @@ -50,54 +49,49 @@ func (i *Menu) GetOptions() []MenuOption { } // SetOptions set the menu's options to opts -func (i *Menu) SetOptions(opts []MenuOption) *Menu { +func (i *Menu) SetOptions(opts []MenuOption) { i.options = opts - return i } // SetOptionsFromStrings sets the options of this menu from a slice of strings -func (i *Menu) SetOptionsFromStrings(opts []string) *Menu { +func (i *Menu) SetOptionsFromStrings(opts []string) { var newOpts []MenuOption for _, v := range opts { newOpts = append(newOpts, *CreateOptionFromText(v)) } - return i.SetOptions(newOpts) + i.SetOptions(newOpts) } // GetX returns the current x coordinate of the menu func (i *Menu) GetX() int { return i.x } // SetX sets the current x coordinate of the menu to x -func (i *Menu) SetX(x int) *Menu { +func (i *Menu) SetX(x int) { i.x = x - return i } // GetY returns the current y coordinate of the menu func (i *Menu) GetY() int { return i.y } // SetY sets the current y coordinate of the menu to y -func (i *Menu) SetY(y int) *Menu { +func (i *Menu) SetY(y int) { i.y = y - return i } // GetWidth returns the current width of the menu func (i *Menu) GetWidth() int { return i.width } // SetWidth sets the current menu width to width -func (i *Menu) SetWidth(width int) *Menu { +func (i *Menu) SetWidth(width int) { i.width = width - return i } // GetHeight returns the current height of the menu func (i *Menu) GetHeight() int { return i.height } // SetHeight set the height of the menu to height -func (i *Menu) SetHeight(height int) *Menu { +func (i *Menu) SetHeight(height int) { i.height = height - return i } // GetSelectedOption returns the current selected option @@ -140,7 +134,7 @@ func (i *Menu) GetSelectedIndex() int { } // SetSelectedOption sets the current selected option to v (if it's valid) -func (i *Menu) SetSelectedOption(v *MenuOption) *Menu { +func (i *Menu) SetSelectedOption(v *MenuOption) { for idx := range i.options { if &i.options[idx] == v { i.options[idx].Select() @@ -148,35 +142,30 @@ func (i *Menu) SetSelectedOption(v *MenuOption) *Menu { i.options[idx].Unselect() } } - return i } // SelectPrevOption Decrements the selected option (if it can) -func (i *Menu) SelectPrevOption() *Menu { +func (i *Menu) SelectPrevOption() { idx := i.GetSelectedIndex() for idx >= 0 { idx-- testOption := i.GetOptionFromIndex(idx) if testOption != nil && !testOption.IsDisabled() { i.SetSelectedOption(testOption) - return i } } - return i } // SelectNextOption Increments the selected option (if it can) -func (i *Menu) SelectNextOption() *Menu { +func (i *Menu) SelectNextOption() { idx := i.GetSelectedIndex() for idx < len(i.options) { idx++ testOption := i.GetOptionFromIndex(idx) if testOption != nil && !testOption.IsDisabled() { i.SetSelectedOption(testOption) - return i } } - return i } // SetOptionDisabled Disables the specified option @@ -197,57 +186,50 @@ func (i *Menu) SetOptionEnabled(idx int) { func (i *Menu) HelpIsShown() bool { return i.showHelp } // ShowHelp sets whether or not to display the help text -func (i *Menu) ShowHelp(b bool) *Menu { +func (i *Menu) ShowHelp(b bool) { i.showHelp = b - return i } // GetBackground returns the current background color func (i *Menu) GetBackground() termbox.Attribute { return i.bg } // SetBackground sets the background color to bg -func (i *Menu) SetBackground(bg termbox.Attribute) *Menu { +func (i *Menu) SetBackground(bg termbox.Attribute) { i.bg = bg - return i } // GetForeground returns the current foreground color func (i *Menu) GetForeground() termbox.Attribute { return i.fg } // SetForeground sets the current foreground color to fg -func (i *Menu) SetForeground(fg termbox.Attribute) *Menu { +func (i *Menu) SetForeground(fg termbox.Attribute) { i.fg = fg - return i } // IsDone returns whether the user has answered the modal func (i *Menu) IsDone() bool { return i.isDone } // SetDone sets whether the modal has completed it's purpose -func (i *Menu) SetDone(b bool) *Menu { +func (i *Menu) SetDone(b bool) { i.isDone = b - return i } // IsBordered returns true or false if this menu has a border func (i *Menu) IsBordered() bool { return i.bordered } // SetBordered sets whether we render a border around the menu -func (i *Menu) SetBordered(b bool) *Menu { +func (i *Menu) SetBordered(b bool) { i.bordered = b - return i } // EnableVimMode Enables h,j,k,l navigation -func (i *Menu) EnableVimMode() *Menu { +func (i *Menu) EnableVimMode() { i.vimMode = true - return i } // DisableVimMode Disables h,j,k,l navigation -func (i *Menu) DisableVimMode() *Menu { +func (i *Menu) DisableVimMode() { i.vimMode = false - return i } // HandleKeyPress handles the termbox event and returns whether it was consumed @@ -348,24 +330,21 @@ func CreateOptionFromText(s string) *MenuOption { } // SetText Sets the text for this option -func (i *MenuOption) SetText(s string) *MenuOption { +func (i *MenuOption) SetText(s string) { i.text = s - return i } // GetText Returns the text for this option func (i *MenuOption) GetText() string { return i.text } // Disable Sets this option to disabled -func (i *MenuOption) Disable() *MenuOption { +func (i *MenuOption) Disable() { i.disabled = true - return i } // Enable Sets this option to enabled -func (i *MenuOption) Enable() *MenuOption { +func (i *MenuOption) Enable() { i.disabled = false - return i } // IsDisabled returns whether this option is enabled @@ -379,21 +358,18 @@ func (i *MenuOption) IsSelected() bool { } // Select Sets this option to selected -func (i *MenuOption) Select() *MenuOption { +func (i *MenuOption) Select() { i.selected = true - return i } // Unselect Sets this option to not selected -func (i *MenuOption) Unselect() *MenuOption { +func (i *MenuOption) Unselect() { i.selected = false - return i } // SetHelpText Sets this option's help text to s -func (i *MenuOption) SetHelpText(s string) *MenuOption { +func (i *MenuOption) SetHelpText(s string) { i.helpText = s - return i } // GetHelpText Returns the help text for this option diff --git a/termbox_progressbar.go b/termbox_progressbar.go index 0cbd3e0..16aaef0 100644 --- a/termbox_progressbar.go +++ b/termbox_progressbar.go @@ -36,27 +36,24 @@ func (i *ProgressBar) GetProgress() int { } // SetProgress sets the current progress of the bar -func (i *ProgressBar) SetProgress(p int) *ProgressBar { +func (i *ProgressBar) SetProgress(p int) { if (p <= i.total || i.allowOverflow) || (p >= 0 || i.allowUnderflow) { i.progress = p } - return i } // IncrProgress increments the current progress of the bar -func (i *ProgressBar) IncrProgress() *ProgressBar { +func (i *ProgressBar) IncrProgress() { if i.progress < i.total || i.allowOverflow { i.progress++ } - return i } // DecrProgress decrements the current progress of the bar -func (i *ProgressBar) DecrProgress() *ProgressBar { +func (i *ProgressBar) DecrProgress() { if i.progress > 0 || i.allowUnderflow { i.progress-- } - return i } // GetPercent returns the percent full of the bar @@ -65,27 +62,23 @@ func (i *ProgressBar) GetPercent() int { } // EnableOverflow Tells the progress bar that it can go over the total -func (i *ProgressBar) EnableOverflow() *ProgressBar { +func (i *ProgressBar) EnableOverflow() { i.allowOverflow = true - return i } // DisableOverflow Tells the progress bar that it can NOT go over the total -func (i *ProgressBar) DisableOverflow() *ProgressBar { +func (i *ProgressBar) DisableOverflow() { i.allowOverflow = false - return i } // EnableUnderflow Tells the progress bar that it can go below zero -func (i *ProgressBar) EnableUnderflow() *ProgressBar { +func (i *ProgressBar) EnableUnderflow() { i.allowUnderflow = true - return i } // DisableUnderflow Tells the progress bar that it can NOT go below zero -func (i *ProgressBar) DisableUnderflow() *ProgressBar { +func (i *ProgressBar) DisableUnderflow() { i.allowUnderflow = false - return i } // GetFullChar returns the rune used for 'full' @@ -94,9 +87,8 @@ func (i *ProgressBar) GetFullChar() rune { } // SetFullChar sets the rune used for 'full' -func (i *ProgressBar) SetFullChar(f rune) *ProgressBar { +func (i *ProgressBar) SetFullChar(f rune) { i.fullChar = f - return i } // GetEmptyChar gets the rune used for 'empty' @@ -105,27 +97,24 @@ func (i *ProgressBar) GetEmptyChar() rune { } // SetEmptyChar sets the rune used for 'empty' -func (i *ProgressBar) SetEmptyChar(f rune) *ProgressBar { +func (i *ProgressBar) SetEmptyChar(f rune) { i.emptyChar = f - return i } // GetX Return the x position of the Progress Bar func (i *ProgressBar) GetX() int { return i.x } // SetX set the x position of the ProgressBar to x -func (i *ProgressBar) SetX(x int) *ProgressBar { +func (i *ProgressBar) SetX(x int) { i.x = x - return i } // GetY Return the y position of the ProgressBar func (i *ProgressBar) GetY() int { return i.y } // SetY Set the y position of the ProgressBar to y -func (i *ProgressBar) SetY(y int) *ProgressBar { +func (i *ProgressBar) SetY(y int) { i.y = y - return i } // GetHeight returns the height of the progress bar @@ -135,9 +124,8 @@ func (i *ProgressBar) GetHeight() int { } // SetHeight Sets the height of the progress bar -func (i *ProgressBar) SetHeight(h int) *ProgressBar { +func (i *ProgressBar) SetHeight(h int) { i.height = h - return i } // GetWidth returns the width of the progress bar @@ -146,33 +134,29 @@ func (i *ProgressBar) GetWidth() int { } // SetWidth Sets the width of the progress bar -func (i *ProgressBar) SetWidth(w int) *ProgressBar { +func (i *ProgressBar) SetWidth(w int) { i.width = w - return i } // GetBackground Return the current background color of the modal func (i *ProgressBar) GetBackground() termbox.Attribute { return i.bg } // SetBackground Set the current background color to bg -func (i *ProgressBar) SetBackground(bg termbox.Attribute) *ProgressBar { +func (i *ProgressBar) SetBackground(bg termbox.Attribute) { i.bg = bg - return i } // GetForeground Return the current foreground color func (i *ProgressBar) GetForeground() termbox.Attribute { return i.fg } // SetForeground Set the foreground color to fg -func (i *ProgressBar) SetForeground(fg termbox.Attribute) *ProgressBar { +func (i *ProgressBar) SetForeground(fg termbox.Attribute) { i.fg = fg - return i } // Align Tells which direction the progress bar empties -func (i *ProgressBar) Align(a TextAlignment) *ProgressBar { +func (i *ProgressBar) Align(a TextAlignment) { i.alignment = a - return i } // SetColorized sets whether the progress bar should be colored @@ -180,9 +164,8 @@ func (i *ProgressBar) Align(a TextAlignment) *ProgressBar { // 10% - Red // 50% - Yellow // 80% - Green -func (i *ProgressBar) SetColorized(c bool) *ProgressBar { +func (i *ProgressBar) SetColorized(c bool) { i.colorized = c - return i } // HandleKeyPress accepts the termbox event and returns whether it was consumed diff --git a/termbox_scrollframe.go b/termbox_scrollframe.go new file mode 100644 index 0000000..5d31f13 --- /dev/null +++ b/termbox_scrollframe.go @@ -0,0 +1,155 @@ +package termboxUtil + +import "github.com/nsf/termbox-go" + +// ScrollFrame is a frame for holding other elements +// It manages it's own x/y, tab index +type ScrollFrame struct { + x, y, width, height int + scrollX, scrollY int + tabIdx int + fg, bg termbox.Attribute + bordered bool + controls []termboxControl +} + +// CreateScrollFrame creates Scrolling Frame at x, y that is w by h +func CreateScrollFrame(x, y, w, h int, fg, bg termbox.Attribute) *ScrollFrame { + s := ScrollFrame{x: x, y: y, width: w, height: h, fg: fg, bg: bg} + return &s +} + +// GetX returns the x position of the scroll frame +func (s *ScrollFrame) GetX() int { return s.x } + +// SetX sets the x position of the scroll frame +func (s *ScrollFrame) SetX(x int) { + s.x = x +} + +// GetY returns the y position of the scroll frame +func (s *ScrollFrame) GetY() int { return s.y } + +// SetY sets the y position of the scroll frame +func (s *ScrollFrame) SetY(y int) { + s.y = y +} + +// GetWidth returns the current width of the scroll frame +func (s *ScrollFrame) GetWidth() int { return s.width } + +// SetWidth sets the current width of the scroll frame +func (s *ScrollFrame) SetWidth(w int) { + s.width = w +} + +// GetHeight returns the current height of the scroll frame +func (s *ScrollFrame) GetHeight() int { return s.height } + +// SetHeight sets the current height of the scroll frame +func (s *ScrollFrame) SetHeight(h int) { + s.height = h +} + +// IsBordered returns true or false if this scroll frame has a border +func (s *ScrollFrame) IsBordered() bool { return s.bordered } + +// SetBordered sets whether we render a border around the scroll frame +func (s *ScrollFrame) SetBordered(b bool) { + s.bordered = b +} + +// GetScrollX returns the x distance scrolled +func (s *ScrollFrame) GetScrollX() int { + return s.scrollX +} + +// GetScrollY returns the y distance scrolled +func (s *ScrollFrame) GetScrollY() int { + return s.scrollY +} + +// ScrollDown scrolls the frame down +func (s *ScrollFrame) ScrollDown() { + s.scrollY++ +} + +// ScrollUp scrolls the frame up +func (s *ScrollFrame) ScrollUp() { + if s.scrollY > 0 { + s.scrollY-- + } +} + +// ScrollLeft scrolls the frame left +func (s *ScrollFrame) ScrollLeft() { + if s.scrollX > 0 { + s.scrollX-- + } +} + +// ScrollRight scrolls the frame right +func (s *ScrollFrame) ScrollRight() { + s.scrollX++ +} + +// AddControl adds a control to the frame +func (s *ScrollFrame) AddControl(t termboxControl) { + s.controls = append(s.controls, t) +} + +// DrawControl figures out the relative position of the control, +// sets it, draws it, then resets it. +func (s *ScrollFrame) DrawControl(t termboxControl) { + if s.IsVisible(t) { + ctlX, ctlY := t.GetX(), t.GetY() + t.SetX((s.GetX() + ctlX)) + t.SetY((s.GetY() + ctlY)) + t.Draw() + t.SetX(ctlX) + t.SetY(ctlY) + } +} + +// IsVisible takes a Termbox Control and returns whether +// that control would be visible in the frame +func (s *ScrollFrame) IsVisible(t termboxControl) bool { + // Check if any part of t should be visible + cX, cY := t.GetX(), t.GetY() + if cX+t.GetWidth() >= s.scrollX && cX <= s.scrollX+s.width { + return cY+t.GetHeight() >= s.scrollY && cY <= s.scrollY+s.height + } + return false +} + +// HandleKeyPress accepts the termbox event and returns whether it was consumed +func (s *ScrollFrame) HandleKeyPress(event termbox.Event) bool { + return false +} + +// DrawToStrings generates a slice of strings with what should +// be drawn to the screen +func (s *ScrollFrame) DrawToStrings() []string { + return []string{} +} + +// Draw outputs the Scoll Frame on the screen +func (s *ScrollFrame) Draw() { + maxWidth := s.width + maxHeight := s.height + x, y := s.x, s.y + startX := s.x + startY := s.y + if s.bordered { + DrawBorder(s.x, s.y, s.x+s.width, s.y+s.height, s.fg, s.bg) + maxWidth-- + maxHeight-- + x++ + y++ + startX++ + startY++ + } + for i := range s.controls { + s.DrawControl(s.controls[i]) + } +} diff --git a/termbox_util.go b/termbox_util.go index 40e6a24..25d12a9 100644 --- a/termbox_util.go +++ b/termbox_util.go @@ -7,6 +7,19 @@ import ( "github.com/nsf/termbox-go" ) +type termboxControl interface { + GetX() int + SetX(int) + GetY() int + SetY(int) + GetWidth() int + SetWidth(int) + GetHeight() int + SetHeight(int) + HandleKeyPress(termbox.Event) bool + Draw() +} + // TextAlignment is an int value for how we're aligning text type TextAlignment int From d3974ccd9600ea3c4553781e0d4a50b03f4242b7 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Wed, 27 Jan 2016 13:42:58 -0600 Subject: [PATCH 27/27] Several Changes Better Menu Better InputModal Also added AlertModal --- termbox_alertmodal.go | 166 ++++++++++++++++++++++++++++++++++++++++++ termbox_inputmodal.go | 1 + termbox_menu.go | 32 +++++--- 3 files changed, 190 insertions(+), 9 deletions(-) create mode 100644 termbox_alertmodal.go diff --git a/termbox_alertmodal.go b/termbox_alertmodal.go new file mode 100644 index 0000000..8efccb7 --- /dev/null +++ b/termbox_alertmodal.go @@ -0,0 +1,166 @@ +package termboxUtil + +import ( + "github.com/nsf/termbox-go" +) + +// AlertModal is a modal with yes/no (or similar) buttons +type AlertModal struct { + title string + text string + x, y, width, height int + showHelp bool + cursor int + bg, fg termbox.Attribute + isDone bool + accepted bool + value string + isVisible bool +} + +// CreateAlertModal Creates a confirmation modal with the specified attributes +func CreateAlertModal(title string, x, y, width, height int, fg, bg termbox.Attribute) *AlertModal { + i := AlertModal{title: title, x: x, y: y, width: width, height: height, fg: fg, bg: bg} + if i.title == "" && i.text == "" { + i.title = "Alert!" + } + i.showHelp = true + return &i +} + +// GetTitle returns the current title of the modal +func (i *AlertModal) GetTitle() string { return i.title } + +// SetTitle sets the current title of the modal to s +func (i *AlertModal) SetTitle(s string) { + i.title = s +} + +// GetText returns the current text of the modal +func (i *AlertModal) GetText() string { return i.text } + +// SetText sets the text of the modal to s +func (i *AlertModal) SetText(s string) { + i.text = s +} + +// GetX returns the current x coordinate of the modal +func (i *AlertModal) GetX() int { return i.x } + +// SetX sets the current x coordinate of the modal to x +func (i *AlertModal) SetX(x int) { + i.x = x +} + +// GetY returns the current y coordinate of the modal +func (i *AlertModal) GetY() int { return i.y } + +// SetY sets the current y coordinate of the modal to y +func (i *AlertModal) SetY(y int) { + i.y = y +} + +// GetWidth returns the current width of the modal +func (i *AlertModal) GetWidth() int { return i.width } + +// SetWidth sets the current modal width to width +func (i *AlertModal) SetWidth(width int) { + i.width = width +} + +// GetHeight returns the current height of the modal +func (i *AlertModal) GetHeight() int { return i.height } + +// SetHeight set the height of the modal to height +func (i *AlertModal) SetHeight(height int) { + i.height = height +} + +// HelpIsShown returns true or false if the help is displayed +func (i *AlertModal) HelpIsShown() bool { return i.showHelp } + +// ShowHelp sets whether or not to display the help text +func (i *AlertModal) ShowHelp(b bool) { + i.showHelp = b +} + +// GetBackground returns the current background color +func (i *AlertModal) GetBackground() termbox.Attribute { return i.bg } + +// SetBackground sets the background color to bg +func (i *AlertModal) SetBackground(bg termbox.Attribute) { + i.bg = bg +} + +// GetForeground returns the current foreground color +func (i *AlertModal) GetForeground() termbox.Attribute { return i.fg } + +// SetForeground sets the current foreground color to fg +func (i *AlertModal) SetForeground(fg termbox.Attribute) { + i.fg = fg +} + +// IsDone returns whether the user has answered the modal +func (i *AlertModal) IsDone() bool { return i.isDone } + +// SetDone sets whether the modal has completed it's purpose +func (i *AlertModal) SetDone(b bool) { + i.isDone = b +} + +// Show sets the visibility flag of the modal to true +func (i *AlertModal) Show() { + i.isVisible = true +} + +// Hide sets the visibility flag of the modal to false +func (i *AlertModal) Hide() { + i.isVisible = false +} + +// IsAccepted returns whether the user accepted the modal +func (i *AlertModal) IsAccepted() bool { return i.accepted } + +// Clear clears all of the non-positional parameters of the modal +func (i *AlertModal) Clear() { + i.title = "" + i.text = "" + i.accepted = false + i.isDone = false +} + +// HandleKeyPress handles the termbox event and returns whether it was consumed +func (i *AlertModal) HandleKeyPress(event termbox.Event) bool { + if event.Key == termbox.KeyEnter { + i.isDone = true + return true + } + return false +} + +// Draw draws the modal +func (i *AlertModal) Draw() { + // First blank out the area we'll be putting the modal + FillWithChar(' ', i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + // Now draw the border + DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) + + nextY := i.y + 1 + // The title + if i.title != "" { + DrawStringAtPoint(i.title, i.x+1, nextY, i.fg, i.bg) + nextY++ + FillWithChar('-', i.x+1, nextY, i.x+i.width-1, nextY, i.fg, i.bg) + nextY++ + } + if i.text != "" { + DrawStringAtPoint(i.text, i.x+1, nextY, i.fg, i.bg) + nextY++ + } + nextY += 2 + if i.showHelp { + helpString := "Press Enter to Continue" + helpX := (i.x + i.width) - len(helpString) - 1 + DrawStringAtPoint(helpString, helpX, nextY, i.fg, i.bg) + } +} diff --git a/termbox_inputmodal.go b/termbox_inputmodal.go index 50ca646..115449e 100644 --- a/termbox_inputmodal.go +++ b/termbox_inputmodal.go @@ -23,6 +23,7 @@ func CreateInputModal(title string, x, y, width, height int, fg, bg termbox.Attr i.input = CreateInputField(i.x+1, i.y+3, i.width-2, 2, i.fg, i.bg) i.showHelp = true i.input.bordered = true + i.isVisible = true return &i } diff --git a/termbox_menu.go b/termbox_menu.go index c9a5ad2..cf96e10 100644 --- a/termbox_menu.go +++ b/termbox_menu.go @@ -60,6 +60,7 @@ func (i *Menu) SetOptionsFromStrings(opts []string) { newOpts = append(newOpts, *CreateOptionFromText(v)) } i.SetOptions(newOpts) + i.SetSelectedOption(i.GetOptionFromIndex(0)) } // GetX returns the current x coordinate of the menu @@ -152,6 +153,7 @@ func (i *Menu) SelectPrevOption() { testOption := i.GetOptionFromIndex(idx) if testOption != nil && !testOption.IsDisabled() { i.SetSelectedOption(testOption) + return } } } @@ -164,6 +166,7 @@ func (i *Menu) SelectNextOption() { testOption := i.GetOptionFromIndex(idx) if testOption != nil && !testOption.IsDisabled() { i.SetSelectedOption(testOption) + return } } } @@ -292,23 +295,34 @@ func (i *Menu) Draw() { } } - // If the currently selected option is disabled, move to the next - if i.GetSelectedOption().IsDisabled() { - i.SelectNextOption() - } - - // Print the options if len(i.options) > 0 { + // If the currently selected option is disabled, move to the next + if i.GetSelectedOption().IsDisabled() { + i.SelectNextOption() + } + + // Print the options for idx := range i.options { + if i.GetSelectedIndex()-idx >= optionHeight-1 { + // Skip this one + continue + } currOpt := &i.options[idx] + outTxt := currOpt.GetText() + if len(outTxt) >= i.width { + outTxt = outTxt[:i.width] + } if currOpt.IsDisabled() { - DrawStringAtPoint(currOpt.GetText(), optionStartX, optionStartY, i.disabledFg, i.disabledBg) + DrawStringAtPoint(outTxt, optionStartX, optionStartY, i.disabledFg, i.disabledBg) } else if i.GetSelectedOption() == currOpt { - DrawStringAtPoint(AlignText(currOpt.GetText(), optionWidth, AlignLeft), optionStartX, optionStartY, i.selectedFg, i.selectedBg) + DrawStringAtPoint(AlignText(outTxt, optionWidth, AlignLeft), optionStartX, optionStartY, i.selectedFg, i.selectedBg) } else { - DrawStringAtPoint(currOpt.GetText(), optionStartX, optionStartY, i.fg, i.bg) + DrawStringAtPoint(outTxt, optionStartX, optionStartY, i.fg, i.bg) } optionStartY++ + if optionStartY > i.y+optionHeight-1 { + break + } } } }