commit a4d4c49de97543719964714a55065b3f463b9b6e Author: Sagi Dayan Date: Tue Jan 16 17:21:35 2024 +0200 initial commit Signed-off-by: Sagi Dayan diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e9418d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +vendor/ diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..8b44f70 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,76 @@ +# This file contains all available configuration options +# with their default values. + +# options for analysis running +run: + # default concurrency is a available CPU number + concurrency: 4 + + # timeout for analysis, e.g. 30s, 5m, default is 1m + timeout: 10m + + # exit code when at least one issue was found, default is 1 + issues-exit-code: 1 + + # include test files or not, default is true + tests: true + + # which dirs to skip: issues from them won't be reported; + # can use regexp here: generated.*, regexp is applied on full path; + # default value is empty list, but default dirs are skipped independently + # from this option's value (see skip-dirs-use-default). + skip-dirs: + - bin + - deploy + - docs + - examples + - hack + - packaging + - reports + +# output configuration options +output: + # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" + format: colored-line-number + + # print lines of code with issue, default is true + print-issued-lines: true + + # print linter name in the end of issue text, default is true + print-linter-name: true + + # make issues output unique by line, default is true + uniq-by-line: true + +issues: + # List of regexps of issue texts to exclude, empty list by default. + # But independently from this option we use default exclude patterns, + # it can be disabled by `exclude-use-default: false`. To list all + # excluded by default patterns execute `golangci-lint run --help` + exclude: + - 'declaration of "err" shadows declaration at' + +linters: + enable: + - megacheck + - govet + - gocyclo + - gofmt + - gosec + - megacheck + - unconvert + - gci + - goimports + - exportloopref + +linters-settings: + govet: + check-shadowing: true + + settings: + printf: + funcs: + - Infof + - Warnf + - Errorf + - Fatalf diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f288702 --- /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. + + + Copyright (C) + + 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: + + Copyright (C) + 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 +. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9a411f3 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +BINARY_NAME=subsonic-tui +BUILD_FOLDER=build + + +.PHONY: build +build: + GOARCH=amd64 GOOS=darwin go build -o ${BUILD_FOLDER}/${BINARY_NAME}-darwin main.go + GOARCH=amd64 GOOS=linux go build -o ${BUILD_FOLDER}/${BINARY_NAME}-linux main.go + GOARCH=amd64 GOOS=windows go build -o ${BUILD_FOLDER}/${BINARY_NAME}-windows main.go + +run: build + ./${BINARY_NAME} + +clean: + go clean + rm ${BINARY_NAME}-darwin + rm ${BINARY_NAME}-linux + rm ${BINARY_NAME}-windows + +test: + go test ./... + +test_coverage: + go test ./... -coverprofile=coverage.out + +dep: + go mod download + +vet: + go vet + +lint: + golangci-lint run --enable-all diff --git a/README.md b/README.md new file mode 100644 index 0000000..6a919a7 --- /dev/null +++ b/README.md @@ -0,0 +1,91 @@ +# Subsonic TUI + +**NOTE ⚠ !!: This is under heavy development! Do not fork just yet. Im still force pushing to main!** + +A Subsonic client and player written in go. + +subsonictui (Name in progress) is a simple and easy to use player for Linux, Mac and (yes...) Windows. + +

+ Screenshot + Screenshot mini player +

+ +> Note: Screenshots blurred for (I dont know) copyright issues? + +## Features + + - [x] Browse Artists/Albums/Playlists + - [x] Artist view + - [x] Playlist view + - [x] Miniplayer on small screen + - [x] Album view + - [x] Search + - [x] Playback + - [x] Scrobble (configurable) + - [x] Play album + - [x] Shuffle album + - [x] Add album to queue + - [x] Add song to queue + - [x] Play Next/Prev + - [x] Stop/Pause + - [x] Generate artist radio + - [x] Generate song radio + - [-] Desktop integration + - [x] Linux (MPRIS) + - [ ] Windows + - [ ] MacOS + - [ ] Playlist management + - [ ] Add song to playlist + - [ ] Create playlist from queue + + +## Keybindings +| Key(s) | Action | +|------------------------------------------------------------------------------|-------------------------------------------------| +| ` | Focus on Main pane | +| 1 | Focus on Atrists pane | +| 2 | Focus on Albums pane | +| 3 | Focus on Playlists pane | +| 4 | Focus on Queue pane | +| Arrow keys or h j k l | Navigation in a pane. Shift for switching panes | +| g | Jump to first item in a list of a pane | +| G | Jump to last item in a list of a pane | +| n | Next song | +| N | Prev song | +| q | Exit | +| r | While on a song - Start Song radio | +| s | Playback - Stop | +| p | Playback - Toggle Play/Pause | +| c | Stop, clear queue | +| / | Search | +| ? | Help | + +## Config +`subsonictui` stores a config file at: +- Linux: `$HOME/.config/subsonictui/config.yaml` +- macOS: `$HOME/Library/Application Support/subsonictui/config.yaml` +- Windows: `C:\\Users\%USER%\AppData\Roaming\subsonictui\config.yaml` + + +## Development +### Build Dependencies +subsonictui uses [Beep](https://github.com/faiface/beep), that uses [OTO](https://github.com/hajimehoshi/oto) under the hood, so you will need OTO dependencies. +Mainly on Linux you will need `alsa-devel`. + +To Build the project: +``` +$ make build +``` + +## Special Thanks +subsonictui is built on top of a few projects. I would like to thank them here + - [go-subsonic](https://github.com/delucks/go-subsonic) + - [OTO](https://github.com/hajimehoshi/oto) + - [Beep](https://github.com/faiface/beep) + - [tview](https://github.com/rivo/tview) + - [tcell](https://github.com/gdamore/tcell) + - [MPRIS server](https://github.com/quarckster/go-mpris-server) + +Thank you for creating wondeful software that everyone can use. Including myself. + diff --git a/assets/screenshot-miniplayer.png b/assets/screenshot-miniplayer.png new file mode 100644 index 0000000..e81e645 Binary files /dev/null and b/assets/screenshot-miniplayer.png differ diff --git a/assets/screenshot.png b/assets/screenshot.png new file mode 100644 index 0000000..ed18319 Binary files /dev/null and b/assets/screenshot.png differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8cedf7f --- /dev/null +++ b/go.mod @@ -0,0 +1,28 @@ +module git.dayanhub.com/sagi/subsonic-tui + +go 1.21.5 + +require ( + github.com/creasty/defaults v1.7.0 + github.com/delucks/go-subsonic v0.0.0-20220915164742-2744002c4be5 + github.com/gdamore/tcell/v2 v2.7.4 + github.com/godbus/dbus/v5 v5.1.0 + github.com/gopxl/beep v1.4.0 + github.com/quarckster/go-mpris-server v1.0.3 + github.com/rivo/tview v0.0.0-20240307173318-e804876934a1 + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + github.com/ebitengine/oto/v3 v3.1.1 // indirect + github.com/ebitengine/purego v0.6.1 // indirect + github.com/gdamore/encoding v1.0.1 // indirect + github.com/hajimehoshi/go-mp3 v0.3.4 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/term v0.18.0 // indirect + golang.org/x/text v0.14.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1f053de --- /dev/null +++ b/go.sum @@ -0,0 +1,83 @@ +github.com/creasty/defaults v1.7.0 h1:eNdqZvc5B509z18lD8yc212CAqJNvfT1Jq6L8WowdBA= +github.com/creasty/defaults v1.7.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/delucks/go-subsonic v0.0.0-20220915164742-2744002c4be5 h1:RuuxidatioSKGOiBzL1mTY4X22DQD8weEbS3iRLHnAg= +github.com/delucks/go-subsonic v0.0.0-20220915164742-2744002c4be5/go.mod h1:vnbEuj6Z20PLcHB4rrLQAOXGMjtULfMGhRVSFPcSdUo= +github.com/ebitengine/oto/v3 v3.1.1 h1:utFNkSF4yXqA7VhMg7oHp3OSdz3vuzJQ42rCDnd8pc8= +github.com/ebitengine/oto/v3 v3.1.1/go.mod h1:bQM4zk9glIVjTynn8X0Lp1zngTlZltFFfzJvx543vdA= +github.com/ebitengine/purego v0.6.1 h1:sjN8rfzbhXQ59/pE+wInswbU9aMDHiwlup4p/a07Mkg= +github.com/ebitengine/purego v0.6.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= +github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= +github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw= +github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo= +github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU= +github.com/gdamore/tcell/v2 v2.7.4/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg= +github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= +github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gopxl/beep v1.4.0 h1:pJERVDZMJkf49R1g/tV9DhVct4xNRuTlyMnMa53gGsc= +github.com/gopxl/beep v1.4.0/go.mod h1:gGVz7MJKlfHrmkzr0wSLGNyY7oisM6rFWJnaLjNxEwA= +github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68= +github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo= +github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/quarckster/go-mpris-server v1.0.3 h1:ef6d3DpxlORtdEBHnhQ/j3gS0Z3+YUfXeJhC9L9DZvA= +github.com/quarckster/go-mpris-server v1.0.3/go.mod h1:2b4IdrpnEoEfU+6fQKjYhAgdvsiz4JxmTpDAUrMJVO4= +github.com/rivo/tview v0.0.0-20240307173318-e804876934a1 h1:bWLHTRekAy497pE7+nXSuzXwwFHI0XauRzz6roUvY+s= +github.com/rivo/tview v0.0.0-20240307173318-e804876934a1/go.mod h1:02iFIz7K/A9jGCvrizLPvoqr4cEIx7q54RH5Qudkrss= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= +golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= +golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/client/artwork_cache.go b/internal/client/artwork_cache.go new file mode 100644 index 0000000..c3049e6 --- /dev/null +++ b/internal/client/artwork_cache.go @@ -0,0 +1,97 @@ +package client + +import ( + "fmt" + "image" + "image/jpeg" + "os" + "path" +) + +type artcache struct { + artPaths map[string]string + cacheDir string +} + +var ArtCache *artcache + +func (c *artcache) saveArt(id string, img image.Image) *string { + path := c.GetPath(id) + if path != nil { + return path + } + + path = c.saveImage(id, img) + if path != nil { + c.artPaths[id] = *path + } + return path +} + +func (c *artcache) saveImage(id string, img image.Image) *string { + filePath := c.filepath(id) + f, err := os.Create(filePath) + + defer func() { + _ = f.Close() + }() + + if err != nil { + return nil + } + err = jpeg.Encode(f, img, nil) + if err != nil { + return nil + } + return &filePath +} + +func (c *artcache) GetPath(id string) *string { + if path, ok := c.artPaths[id]; ok { + return &path + } + return nil +} + +func (c *artcache) GetImage(id string) *image.Image { + path := c.GetPath(id) + if path == nil { + return nil + } + + f, err := os.Open(*path) + if err != nil { + return nil + } + + defer func() { + _ = f.Close() + }() + + img, err := jpeg.Decode(f) + if err != nil { + return nil + } + return &img +} + +func (c *artcache) filepath(id string) string { + return path.Join(c.cacheDir, fmt.Sprintf("%s.jpg", id)) +} + +func (c *artcache) Destroy() { + os.RemoveAll(c.cacheDir) +} + +func init() { + tmpDir := os.TempDir() + cacheDir := path.Join(tmpDir, fmt.Sprintf("subsonic-tui-%d", os.Getpid())) + err := os.Mkdir(cacheDir, 0777) + if err != nil { + panic("Failed to create cacheDir") + } + ArtCache = &artcache{ + cacheDir: cacheDir, + artPaths: make(map[string]string), + } +} diff --git a/internal/client/client.go b/internal/client/client.go new file mode 100644 index 0000000..2115e82 --- /dev/null +++ b/internal/client/client.go @@ -0,0 +1,163 @@ +package client + +import ( + "fmt" + "image" + "io" + "net/http" + "sync" + + "git.dayanhub.com/sagi/subsonic-tui/internal/common" + "github.com/delucks/go-subsonic" +) + +type Client struct { + client subsonic.Client +} + +func NewClient(baseURL string) *Client { + var client subsonic.Client = subsonic.Client{ + Client: &http.Client{}, + ClientName: "subsonic-tui", + BaseUrl: baseURL, + PasswordAuth: true, + } + + return &Client{ + client: client, + } +} + +func (c *Client) Authenticate(username, password string) error { + c.client.User = username + return c.client.Authenticate(password) +} + +func (c *Client) GetUser() (*subsonic.User, error) { + return c.client.GetUser(c.client.User) +} + +func (c *Client) GetPlaylists() ([]*subsonic.Playlist, error) { + return c.client.GetPlaylists(map[string]string{}) +} + +func (c *Client) GetPlaylist(ID string) (*subsonic.Playlist, error) { + return c.client.GetPlaylist(ID) +} + +func (c *Client) GetArtists() ([]*subsonic.ArtistID3, error) { + indexes, err := c.client.GetArtists(map[string]string{}) + if err != nil { + return nil, err + } + artists := []*subsonic.ArtistID3{} + for _, i := range indexes.Index { + artists = append(artists, i.Artist...) + } + + return artists, nil +} + +func (c *Client) GetAlbums() ([]*subsonic.AlbumID3, error) { + return c.client.GetAlbumList2("alphabeticalByName", map[string]string{ + "size": "500", + }) +} + +func (c *Client) GetArtist(ID string) (*subsonic.ArtistID3, error) { + return c.client.GetArtist(ID) +} + +func (c *Client) GetArtistInfo(ID string) (*subsonic.ArtistInfo2, error) { + return c.client.GetArtistInfo2(ID, map[string]string{ + "count": "20", + }) +} + +func (c *Client) GetAlbum(ID string) (*subsonic.AlbumID3, error) { + return c.client.GetAlbum(ID) +} + +func (c *Client) GetCoverArt(ID string) (image.Image, error) { + if img := ArtCache.GetImage(ID); img != nil { + return *img, nil + } + img, err := c.client.GetCoverArt(ID, map[string]string{ + //"size": "64", + }) + if err != nil { + return nil, err + } + ArtCache.saveArt(ID, img) + return img, err +} + +func (c *Client) GetSimilarSongs(artistID string, maxSongs int) ([]*subsonic.Child, error) { + max := fmt.Sprintf("%d", maxSongs) + return c.client.GetSimilarSongs2(artistID, map[string]string{ + "count": max, + }) +} + +func (c *Client) Stream(ID string) (io.Reader, error) { + return c.client.Stream(ID, map[string]string{ + "format": "mp3", + }) +} + +func (c *Client) Scrobble(ID string) error { + return c.client.Scrobble(ID, map[string]string{}) +} + +func (c *Client) GetTopSongs(name string, max int) ([]*subsonic.Child, error) { + count := fmt.Sprintf("%d", max) + return c.client.GetTopSongs(name, map[string]string{ + "count": count, + }) +} + +func (c *Client) Search(query string) (*subsonic.SearchResult3, error) { + return c.client.Search3(query, map[string]string{ + "artistCount": "20", + "songCount": "20", + "albumCount": "20", + }) +} + +func (c *Client) GetExperimentalArtistRadio(artistId3 *subsonic.ArtistID3, info *subsonic.ArtistInfo2, max int) ([]*subsonic.Child, error) { + var wg sync.WaitGroup + ID := artistId3.ID + similarArtists := info.SimilarArtist + songs := []*subsonic.Child{} + similarArtistsSongs := 10 + thisArtistFactor := 3 + portion := len(info.SimilarArtist) * similarArtistsSongs * thisArtistFactor + wg.Add(2) + go func() { + s, _ := c.GetSimilarSongs(ID, portion) + songs = append(songs, s...) + wg.Done() + }() + go func() { + s, _ := c.GetTopSongs(artistId3.Name, similarArtistsSongs) + songs = append(songs, s...) + wg.Done() + }() + common.ShuffleSlice(similarArtists) + for _, a := range similarArtists { + wg.Add(1) + artist := a + go func() { + s, _ := c.GetSimilarSongs(artist.ID, similarArtistsSongs) + songs = append(songs, s...) + wg.Done() + }() + } + wg.Wait() + if max > len(songs) { + max = len(songs) + } + songs = songs[:max] + common.ShuffleSlice(songs) + return songs, nil +} diff --git a/internal/common/shuffle.go b/internal/common/shuffle.go new file mode 100644 index 0000000..82d420f --- /dev/null +++ b/internal/common/shuffle.go @@ -0,0 +1,21 @@ +package common + +import ( + "crypto/rand" + "math/big" + "reflect" +) + +func ShuffleSlice(slice interface{}) { + rv := reflect.ValueOf(slice) + swap := reflect.Swapper(slice) + + length := rv.Len() + for i := length - 1; i > 0; i-- { + j, err := rand.Int(rand.Reader, big.NewInt(int64(i+1))) + if err != nil { + panic("Shuffle error") + } + swap(i, int(j.Int64())) + } +} diff --git a/internal/config/colors.go b/internal/config/colors.go new file mode 100644 index 0000000..5442938 --- /dev/null +++ b/internal/config/colors.go @@ -0,0 +1,21 @@ +package config + +import ( + "github.com/gdamore/tcell/v2" +) + +const ( + ColorBackground = tcell.ColorDefault + ColorSelectedBoarder = tcell.ColorRed + ColorBluredBoarder = tcell.ColorWhite + ColorText = tcell.ColorWhite + ColorTextAccent = tcell.ColorYellow + ColorPlaybackProgressElapsed = tcell.ColorLightCyan + ColorPlaybackProgressRemaining = tcell.ColorBlack + ColorQueuePlayedBg = tcell.ColorBlack + ColorQueuePlayingBg = tcell.ColorDarkRed + ColorButtonBg = tcell.ColorBlue + ColorButtonTxt = tcell.ColorBlack + ColorButtonSelectedBg = tcell.ColorYellow + ColorButtonSelectedTxt = tcell.ColorDarkRed +) diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..7d10602 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,123 @@ +package config + +import ( + "encoding/base64" + "fmt" + "os" + "path" + "strings" + + "github.com/creasty/defaults" + "gopkg.in/yaml.v3" +) + +var configPath string + +type _config struct { + Username string `yaml:"username"` + Password string `yaml:"password"` + URL string `yaml:"url"` + EnableScrobble bool `yaml:"enable_scrobble" default:"false"` + MaxRadioSongs int `yaml:"max_radio_songs" default:"50"` + ExperimentalRadioAlgo bool `yaml:"experimental_radio_algo" default:"false"` +} + +var configStruct *_config + +func init() { + userConfigDir, err := os.UserConfigDir() + configDir := path.Join(userConfigDir, "subsonictui") + configPath = path.Join(configDir, "config.yaml") + + if err != nil { + fmt.Printf("[ERROR] Failed to fetch user config directory. %e\n", err) + os.Exit(1) + } + if _, err := os.Stat(configDir); os.IsNotExist(err) { + err := os.MkdirAll(configDir, 0700) + if err != nil { + panic(err) + } + } + var configFile *os.File + if _, err := os.Stat(configPath); os.IsNotExist(err) { + configFile, err = os.Create(configPath) + defer func() { + err := configFile.Close() + if err != nil { + panic(err) + } + }() + if err != nil { + fmt.Printf("[ERROR] Failed to create config file @ %s. %e\n", configPath, err) + os.Exit(1) + } + } + configStruct, err = loadConfig() + if err != nil { + fmt.Printf("[ERROR] Failed to load config file @ %s. %e\n", configPath, err) + os.Exit(1) + } + fmt.Printf("Init Config %s\n", configPath) +} + +func URL() string { + return configStruct.URL +} +func Username() string { + return configStruct.Username +} +func Password() string { + p, _ := base64.StdEncoding.DecodeString(configStruct.Password) + return strings.TrimSpace(string(p)) +} +func ScrobbleEnabled() bool { + return configStruct.EnableScrobble +} +func MaxRadioSongs() int { + return configStruct.MaxRadioSongs +} +func ExperimentalRadioAlgo() bool { + return configStruct.ExperimentalRadioAlgo +} + +func SetPassword(p string) { + configStruct.Password = base64.StdEncoding.EncodeToString([]byte(p)) +} + +func SetUsername(u string) { + configStruct.Username = u +} + +func SetURL(u string) { + configStruct.URL = u +} + +func loadConfig() (*_config, error) { + c := &_config{} + err := defaults.Set(c) + if err != nil { + panic(err) + } + file, err := os.ReadFile(configPath) + if err != nil { + return nil, err + } + if err = yaml.Unmarshal(file, c); err != nil { + return nil, err + } + return c, nil +} + +func SaveConfig() { + yml, err := yaml.Marshal(configStruct) + if err != nil { + fmt.Printf("[ERROR] Failed to convert config to yaml. %e\n", err) + os.Exit(1) + } + err = os.WriteFile(configPath, yml, 0600) + if err != nil { + fmt.Printf("[ERROR] Failed to save config file @ %s. %e\n", configPath, err) + os.Exit(1) + } +} diff --git a/internal/playback/controller.go b/internal/playback/controller.go new file mode 100644 index 0000000..69cb8b8 --- /dev/null +++ b/internal/playback/controller.go @@ -0,0 +1,223 @@ +package playback + +import ( + "io" + "time" + + "git.dayanhub.com/sagi/subsonic-tui/internal/client" + "github.com/delucks/go-subsonic" + "github.com/gopxl/beep" + "github.com/gopxl/beep/mp3" + "github.com/gopxl/beep/speaker" +) + +type PlaybackState int + +const ( + PlaybackStateStopped = iota + PlaybackStatePaused + PlaybackStatePlaying +) + +type Controller struct { + client *client.Client + stream beep.StreamSeekCloser + song *subsonic.Child + songElapsedFunc func(song *subsonic.Child, elapsed time.Duration) + initialFormat *beep.Format + currentFormat *beep.Format + position float64 + closeChan chan bool + songEndedChan chan bool + songEndedFunc func(song *subsonic.Child) + ctrl *beep.Ctrl + desktopPlayback DesktopPlayback + queue *queue + playbackState PlaybackState +} + +func NewController(client *client.Client) *Controller { + controller := &Controller{ + client: client, + closeChan: make(chan bool), + songEndedChan: make(chan bool), + playbackState: PlaybackStateStopped, + queue: newQueue(), + } + controller.desktopPlayback = desktopPlayer(controller) + controller.desktopPlayback.Start() + go controller.playbackTicker() + return controller +} + +func (c *Controller) State() PlaybackState { + return c.playbackState +} + +func (c *Controller) Play(song *subsonic.Child) { + if song == nil { + return + } + r, err := c.client.Stream(song.ID) + if err != nil { + //TODO: Log error + c.Stop() + return + } + c.Stream(r, song) +} +func (c *Controller) Next() { + song := c.queue.Next() + if song != nil { + c.Play(song) + } +} + +func (c *Controller) AddToQueue(songs []*subsonic.Child) { + shouldPlay := c.queue.Add(songs...) + if shouldPlay { + c.Play(c.queue.GetCurrentSong()) + } + c.desktopPlayback.OnPlaylistChanged() +} + +func (c *Controller) GetQueuePosition() int { + return c.queue.GetPosition() +} + +func (c *Controller) GetCurrentSong() *subsonic.Child { + return c.queue.GetCurrentSong() +} + +func (c *Controller) SetQueue(songs []*subsonic.Child) { + c.queue.Clear() + c.Stop() + c.queue.Set(songs) + c.Play(c.queue.GetCurrentSong()) + c.desktopPlayback.OnPlaylistChanged() +} + +func (c *Controller) SetQueuePosition(position int) { + s := c.queue.SetPosition(position) + c.Play(s) +} + +func (c *Controller) Prev() { + song := c.queue.Prev() + if song != nil { + c.Play(song) + } +} +func (c *Controller) GetQueue() []*subsonic.Child { + return c.queue.Get() +} + +func (c *Controller) ClearQueue() { + c.Stop() + c.queue.Clear() + c.desktopPlayback.OnPlayPause() + c.desktopPlayback.OnSongChanged() + c.desktopPlayback.OnPlaylistChanged() +} + +func (c *Controller) SetSongElapsedFunc(f func(sing *subsonic.Child, elapsed time.Duration)) { + c.songElapsedFunc = f +} + +func (c *Controller) SetSongEndedFunc(f func(song *subsonic.Child)) { + c.songEndedFunc = f +} + +func (c *Controller) Close() error { + c.Stop() + c.closeChan <- true + return nil +} + +func (c *Controller) TogglePlayPause() { + if c.playbackState != PlaybackStateStopped { + c.ctrl.Paused = !c.ctrl.Paused + if c.ctrl.Paused { + c.playbackState = PlaybackStatePaused + } else { + c.playbackState = PlaybackStatePlaying + } + } + c.desktopPlayback.OnPlayPause() +} + +func (c *Controller) Stop() { + if c.ctrl == nil { + return + } + speaker.Clear() + c.ctrl.Paused = true + c.playbackState = PlaybackStateStopped + c.ctrl = nil + c.stream = nil + c.songElapsedFunc(c.song, time.Duration(0)) + c.song = nil + c.position = 0 + c.desktopPlayback.OnPlayPause() + c.desktopPlayback.OnPlaylistChanged() +} + +func (c *Controller) playbackTicker() { + for { + select { + case <-c.closeChan: + return + case <-c.songEndedChan: + c.Stop() + c.Next() + default: + if c.playbackState == PlaybackStatePlaying && c.song != nil { + if c.stream != nil { + pos := c.stream.Position() + elapsed := c.currentFormat.SampleRate.D(pos).Round(time.Second) + c.position = elapsed.Seconds() + c.songElapsedFunc(c.song, elapsed) + c.desktopPlayback.OnPositionChanged(int(c.position)) + } + } + } + time.Sleep(time.Second) + } +} + +func (c *Controller) Stream(reader io.Reader, song *subsonic.Child) { + c.Stop() + // Ensure artwork cache... + _, _ = c.client.GetCoverArt(song.CoverArt) + + readerCloser := io.NopCloser(reader) + decodedMp3, format, err := mp3.Decode(readerCloser) + decodedMp3.Position() + if err != nil { + panic("mp3.NewDecoder failed: " + err.Error()) + } + + if c.initialFormat == nil { + c.initialFormat = &format + } + + c.currentFormat = &format + + var stream beep.Streamer = decodedMp3 + if err = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10)); err != nil { + stream = beep.Resample(3, format.SampleRate, c.initialFormat.SampleRate, decodedMp3) + } + + c.stream = decodedMp3 + c.song = song + + ctrl := &beep.Ctrl{Streamer: stream} + c.ctrl = ctrl + c.playbackState = PlaybackStatePlaying + speaker.Play(beep.Seq(ctrl, beep.Callback(func() { + c.songEndedFunc(song) + c.songEndedChan <- true + }))) + c.desktopPlayback.OnSongChanged() + c.desktopPlayback.OnPlayPause() +} diff --git a/internal/playback/desktop.go b/internal/playback/desktop.go new file mode 100644 index 0000000..f5f7d6f --- /dev/null +++ b/internal/playback/desktop.go @@ -0,0 +1,10 @@ +package playback + +type DesktopPlayback interface { + Start() + Stop() error + OnPlayPause() + OnPlaylistChanged() + OnSongChanged() + OnPositionChanged(sec int) +} diff --git a/internal/playback/mpris.go b/internal/playback/mpris.go new file mode 100644 index 0000000..dd7e83c --- /dev/null +++ b/internal/playback/mpris.go @@ -0,0 +1,249 @@ +package playback + +import ( + "encoding/base32" + "fmt" + + "git.dayanhub.com/sagi/subsonic-tui/internal/client" + "github.com/delucks/go-subsonic" + "github.com/godbus/dbus/v5" + "github.com/quarckster/go-mpris-server/pkg/events" + "github.com/quarckster/go-mpris-server/pkg/server" + . "github.com/quarckster/go-mpris-server/pkg/types" +) + +const ( + mprisPlayerNmae = "MehSonic" + mprisNoTrack = "/org/mpris/MediaPlayer2/TrackList/NoTrack" +) + +type mprisRoot struct{} + +func (r mprisRoot) Raise() error { + return nil +} +func (r mprisRoot) Quit() error { + return nil +} +func (r mprisRoot) CanQuit() (bool, error) { + return true, nil +} +func (r mprisRoot) CanRaise() (bool, error) { + return false, nil +} +func (r mprisRoot) HasTrackList() (bool, error) { + return false, nil +} +func (r mprisRoot) Identity() (string, error) { + return mprisPlayerNmae, nil +} +func (r mprisRoot) SupportedUriSchemes() ([]string, error) { + return []string{}, nil +} +func (r mprisRoot) SupportedMimeTypes() ([]string, error) { + return []string{}, nil +} + +type mprisPlayer struct { + ctrl *Controller +} + +// Implement other methods of `pkg.types.OrgMprisMediaPlayer2PlayerAdapter` +func (p mprisPlayer) Next() error { + p.ctrl.Next() + return nil +} +func (p mprisPlayer) Previous() error { + p.ctrl.Prev() + return nil +} +func (p mprisPlayer) Pause() error { + if p.ctrl.State() == PlaybackStatePlaying { + p.ctrl.TogglePlayPause() + } + return nil +} +func (p mprisPlayer) PlayPause() error { + switch p.ctrl.State() { + case PlaybackStatePaused, PlaybackStatePlaying: + p.ctrl.TogglePlayPause() + case PlaybackStateStopped: + p.ctrl.Play(p.ctrl.GetCurrentSong()) + } + return nil +} +func (p mprisPlayer) Stop() error { + p.ctrl.Stop() + return nil +} +func (p mprisPlayer) Play() error { + switch p.ctrl.State() { + case PlaybackStatePaused: + p.ctrl.TogglePlayPause() + case PlaybackStateStopped: + p.ctrl.Play(p.ctrl.GetCurrentSong()) + } + return nil +} +func (p mprisPlayer) Seek(offset Microseconds) error { + return nil +} +func (p mprisPlayer) SetPosition(trackId string, position Microseconds) error { + return nil +} +func (p mprisPlayer) OpenUri(uri string) error { + return nil +} +func (p mprisPlayer) PlaybackStatus() (PlaybackStatus, error) { + switch p.ctrl.State() { + case PlaybackStatePlaying: + return PlaybackStatusPlaying, nil + case PlaybackStatePaused: + return PlaybackStatusPaused, nil + case PlaybackStateStopped: + return PlaybackStatusStopped, nil + } + // Should not get here + return PlaybackStatusStopped, nil + +} +func (p mprisPlayer) Rate() (float64, error) { + return 1, nil +} +func (p mprisPlayer) SetRate(float64) error { + return nil +} +func (p mprisPlayer) Metadata() (Metadata, error) { + s := p.ctrl.GetCurrentSong() + objPath := mprisNoTrack + if s != nil { + objPath = encodeTrackId(s.ID) + } else { + s = &subsonic.Child{} + } + md := Metadata{ + TrackId: dbus.ObjectPath(objPath), + Length: secondsToMicroseconds(s.Duration), + Title: s.Title, + Album: s.Album, + Artist: []string{s.Artist}, + DiscNumber: s.DiscNumber, + Genre: []string{s.Genre}, + TrackNumber: s.Track, + UserRating: float64(s.UserRating), + UseCount: int(s.PlayCount), + } + artw := client.ArtCache.GetPath(s.CoverArt) + if artw != nil { + md.ArtUrl = fmt.Sprintf("file://%s", *artw) + } + return md, nil +} +func (p mprisPlayer) Volume() (float64, error) { + return 1, nil +} +func (p mprisPlayer) SetVolume(float64) error { + return nil +} +func (p mprisPlayer) Position() (int64, error) { + return int64(secondsToMicroseconds(int(p.ctrl.position))), nil +} +func (p mprisPlayer) MinimumRate() (float64, error) { + return 1, nil +} +func (p mprisPlayer) MaximumRate() (float64, error) { + return 1, nil +} +func (p mprisPlayer) CanGoNext() (bool, error) { + return p.ctrl.queue.HasNext(), nil +} +func (p mprisPlayer) CanGoPrevious() (bool, error) { + return p.ctrl.queue.HasPrev(), nil +} +func (p mprisPlayer) CanPlay() (bool, error) { + return p.ctrl.GetCurrentSong() != nil, nil +} +func (p mprisPlayer) CanPause() (bool, error) { + return true, nil +} +func (p mprisPlayer) CanSeek() (bool, error) { + return false, nil +} +func (p mprisPlayer) CanControl() (bool, error) { + return true, nil +} + +var _ DesktopPlayback = &mprisPlayback{} + +type mprisPlayback struct { + root mprisRoot + player mprisPlayer + eventHandler *events.EventHandler + server *server.Server + err error +} + +func (p *mprisPlayback) Start() { + go func() { + p.err = p.server.Listen() + }() +} + +func (p *mprisPlayback) Stop() error { + return p.server.Stop() +} + +func (p *mprisPlayback) OnPlayPause() { + if p.err != nil { + return + } + p.err = p.eventHandler.Player.OnPlayPause() +} +func (p *mprisPlayback) OnPlaylistChanged() { + if p.err != nil { + return + } + p.err = p.eventHandler.Player.OnOptions() +} +func (p *mprisPlayback) OnSongChanged() { + if p.err != nil { + return + } + p.err = p.eventHandler.Player.OnTitle() +} + +func (p *mprisPlayback) OnPositionChanged(position int) { + if p.err != nil { + return + } + p.err = p.eventHandler.Player.OnSeek(secondsToMicroseconds(position)) + if p.err != nil { + return + } + p.err = p.eventHandler.Player.OnOptions() +} + +func desktopPlayer(c *Controller) DesktopPlayback { + r := mprisRoot{} + p := mprisPlayer{ + ctrl: c, + } + s := server.NewServer(mprisPlayerNmae, r, p) + ev := events.NewEventHandler(s) + + return &mprisPlayback{ + root: r, + player: p, + server: s, + eventHandler: ev, + } +} + +func secondsToMicroseconds(s int) Microseconds { + return Microseconds(s * 1_000_000) +} + +func encodeTrackId(id string) string { + data := []byte(id) + return fmt.Sprintf("/%s/Track/%s", mprisPlayerNmae, base32.StdEncoding.WithPadding('0').EncodeToString(data)) +} diff --git a/internal/playback/queue.go b/internal/playback/queue.go new file mode 100644 index 0000000..2800f28 --- /dev/null +++ b/internal/playback/queue.go @@ -0,0 +1,84 @@ +package playback + +import "github.com/delucks/go-subsonic" + +type queue struct { + currentSong int + songQueue []*subsonic.Child +} + +func newQueue() *queue { + return &queue{ + currentSong: 0, + songQueue: []*subsonic.Child{}, + } +} + +func (q *queue) Set(songs []*subsonic.Child) { + q.currentSong = 0 + q.songQueue = songs +} + +func (q *queue) Clear() { + q.currentSong = 0 + q.songQueue = []*subsonic.Child{} +} + +// returns true if queue was empty before addition +func (q *queue) Add(songs ...*subsonic.Child) bool { + shouldStartPlaying := len(q.songQueue) == 0 + q.songQueue = append(q.songQueue, songs...) + if shouldStartPlaying { + q.currentSong = 0 + return true + } + return false +} + +// returns a song if position has changed +func (q *queue) SetPosition(position int) *subsonic.Child { + if position == q.currentSong || position < 0 || len(q.songQueue) < position { + return nil + } + q.currentSong = position + return q.GetCurrentSong() +} + +func (q *queue) GetPosition() int { + return q.currentSong +} + +func (q *queue) GetCurrentSong() *subsonic.Child { + if len(q.songQueue) == 0 { + return nil + } + return q.songQueue[q.currentSong] +} + +func (q *queue) HasPrev() bool { + return 0 < q.currentSong +} + +func (q *queue) HasNext() bool { + return q.currentSong < len(q.songQueue)-1 +} + +func (q *queue) Next() *subsonic.Child { + if len(q.songQueue) > q.currentSong+1 { + q.currentSong = q.currentSong + 1 + return q.GetCurrentSong() + } + return nil +} + +func (q *queue) Prev() *subsonic.Child { + if q.currentSong > 0 { + q.currentSong = q.currentSong - 1 + return q.GetCurrentSong() + } + return nil +} + +func (q *queue) Get() []*subsonic.Child { + return q.songQueue +} diff --git a/internal/tui/tui.go b/internal/tui/tui.go new file mode 100644 index 0000000..9927162 --- /dev/null +++ b/internal/tui/tui.go @@ -0,0 +1,124 @@ +package tui + +import ( + "fmt" + "os" + + "git.dayanhub.com/sagi/subsonic-tui/internal/client" + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "git.dayanhub.com/sagi/subsonic-tui/internal/playback" + "git.dayanhub.com/sagi/subsonic-tui/internal/tui/views" + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +type TUI struct { + app *tview.Application + layout views.View + + client *client.Client + playbackCtl *playback.Controller +} + +func NewLogin() *TUI { + app := tview.NewApplication() + layout := views.NewLoginView(func(u, p, url string) { + c := client.NewClient(url) + err := c.Authenticate(u, p) + if err != nil { + app.Stop() + fmt.Printf("[Error] Failed to login. Aborting %e", err) + os.Exit(1) + } + config.SetURL(url) + config.SetUsername(u) + config.SetPassword(p) + config.SaveConfig() + app.Stop() + }, func() { + app.Stop() + os.Exit(0) + }) + app.EnableMouse(true). + SetRoot(layout.GetView(), true) + + app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Key() == tcell.KeyEsc { + app.Stop() + os.Exit(0) + } + return event + }) + + return &TUI{ + app: app, + layout: layout, + } +} + +func NewPlayer(client *client.Client, playbackCtl *playback.Controller) *TUI { + app := tview.NewApplication() + + layout := views.NewLayout(client, playbackCtl, func() { + go app.Draw() + }) + help := views.NewHelp() + + pages := tview.NewPages() + pages.AddPage("app", layout.GetView(), true, true) + pages.AddPage("help", help.GetView(), true, false) + + help.SetKeyPressedFunc(func() { + pages.SwitchToPage("app") + }) + app.EnableMouse(true). + SetRoot(pages, true) + + app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if currentPage, _ := pages.GetFrontPage(); currentPage == "help" { + pages.SwitchToPage("app") + help.GetView().Blur() + layout.GetView().Focus(nil) + return nil + } + if layout.Mode() == views.StatusModeSearch { + return event + } + if event.Rune() == 'q' { + app.Stop() + fmt.Println("Exiting..") + return nil + } else if event.Rune() == 'h' { + return tcell.NewEventKey(tcell.KeyLeft, rune(tcell.KeyLeft), event.Modifiers()) + } else if event.Rune() == 'j' { + return tcell.NewEventKey(tcell.KeyDown, rune(tcell.KeyDown), event.Modifiers()) + } else if event.Rune() == 'k' { + return tcell.NewEventKey(tcell.KeyUp, rune(tcell.KeyUp), event.Modifiers()) + } else if event.Rune() == 'l' { + return tcell.NewEventKey(tcell.KeyRight, rune(tcell.KeyRight), event.Modifiers()) + } else if event.Rune() == '?' { + pages.SwitchToPage("help") + layout.GetView().Blur() + go app.Draw() + } + return event + }) + app.SetAfterDrawFunc(func(screen tcell.Screen) { + layout.Update() + }) + app.GetFocus().Blur() + + //app.SetFocus(layout.GetView()) + + return &TUI{ + app: app, + layout: layout, + client: client, + playbackCtl: playbackCtl, + } + +} + +func (t *TUI) Run() error { + return t.app.Run() +} diff --git a/internal/tui/views/albums.go b/internal/tui/views/albums.go new file mode 100644 index 0000000..3cb9a50 --- /dev/null +++ b/internal/tui/views/albums.go @@ -0,0 +1,73 @@ +package views + +import ( + "git.dayanhub.com/sagi/subsonic-tui/internal/client" + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "github.com/delucks/go-subsonic" + "github.com/rivo/tview" +) + +var _ View = &albums{} + +type albums struct { + view *tview.Table + client *client.Client + albums []*subsonic.AlbumID3 + callback func(albumID string) +} + +func NewAlbums(client *client.Client) *albums { + + list := tview.NewTable() + + list.SetBackgroundColor(config.ColorBackground) + list.SetTitle("Albums [2]") + list.SetBorder(true) + + resp, _ := client.GetAlbums() + + obj := &albums{ + view: list, + client: client, + albums: resp, + } + + list.SetSelectedFunc(func(row, column int) { + obj.callback(obj.albums[row].ID) + }) + + list.SetFocusFunc(func() { + list.SetBorderColor(config.ColorSelectedBoarder) + list.SetSelectable(true, false) + }) + list.SetBlurFunc(func() { + list.SetBorderColor(config.ColorBluredBoarder) + list.SetSelectable(false, false) + }) + + obj.Update() + return obj +} + +func (a *albums) SetAlbums(al []*subsonic.AlbumID3) { + a.albums = al + a.Update() +} + +func (a *albums) Update() { + a.view.Clear() + for i, pl := range a.albums { + title := tview.NewTableCell(pl.Name).SetExpansion(1).SetMaxWidth(15) + artist := tview.NewTableCell(pl.Artist).SetExpansion(1).SetAlign(tview.AlignRight) + a.view.SetCell(i, 0, title) + a.view.SetCell(i, 1, artist) + } +} + +func (a *albums) SetCallback(f func(albumID string)) { + a.callback = f +} + +func (a *albums) GetView() tview.Primitive { + return a.view +} diff --git a/internal/tui/views/artists.go b/internal/tui/views/artists.go new file mode 100644 index 0000000..94f3d5c --- /dev/null +++ b/internal/tui/views/artists.go @@ -0,0 +1,70 @@ +package views + +import ( + "git.dayanhub.com/sagi/subsonic-tui/internal/client" + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "github.com/delucks/go-subsonic" + "github.com/rivo/tview" +) + +var _ View = &artists{} + +type artists struct { + view *tview.Table + client *client.Client + artists []*subsonic.ArtistID3 + selectArtistFunc func(artistId string) + openArtistFunc func(artistId string) +} + +func NewArtists(client *client.Client) *artists { + + list := tview.NewTable() + + list.SetBackgroundColor(config.ColorBackground) + list.SetTitle("Artists [1]") + list.SetBorder(true) + list.SetFocusFunc(func() { + list.SetBorderColor(config.ColorSelectedBoarder) + list.SetSelectable(true, false) + }) + list.SetBlurFunc(func() { + list.SetBorderColor(config.ColorBluredBoarder) + list.SetSelectable(false, false) + }) + + arts, _ := client.GetArtists() + + for i, artist := range arts { + cell := tview.NewTableCell(artist.Name).SetExpansion(1) + list.SetCell(i, 0, cell) + // list.AddItem(artist.Name, fmt.Sprintf("%s", artist.Name), '0', nil) + } + + resp := &artists{ + view: list, + client: client, + artists: arts, + } + + list.SetSelectedFunc(func(row, column int) { + resp.openArtistFunc(resp.artists[row].ID) + }) + + return resp +} + +func (a *artists) SetSelectArtistFunc(f func(artistId string)) { + a.selectArtistFunc = f +} +func (a *artists) SetOpenArtistFunc(f func(artistId string)) { + a.openArtistFunc = f +} + +func (a *artists) Update() { + +} + +func (a *artists) GetView() tview.Primitive { + return a.view +} diff --git a/internal/tui/views/button.go b/internal/tui/views/button.go new file mode 100644 index 0000000..847438a --- /dev/null +++ b/internal/tui/views/button.go @@ -0,0 +1,21 @@ +package views + +import ( + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +func NewButton(label string) *tview.Button { + style := tcell.Style{} + inactiveS := style.Background(config.ColorButtonBg). + Foreground(config.ColorButtonTxt).Bold(true) + activeS := style.Background(config.ColorButtonSelectedBg). + Foreground(config.ColorButtonSelectedTxt).Bold(true).Underline(true) + + b := tview.NewButton(label) + b.SetStyle(inactiveS) + b.SetActivatedStyle(activeS) + + return b +} diff --git a/internal/tui/views/help.go b/internal/tui/views/help.go new file mode 100644 index 0000000..0343076 --- /dev/null +++ b/internal/tui/views/help.go @@ -0,0 +1,113 @@ +package views + +import ( + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "github.com/rivo/tview" +) + +var _ View = &help{} + +type help struct { + view *tview.Flex + keyPressedFunc func() +} + +type keyMap struct { + key string + description string +} + +var keyMaps = []keyMap{ + { + "Arrows", "Navigation", + }, + { + "h, j, k, l", "Navigation. Use [shift] for moving panes", + }, + { + "g", "Jump to top", + }, + { + "G", "Jump to bottom", + }, + { + "p", "Toggle Play/Pause", + }, + { + "s", "Stop", + }, + { + "n", "Play next song", + }, + { + "N", "Play previous song", + }, + { + "c", "Stop and clear queue", + }, + { + "r", "Start song radio", + }, + { + "/", "Search", + }, + { + "q", "Quit", + }, +} + +func NewHelp() *help { + h := &help{} + view := tview.NewTable() + view.SetBackgroundColor(config.ColorBackground) + height := 16 + width := 100 + view.SetBorder(true) + view.SetTitle(" Keybindings ") + view.SetTitleAlign(tview.AlignCenter) + + for i, km := range keyMaps { + odd := i%2 != 0 + txtColor := config.ColorText + if odd { + txtColor = config.ColorTextAccent + } + keyCell := tview.NewTableCell(km.key). + SetExpansion(1). + SetAlign(tview.AlignCenter). + SetTextColor(txtColor) + descCell := tview.NewTableCell(km.description). + SetExpansion(1). + SetAlign(tview.AlignCenter). + SetTextColor(txtColor) + view.SetCell(i, 0, keyCell) + view.SetCell(i, 1, descCell) + } + + innerFlex := tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(EmptyBox, 0, 1, false). + AddItem(view, height, 1, true). + AddItem(EmptyBox, 0, 1, false) + innerFlex.SetBackgroundColor(config.ColorBackground) + wrapper := tview.NewFlex(). + AddItem(EmptyBox, 0, 1, false). + AddItem(innerFlex, width, 1, true). + AddItem(EmptyBox, 0, 1, false) + wrapper.SetBackgroundColor((config.ColorBackground)) + + h.view = wrapper + + return h +} + +func (h *help) GetView() tview.Primitive { + return h.view +} + +func (h *help) Update() { + +} + +func (h *help) SetKeyPressedFunc(f func()) { + h.keyPressedFunc = f +} diff --git a/internal/tui/views/layout.go b/internal/tui/views/layout.go new file mode 100644 index 0000000..0b2bc6d --- /dev/null +++ b/internal/tui/views/layout.go @@ -0,0 +1,398 @@ +package views + +import ( + "time" + + "git.dayanhub.com/sagi/subsonic-tui/internal/client" + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "git.dayanhub.com/sagi/subsonic-tui/internal/playback" + "github.com/delucks/go-subsonic" + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +var _ View = &layout{} + +type layout struct { + view *tview.Pages + player View + currentFocusedView tview.Primitive + smallView *tview.Flex + largeView *tview.Grid + status *statusLine + mainView *main +} + +func NewLayout(client *client.Client, playbackCtl *playback.Controller, refreshUI func()) *layout { + layout := &layout{} + + largeView := tview.NewGrid().SetRows(0, 4, 1) + largeView.SetBackgroundColor(config.ColorBackground) + smallView := tview.NewFlex().SetDirection(tview.FlexRow) + smallView.SetBackgroundColor(config.ColorBackground) + layout.largeView = largeView + layout.smallView = smallView + pages := tview.NewPages() + pages.SetBackgroundColor(config.ColorBackground) + + // Status / command / search + statusLine := NewStatusLine() + layout.status = statusLine + + // Side Panel (Artist/Albums/Playlist) + albums := NewAlbums(client) + artists := NewArtists(client) + playlists := NewPlaylists(client) + + sidePanel := tview.NewGrid().SetRows(0, 0, 0) + sidePanel.AddItem(artists.GetView(), 0, 0, 1, 1, 0, 0, false) + sidePanel.AddItem(albums.GetView(), 1, 0, 1, 1, 0, 0, false) + sidePanel.AddItem(playlists.GetView(), 2, 0, 1, 1, 0, 0, false) + + // main pane + main := NewMainView(client, statusLine.Log) + layout.mainView = main + // Queue + queue := NewQueue() + + // Main view + mainView := tview.NewFlex() + mainView.SetBackgroundColor(config.ColorBackground) + mainView.AddItem(sidePanel, 0, 1, false) + mainView.AddItem(main.GetView(), 0, 2, false) + mainView.AddItem(queue.GetView(), 0, 1, false) + largeView.AddItem(mainView, 0, 0, 1, 3, 0, 0, false) + // Player + player := NewPlayer(client) + largeView.AddItem(player.GetView(), 1, 0, 1, 3, 0, 0, false) + + // Add status line + largeView.AddItem(statusLine.GetView(), 2, 0, 1, 3, 0, 0, false) + + // Callbacks + artists.SetSelectArtistFunc(func(artistId string) { + artists.view.Blur() + statusLine.Log("Fetching artist's albums...") + go func() { + a, _ := client.GetArtist(artistId) + albums.SetAlbums(a.Album) + albums.GetView().Focus(nil) + refreshUI() + }() + }) + artists.SetOpenArtistFunc(func(artistId string) { + artists.view.Blur() + statusLine.Log("Fetching artists...") + layout.currentFocusedView = main.GetView() + layout.rebuildSmallView() + go func() { + a, _ := client.GetArtist(artistId) + main.SetArtist(a) + main.GetView().Focus(nil) + refreshUI() + }() + }) + + albums.SetCallback(func(albumID string) { + albums.view.Blur() + statusLine.Log("Fetching album...") + layout.currentFocusedView = main.GetView() + layout.rebuildSmallView() + go func() { + a, _ := client.GetAlbum(albumID) + main.SetAlbum(a) + main.view.Focus(nil) + refreshUI() + }() + }) + + playlists.SetCallback(func(p *subsonic.Playlist) { + playlists.view.Blur() + statusLine.Log("Fetching playlist...") + layout.currentFocusedView = main.GetView() + layout.rebuildSmallView() + go func() { + playlist, _ := client.GetPlaylist(p.ID) + main.SetPlaylist(playlist) + main.view.Focus(nil) + refreshUI() + }() + }) + + main.SetPlayAllFunc(func(songs ...*subsonic.Child) { + statusLine.Log("Loaded #%d songs.", len(songs)) + playbackCtl.SetQueue(songs) + queue.Update(songs, playbackCtl.GetQueuePosition()) + }) + main.SetPlayAddSongFunc(func(song ...*subsonic.Child) { + statusLine.Log("Added #%d songs to queue.", len(song)) + playbackCtl.AddToQueue(song) + queue.Update(playbackCtl.GetQueue(), playbackCtl.GetQueuePosition()) + }) + + playbackCtl.SetSongElapsedFunc(func(song *subsonic.Child, elapsed time.Duration) { + player.SetSongInfo(song) + player.UpdateProgress(elapsed) + queue.Update(playbackCtl.GetQueue(), playbackCtl.GetQueuePosition()) + refreshUI() + }) + + playbackCtl.SetSongEndedFunc(func(song *subsonic.Child) { + statusLine.Log("") + queue.Update(playbackCtl.GetQueue(), playbackCtl.GetQueuePosition()) + if config.ScrobbleEnabled() { + // Scrobble + _ = client.Scrobble(song.ID) + } + }) + + queue.SetPlayFunc(func(position int) { + playbackCtl.SetQueuePosition(position) + queue.Update(playbackCtl.GetQueue(), playbackCtl.GetQueuePosition()) + }) + + statusLine.SetOnUpdateFunc(func() { + refreshUI() + }) + + statusLine.SetSearchFunc(func(quary string) { + if len(quary) == 0 { + layout.currentFocusedView.Focus(nil) + statusLine.Log("Search canceled") + return + } + // Search... + statusLine.Log("Searching for '%s'....", quary) + statusLine.view.Blur() + go func() { + result, _ := client.Search(quary) + layout.currentFocusedView.Blur() + main.SetSearch(result, quary) + layout.currentFocusedView = main.GetView() + layout.rebuildSmallView() + refreshUI() + }() + }) + + // Key Bindings + pages.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if statusLine.Mode() == StatusModeSearch { + return event + } + if event.Rune() == '1' { + // Focus Artists + artists.view.Blur() + albums.view.Blur() + playlists.view.Blur() + main.view.Blur() + queue.view.Blur() + artists.view.Focus(nil) + layout.currentFocusedView = artists.GetView() + layout.rebuildSmallView() + return nil + } else if event.Rune() == '2' { + // Focus Albums + artists.view.Blur() + albums.view.Blur() + playlists.view.Blur() + main.view.Blur() + queue.view.Blur() + albums.view.Focus(nil) + layout.currentFocusedView = albums.GetView() + layout.rebuildSmallView() + return nil + } else if event.Rune() == '3' { + // Focus Playlists + artists.view.Blur() + albums.view.Blur() + playlists.view.Blur() + main.view.Blur() + queue.view.Blur() + playlists.view.Focus(nil) + layout.currentFocusedView = playlists.GetView() + layout.rebuildSmallView() + return nil + } else if event.Rune() == '`' { + // Focus Songs + artists.view.Blur() + albums.view.Blur() + playlists.view.Blur() + main.view.Blur() + queue.view.Blur() + main.view.Focus(nil) + layout.currentFocusedView = main.GetView() + layout.rebuildSmallView() + return nil + } else if event.Rune() == '4' { + // Focus Queue + artists.view.Blur() + albums.view.Blur() + playlists.view.Blur() + main.view.Blur() + queue.view.Blur() + queue.view.Focus(nil) + layout.currentFocusedView = queue.GetView() + layout.rebuildSmallView() + return nil + } else if event.Rune() == 'n' { + playbackCtl.Next() + queue.Update(playbackCtl.GetQueue(), playbackCtl.GetQueuePosition()) + return nil + + } else if event.Rune() == 'N' { + playbackCtl.Prev() + queue.Update(playbackCtl.GetQueue(), playbackCtl.GetQueuePosition()) + return nil + } else if event.Rune() == 's' { + playbackCtl.Stop() + queue.Update(playbackCtl.GetQueue(), playbackCtl.GetQueuePosition()) + return nil + } else if event.Rune() == 'p' { + if playbackCtl.State() == playback.PlaybackStateStopped { + song := playbackCtl.GetCurrentSong() + if song != nil { + playbackCtl.Play(song) + } + queue.Update(playbackCtl.GetQueue(), playbackCtl.GetQueuePosition()) + return nil + } + playbackCtl.TogglePlayPause() + return nil + } else if event.Rune() == 'c' { + playbackCtl.ClearQueue() + queue.Update(playbackCtl.GetQueue(), playbackCtl.GetQueuePosition()) + return nil + } else if event.Rune() == '/' { + layout.currentFocusedView.Blur() + statusLine.Search() + refreshUI() + return nil + } else if event.Rune() == 'L' { + if layout.currentFocusedView == albums.GetView() { + layout.currentFocusedView.Blur() + main.view.Focus(nil) + layout.currentFocusedView = main.GetView() + layout.rebuildSmallView() + } else if layout.currentFocusedView == artists.GetView() { + layout.currentFocusedView.Blur() + main.view.Focus(nil) + layout.currentFocusedView = main.GetView() + layout.rebuildSmallView() + } else if layout.currentFocusedView == playlists.GetView() { + layout.currentFocusedView.Blur() + main.view.Focus(nil) + layout.currentFocusedView = main.GetView() + layout.rebuildSmallView() + } else if layout.currentFocusedView == main.GetView() { + layout.currentFocusedView.Blur() + queue.view.Focus(nil) + layout.currentFocusedView = queue.GetView() + layout.rebuildSmallView() + } + } else if event.Rune() == 'H' { + if layout.currentFocusedView == queue.GetView() { + layout.currentFocusedView.Blur() + main.view.Focus(nil) + layout.currentFocusedView = main.GetView() + layout.rebuildSmallView() + } else if layout.currentFocusedView == main.GetView() { + layout.currentFocusedView.Blur() + artists.view.Focus(nil) + layout.currentFocusedView = artists.GetView() + layout.rebuildSmallView() + } + } else if event.Rune() == 'J' { + if layout.currentFocusedView == albums.GetView() { + layout.currentFocusedView.Blur() + playlists.view.Focus(nil) + layout.currentFocusedView = playlists.GetView() + layout.rebuildSmallView() + } else if layout.currentFocusedView == artists.GetView() { + layout.currentFocusedView.Blur() + albums.view.Focus(nil) + layout.currentFocusedView = albums.GetView() + layout.rebuildSmallView() + } else if layout.currentFocusedView == playlists.GetView() { + layout.currentFocusedView.Blur() + artists.view.Focus(nil) + layout.currentFocusedView = artists.GetView() + layout.rebuildSmallView() + } + } else if event.Rune() == 'K' { + if layout.currentFocusedView == albums.GetView() { + layout.currentFocusedView.Blur() + artists.view.Focus(nil) + layout.currentFocusedView = artists.GetView() + layout.rebuildSmallView() + } else if layout.currentFocusedView == artists.GetView() { + layout.currentFocusedView.Blur() + playlists.view.Focus(nil) + layout.currentFocusedView = playlists.GetView() + layout.rebuildSmallView() + } else if layout.currentFocusedView == playlists.GetView() { + layout.currentFocusedView.Blur() + albums.view.Focus(nil) + layout.currentFocusedView = albums.GetView() + layout.rebuildSmallView() + } + } + + return event + }) + + largeView.SetFocusFunc(func() { + largeView.Blur() + layout.currentFocusedView.Focus(nil) + }) + smallView.SetFocusFunc(func() { + smallView.Blur() + layout.currentFocusedView.Focus(nil) + }) + + statusLine.Log("Press '?' for help") + + pages.AddPage("small", smallView, true, false) + pages.AddPage("large", largeView, true, true) + + layout.view = pages + layout.player = player + //Auto focus on artists + artists.GetView().Focus(nil) + layout.currentFocusedView = artists.GetView() + layout.rebuildSmallView() + return layout +} +func (l *layout) rebuildSmallView() { + l.smallView.Clear() + l.smallView.AddItem(l.currentFocusedView, 0, 1, false) + l.smallView.AddItem(l.player.GetView(), 4, 0, false) + l.smallView.AddItem(l.status.GetView(), 1, 0, false) +} + +func (l *layout) Mode() Statusmode { + return l.status.Mode() +} + +func (l *layout) GetView() tview.Primitive { + return l.view +} + +func (l *layout) Update() { + _, _, w, h := l.view.GetRect() + page, _ := l.view.GetFrontPage() + smallView := w < 100 || h < 30 + if smallView { + if page != "small" { + l.mainView.SetMiniView(true) + go l.view.SwitchToPage("small") + return + } + } else { + if page != "large" { + l.mainView.SetMiniView(false) + go l.view.SwitchToPage("large") + return + } + } + l.player.Update() +} diff --git a/internal/tui/views/login.go b/internal/tui/views/login.go new file mode 100644 index 0000000..85d70a2 --- /dev/null +++ b/internal/tui/views/login.go @@ -0,0 +1,73 @@ +package views + +import ( + "github.com/rivo/tview" +) + +var _ View = &login{} + +const ( + ViewEventLoginClicked = "login-clicked" +) + +type login struct { + view *tview.Flex + form *tview.Form + username string + password string + url string + loginFunc func(u, p, url string) + exitFunc func() +} + +func (l *login) GetView() tview.Primitive { + return l.view +} + +func (l *login) onFormLogin() { + l.loginFunc(l.username, l.password, l.url) +} + +func NewLoginView(loginFunc func(u, p, url string), exitFunc func()) View { + l := &login{} + l.loginFunc = loginFunc + l.exitFunc = exitFunc + + form := tview.NewForm(). + AddInputField("Server URL", "https://", 20, nil, func(text string) { + l.url = text + }). + AddInputField("Username", "", 20, nil, func(text string) { + l.username = text + }). + AddPasswordField("Password", "", 20, '*', func(text string) { + l.password = text + }). + AddButton("Login", l.onFormLogin). + AddButton("Quit", func() { + l.exitFunc() + }) + form.SetBorder(true).SetTitle(" Login ").SetTitleAlign(tview.AlignCenter) + + width := 50 + height := 20 + + wrapper := tview.NewFlex(). + AddItem(nil, 0, 1, false). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(nil, 0, 1, false). + AddItem(form, height, 1, true). + AddItem(nil, 0, 1, false), width, 1, true). + AddItem(nil, 0, 1, false) + + l.view = wrapper + l.form = form + + l.GetView().Focus(func(p tview.Primitive) {}) + form.SetFocus(0) + return l +} + +func (l *login) Update() { + +} diff --git a/internal/tui/views/main.go b/internal/tui/views/main.go new file mode 100644 index 0000000..a6bb8a2 --- /dev/null +++ b/internal/tui/views/main.go @@ -0,0 +1,616 @@ +package views + +import ( + "fmt" + "strconv" + "time" + + "git.dayanhub.com/sagi/subsonic-tui/internal/client" + "git.dayanhub.com/sagi/subsonic-tui/internal/common" + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "github.com/delucks/go-subsonic" + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +var _ View = &main{} + +type mainviewmode int + +const ( + mainModeAlbum mainviewmode = iota + mainModePlaylist mainviewmode = iota + mainModeArtist mainviewmode = iota + mainModeSearch mainviewmode = iota +) + +type main struct { + view *tview.Flex + client *client.Client + mode mainviewmode + query string + miniView bool + album *subsonic.AlbumID3 + searchResult *subsonic.SearchResult3 + playlist *subsonic.Playlist + songList *tview.Table + artist *subsonic.ArtistID3 + artistInfo *subsonic.ArtistInfo2 + playAllFunc func(song ...*subsonic.Child) + addSongsFunc func(song ...*subsonic.Child) + log func(format string, a ...any) +} + +func NewMainView(client *client.Client, log func(format string, a ...any)) *main { + playlistAlbum := &main{ + client: client, + log: log, + } + + flex := tview.NewFlex().SetDirection(tview.FlexRow) + + flex.SetBackgroundColor(config.ColorBackground) + flex.SetTitle("Subsonic TUI [`]") + flex.SetBorder(true) + flex.SetFocusFunc(func() { + flex.SetBorderColor(config.ColorSelectedBoarder) + if playlistAlbum.songList != nil { + //playlistAlbum.view.Blur() + playlistAlbum.songList.Focus(nil) + } + }) + flex.SetBlurFunc(func() { + flex.SetBorderColor(config.ColorBluredBoarder) + if playlistAlbum.songList != nil { + playlistAlbum.songList.Blur() + } + }) + + playlistAlbum.view = flex + // Empty Box for starters... + playlistAlbum.view.AddItem(EmptyBox, 0, 1, false) + return playlistAlbum +} + +func (m *main) SetMiniView(mini bool) { + m.miniView = mini +} + +func (m *main) SetAlbum(album *subsonic.AlbumID3) { + m.mode = mainModeAlbum + m.album = album + m.Update() + m.log("") +} + +func (m *main) SetPlaylist(playlist *subsonic.Playlist) { + m.mode = mainModePlaylist + m.playlist = playlist + m.Update() + m.log("") +} + +func (m *main) SetArtist(artist *subsonic.ArtistID3) { + m.mode = mainModeArtist + m.artist = artist + info, _ := m.client.GetArtistInfo(artist.ID) + m.artistInfo = info + m.Update() + m.log("") +} + +func (m *main) SetSearch(result *subsonic.SearchResult3, query string) { + m.mode = mainModeSearch + m.searchResult = result + m.query = query + m.Update() + m.log("Found #%d artists, #%d albums and #%d songs", + len(result.Artist), + len(result.Album), + len(result.Song), + ) +} + +func (m *main) drawPlaylist() { + subtitle := fmt.Sprintf("%s\n\nCreated by: %s | %s", m.playlist.Comment, m.playlist.Owner, time.Duration(m.playlist.Duration*int(time.Second)).String()) + m.populateHeader(m.playlist.Name, subtitle, m.playlist.Duration, m.playlist.CoverArt) + playBtn := m.drawPlaylistAlbumButtons(m.playlist.Entry) + m.populateSongs(m.playlist.Entry, playBtn) +} + +func (m *main) drawAlbum() { + subtitle := fmt.Sprintf("%s\n\n%d | %s", m.album.Artist, m.album.Year, time.Duration(m.album.Duration*int(time.Second)).String()) + m.populateHeader(m.album.Name, subtitle, m.album.Duration, m.album.CoverArt) + playBtn := m.drawPlaylistAlbumButtons(m.album.Song) + m.populateSongs(m.album.Song, playBtn) + +} + +func (m *main) drawArtist() { + m.populateHeader(m.artist.Name, m.artistInfo.Biography, 0, m.artist.CoverArt) + btn := m.drawArtistButtons() + m.populateAlbums(btn) +} + +func (m *main) drawSearch() { + sub := fmt.Sprintf("Query: %s", m.query) + m.populateHeader("Search Results", sub, 0, "") + m.populateSearchResults() +} + +func (m *main) Update() { + m.view.Clear() + switch m.mode { + case mainModeAlbum: + m.drawAlbum() + case mainModePlaylist: + m.drawPlaylist() + case mainModeArtist: + m.drawArtist() + case mainModeSearch: + m.drawSearch() + } + m.songList.Focus(nil) +} + +func (m *main) drawArtistButtons() *tview.Button { + // Add buttons: Radio + songs, _ := m.client.GetTopSongs(m.artist.Name, 10) + f := tview.NewFlex() + f.SetBackgroundColor(config.ColorBackground) + f.SetBorderPadding(0, 0, 2, 2) + // Buttons + radio := NewButton("Radio") + top10 := NewButton("Top 10") + // Button callbacks + top10.SetSelectedFunc(func() { + top10.Blur() + m.log("Playing %s's top 10 songs", m.artist.Name) + m.playAllFunc(songs...) + m.songList.Focus(nil) + }) + radio.SetSelectedFunc(func() { + radio.Blur() + m.log("Generating %s's radio", m.artist.Name) + go func() { + radioSongs := []*subsonic.Child{} + if config.ExperimentalRadioAlgo() { + radioSongs, _ = m.client.GetExperimentalArtistRadio(m.artist, m.artistInfo, config.MaxRadioSongs()) + } + if len(radioSongs) == 0 { + radioSongs, _ = m.client.GetSimilarSongs(m.artist.ID, config.MaxRadioSongs()) + } + + common.ShuffleSlice(radioSongs) + m.playAllFunc(radioSongs...) + }() + m.songList.Focus(nil) + }) + radio.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + switch event.Key() { + case tcell.KeyDown: + radio.Blur() + m.songList.Focus(nil) + return nil + case tcell.KeyRight: + radio.Blur() + top10.Focus(nil) + return nil + } + return event + }) + top10.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + switch event.Key() { + case tcell.KeyDown: + top10.Blur() + m.songList.Focus(nil) + return nil + case tcell.KeyLeft: + top10.Blur() + radio.Focus(nil) + return nil + } + return event + }) + + f.AddItem(radio, 0, 1, false) + f.AddItem(EmptyBox, 0, 1, false) + if len(songs) > 0 { + f.AddItem(top10, 0, 1, false) + } + f.AddItem(EmptyBox, 0, 1, false) + + // Add the buttons to the view + m.view.AddItem(f, 1, 0, false) + // Margin bottom of 1 line + m.view.AddItem(EmptyBox, 1, 0, false) + + return radio +} + +func (m *main) populateAlbums(btn *tview.Button) { + table := tview.NewTable() + table.SetBackgroundColor(config.ColorBackground) + table.SetWrapSelection(true, false) + for i, album := range m.artist.Album { + year := tview.NewTableCell(fmt.Sprintf("%d", album.Year)).SetTextColor(config.ColorTextAccent) + name := tview.NewTableCell(album.Name).SetExpansion(2).SetAlign(tview.AlignCenter) + d := time.Second * time.Duration(album.Duration) + duration := tview.NewTableCell(d.String()).SetAlign(tview.AlignRight).SetExpansion(1) + + table.SetCell(i, 0, year) + table.SetCell(i, 1, name) + table.SetCell(i, 2, duration) + } + + table.SetFocusFunc(func() { + m.view.SetBorderColor(config.ColorSelectedBoarder) + table.SetSelectable(true, false) + }) + table.SetBlurFunc(func() { + m.view.SetBorderColor(config.ColorBluredBoarder) + table.SetSelectable(false, false) + }) + table.SetSelectedFunc(func(row, column int) { + alb, _ := m.client.GetAlbum(m.artist.Album[row].ID) + m.SetAlbum(alb) + m.view.Focus(nil) + }) + table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + row, _ := m.songList.GetSelection() + if row == 0 && event.Key() == tcell.KeyUp { + table.Blur() + m.view.SetBorderColor(config.ColorSelectedBoarder) + btn.Focus(nil) + return nil + } + return event + }) + m.songList = table + + m.view.AddItem(table, 0, 1, false) + +} + +func (m *main) drawPlaylistAlbumButtons(songs []*subsonic.Child) *tview.Button { + // Add buttons: Play | Shuffle | Add to queue + f := tview.NewFlex() + f.SetBackgroundColor(config.ColorBackground) + f.SetBorderPadding(0, 0, 2, 2) + // Buttons + play := NewButton("Play") + shuffle := NewButton("Shuffle") + queue := NewButton("Queue") + artist := NewButton("Artist") + // Button callbacks + play.SetSelectedFunc(func() { + play.Blur() + m.playAllFunc(songs...) + m.songList.Focus(nil) + }) + play.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + switch event.Key() { + case tcell.KeyLeft: + play.Blur() + artist.Focus(nil) + return nil + case tcell.KeyRight: + play.Blur() + shuffle.Focus(nil) + return nil + case tcell.KeyDown: + play.Blur() + m.songList.Focus(nil) + return nil + } + return event + }) + shuffle.SetSelectedFunc(func() { + shuffle.Blur() + cpy := make([]*subsonic.Child, len(songs)) + copy(cpy, songs) + common.ShuffleSlice(cpy) + m.playAllFunc(cpy...) + m.songList.Focus(nil) + }) + shuffle.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + switch event.Key() { + case tcell.KeyLeft: + shuffle.Blur() + play.Focus(nil) + return nil + case tcell.KeyRight: + shuffle.Blur() + queue.Focus(nil) + return nil + case tcell.KeyDown: + shuffle.Blur() + m.songList.Focus(nil) + return nil + } + return event + }) + queue.SetSelectedFunc(func() { + queue.Blur() + m.addSongsFunc(songs...) + m.songList.Focus(nil) + }) + queue.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + switch event.Key() { + case tcell.KeyLeft: + queue.Blur() + shuffle.Focus(nil) + return nil + case tcell.KeyRight: + queue.Blur() + artist.Focus(nil) + return nil + case tcell.KeyDown: + queue.Blur() + m.songList.Focus(nil) + return nil + } + return event + }) + artist.SetSelectedFunc(func() { + artist.Blur() + ar, _ := m.client.GetArtist(m.album.ArtistID) + m.SetArtist(ar) + m.songList.Focus(nil) + }) + artist.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + switch event.Key() { + case tcell.KeyLeft: + artist.Blur() + queue.Focus(nil) + return nil + case tcell.KeyRight: + artist.Blur() + play.Focus(nil) + return nil + case tcell.KeyDown: + artist.Blur() + m.songList.Focus(nil) + return nil + } + return event + }) + + f.AddItem(play, 0, 1, true) + f.AddItem(EmptyBox, 0, 1, false) + f.AddItem(shuffle, 0, 1, false) + f.AddItem(EmptyBox, 0, 1, false) + f.AddItem(queue, 0, 1, false) + f.AddItem(EmptyBox, 0, 1, false) + f.AddItem(artist, 0, 1, false) + + // Add the buttons to the view + m.view.AddItem(f, 1, 0, false) + // Margin bottom of 1 line + m.view.AddItem(EmptyBox, 1, 0, false) + + return play +} + +func (m *main) populateSongs(songs []*subsonic.Child, play *tview.Button) { + table := tview.NewTable() + table.SetBackgroundColor(config.ColorBackground) + table.SetWrapSelection(true, false) + for i, song := range songs { + num := tview.NewTableCell(fmt.Sprintf("%d", i+1)).SetTextColor(config.ColorTextAccent) + title := tview.NewTableCell(song.Title).SetMaxWidth(15).SetExpansion(2) + artist := tview.NewTableCell(song.Artist).SetMaxWidth(15).SetExpansion(1) + d := time.Second * time.Duration(song.Duration) + duration := tview.NewTableCell(d.String()).SetAlign(tview.AlignRight).SetExpansion(1) + table.SetCell(i, 0, num) + table.SetCell(i, 1, title) + table.SetCell(i, 2, artist) + table.SetCell(i, 3, duration) + } + + m.songList = table + table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + // on first line and pressing up -> play button + row, _ := table.GetSelection() + if row == 0 && event.Key() == tcell.KeyUp { + table.Blur() + m.view.SetBorderColor(config.ColorSelectedBoarder) + play.Focus(nil) + return nil + } else if event.Rune() == 'r' { + song := songs[row] + m.log("Generating song (%s) radio....", song.Title) + go func() { + radioSongs, _ := m.client.GetSimilarSongs(song.ID, config.MaxRadioSongs()) + m.playAllFunc(radioSongs...) + }() + } + return event + }) + table.SetFocusFunc(func() { + m.view.SetBorderColor(config.ColorSelectedBoarder) + table.SetSelectable(true, false) + }) + table.SetBlurFunc(func() { + m.view.SetBorderColor(config.ColorBluredBoarder) + table.SetSelectable(false, false) + }) + m.songList.SetSelectedFunc(func(row, column int) { + m.addSongsFunc(songs[row]) + }) + + m.view.AddItem(table, 0, 1, false) +} + +func (m *main) populateSearchResults() { + table := tview.NewTable() + table.SetBackgroundColor(config.ColorBackground) + table.SetWrapSelection(true, false) + row := 0 + lastArtist := 0 + lastAlbum := 0 + + // Artists + if len(m.searchResult.Artist) > 0 { + // Header + header := tview.NewTableCell("Artists").SetSelectable(false) + table.SetCell(row, 0, header) + row++ + //List + for i, artist := range m.searchResult.Artist { + index := tview.NewTableCell(fmt.Sprintf("%d", i+1)). + SetTextColor(config.ColorTextAccent) + a := tview.NewTableCell(artist.Name).SetExpansion(2).SetMaxWidth(15) + acount := tview.NewTableCell(fmt.Sprintf("%d Albums", artist.AlbumCount)). + SetExpansion(1).SetAlign(tview.AlignRight).SetMaxWidth(15) + table.SetCell(row, 0, index) + table.SetCell(row, 1, a) + table.SetCell(row, 2, acount) + row++ + } + lastArtist = row + } + + // Albums + + if len(m.searchResult.Album) > 0 { + // Header + header := tview.NewTableCell("Albums").SetSelectable(false) + table.SetCell(row, 0, header) + row++ + //List + for i, album := range m.searchResult.Album { + index := tview.NewTableCell(fmt.Sprintf("%d", i+1)). + SetTextColor(config.ColorTextAccent) + title := tview.NewTableCell(album.Name).SetExpansion(2).SetMaxWidth(15) + artist := tview.NewTableCell(album.Artist). + SetExpansion(1).SetAlign(tview.AlignRight).SetMaxWidth(15) + table.SetCell(row, 0, index) + table.SetCell(row, 1, title) + table.SetCell(row, 2, artist) + row++ + } + lastAlbum = row + } + // Songs + if len(m.searchResult.Song) > 0 { + // Header + header := tview.NewTableCell("Songs").SetSelectable(false) + table.SetCell(row, 0, header) + row++ + //List + for i, song := range m.searchResult.Song { + index := tview.NewTableCell(fmt.Sprintf("%d", i+1)). + SetTextColor(config.ColorTextAccent) + title := tview.NewTableCell(song.Title).SetExpansion(2).SetMaxWidth(15) + artist := tview.NewTableCell(song.Artist). + SetExpansion(1).SetAlign(tview.AlignRight).SetMaxWidth(15) + table.SetCell(row, 0, index) + table.SetCell(row, 1, title) + table.SetCell(row, 2, artist) + row++ + } + } + + m.songList = table + table.SetFocusFunc(func() { + m.view.SetBorderColor(config.ColorSelectedBoarder) + table.SetSelectable(true, false) + }) + table.SetBlurFunc(func() { + m.view.SetBorderColor(config.ColorBluredBoarder) + table.SetSelectable(false, false) + }) + table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + row, _ := table.GetSelection() + if row <= lastAlbum { + return event + } + if event.Rune() != 'r' { + return event + } + cell := table.GetCell(row, 0) + index, err := strconv.Atoi(cell.Text) + if err != nil { + return nil + } + song := m.searchResult.Song[index-1] + m.log("Generating song (%s) radio....", song.Title) + go func() { + radioSongs, _ := m.client.GetSimilarSongs(song.ID, config.MaxRadioSongs()) + m.playAllFunc(radioSongs...) + }() + return nil + }) + m.songList.SetSelectedFunc(func(row, column int) { + cell := table.GetCell(row, 0) + index, err := strconv.Atoi(cell.Text) + if err != nil { + return + } + if row <= lastArtist { + artist, _ := m.client.GetArtist(m.searchResult.Artist[index-1].ID) + m.SetArtist(artist) + } else if lastArtist < row && row <= lastAlbum { + album, _ := m.client.GetAlbum(m.searchResult.Album[index-1].ID) + m.SetAlbum(album) + } else { + m.addSongsFunc(m.searchResult.Song[index-1]) + } + }) + + m.view.AddItem(table, 0, 1, false) +} + +func (m *main) populateHeader(title, subtitle string, + duration int, coverArtID string) { + + header := tview.NewFlex() + header.SetBackgroundColor(config.ColorBackground) + art := tview.NewImage() + art.SetBackgroundColor(config.ColorBackground) + + img, _ := m.client.GetCoverArt(coverArtID) + art.SetImage(img) + t := tview.NewTextView(). + SetTextColor(config.ColorTextAccent). + SetTextAlign(tview.AlignCenter). + SetText(title) + t.SetBackgroundColor(config.ColorBackground) + s := tview.NewTextView(). + SetTextColor(config.ColorText). + SetTextAlign(tview.AlignCenter). + SetText(subtitle).SetWordWrap(true) + s.SetBackgroundColor(config.ColorBackground) + + g := tview.NewFlex().SetDirection(tview.FlexRow) + g.SetBackgroundColor(config.ColorBackground) + g.AddItem(EmptyBox, 1, 1, false) + g.AddItem(t, 1, 1, false) + g.AddItem(s, 0, 1, false) + + header.AddItem(art, 0, 1, false) + header.AddItem(g, 0, 3, false) + size := 6 + if m.miniView { + size = 4 + } + m.view.AddItem(header, size, 1, false) + + // Margin bottom of 1 line + if !m.miniView { + m.view.AddItem(EmptyBox, 1, 0, false) + } +} + +func (m *main) SetPlayAllFunc(f func(song ...*subsonic.Child)) { + m.playAllFunc = f +} + +func (m *main) SetPlayAddSongFunc(f func(song ...*subsonic.Child)) { + m.addSongsFunc = f +} + +func (m *main) GetView() tview.Primitive { + return m.view +} diff --git a/internal/tui/views/player.go b/internal/tui/views/player.go new file mode 100644 index 0000000..45fb8d0 --- /dev/null +++ b/internal/tui/views/player.go @@ -0,0 +1,117 @@ +package views + +import ( + "fmt" + "time" + + "git.dayanhub.com/sagi/subsonic-tui/internal/client" + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "github.com/delucks/go-subsonic" + "github.com/rivo/tview" +) + +var _ View = &player{} + +type player struct { + client *client.Client + view tview.Primitive + grid *tview.Grid + artwork *tview.Image + progress *tview.Flex + songInfo *tview.TextView + song *subsonic.Child +} + +func NewPlayer(client *client.Client) *player { + grid := tview.NewGrid().SetColumns(9, 0) + grid.SetBackgroundColor(config.ColorBackground) + + //album art + art := tview.NewImage() + art.SetBackgroundColor(config.ColorBackground) + + grid.AddItem(art, 0, 0, 1, 1, 4, 4, false) + // Progress + progress := tview.NewFlex() + progress.SetBackgroundColor(config.ColorBackground) + + // Song songInfo + songInfo := tview.NewTextView() + songInfo.SetDynamicColors(true) + songInfo.SetBackgroundColor(config.ColorBackground) + + // Info + Progress + songProg := tview.NewFlex().SetDirection(tview.FlexRow) + songProg.AddItem(songInfo, 0, 4, false) + songProg.AddItem(progress, 0, 1, false) + grid.AddItem(songProg, 0, 1, 1, 1, 0, 0, false) + + player := &player{ + client: client, + view: grid, + artwork: art, + songInfo: songInfo, + grid: grid, + progress: progress, + } + + player.SetSongInfo(&subsonic.Child{ + Title: "Subsonic TUI", + Album: "MaVeZe", + Artist: "ZeGoomba", + CoverArt: "", + Duration: 0, + }) + player.UpdateProgress(time.Duration(0)) + + return player +} + +func (p *player) SetSongInfo(song *subsonic.Child) { + info := fmt.Sprintf("[yellow]Title:[white] %s\n[yellow]Album:[white] %s\n[yellow]Atrists:[white] %s", + song.Title, song.Album, song.Artist) + p.songInfo.SetText(info) + p.song = song + p.LoadAlbumArt(song.CoverArt) +} + +func (p *player) LoadAlbumArt(ID string) { + i, _ := p.client.GetCoverArt(ID) + p.artwork.SetImage(i) +} + +func (p *player) UpdateProgress(elapsed time.Duration) { + + if p.song.Duration == 0 { + // Startup... Show version number + versionInfo := tview.NewTextView().SetText("Version: 0.1") + versionInfo.SetBackgroundColor(config.ColorPlaybackProgressRemaining) + versionInfo.SetTextColor(config.ColorPlaybackProgressElapsed) + p.progress.AddItem(versionInfo, 0, 1, false) + return + } + + songDuration := time.Duration(p.song.Duration) * time.Second + overlappedBox := tview.NewTextView() + overlappedBox.SetBackgroundColor(config.ColorPlaybackProgressElapsed) + overlappedBox.SetTextColor(config.ColorPlaybackProgressRemaining) + overlappedBox.SetText(songDuration.String()) + remainingBox := tview.NewTextView() + remainingBox.SetBackgroundColor(config.ColorPlaybackProgressRemaining) + remainingBox.SetTextColor(config.ColorPlaybackProgressElapsed) + remainingBox.SetTextAlign(tview.AlignRight) + rm := time.Duration(songDuration.Seconds()-elapsed.Seconds()) * time.Second + remaining := fmt.Sprintf("-%s", rm.String()) + remainingBox.SetText(remaining) + p.progress.Clear() + p.progress.AddItem(overlappedBox, 0, int(elapsed.Seconds()), false) + p.progress.AddItem(remainingBox, 0, int(songDuration.Seconds())-int(elapsed.Seconds()), false) +} + +func (p *player) GetView() tview.Primitive { + return p.view +} + +func (p *player) Update() { + //p.UpdateProgress("00:00", 50) +} diff --git a/internal/tui/views/playlists.go b/internal/tui/views/playlists.go new file mode 100644 index 0000000..674720a --- /dev/null +++ b/internal/tui/views/playlists.go @@ -0,0 +1,63 @@ +package views + +import ( + "git.dayanhub.com/sagi/subsonic-tui/internal/client" + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "github.com/delucks/go-subsonic" + "github.com/rivo/tview" +) + +var _ View = &playlists{} + +type playlists struct { + view *tview.Table + client *client.Client + callback func(playlist *subsonic.Playlist) +} + +func NewPlaylists(client *client.Client) *playlists { + + obj := &playlists{ + client: client, + } + + list := tview.NewTable() + + list.SetBackgroundColor(config.ColorBackground) + list.SetTitle("Playlists [3]") + list.SetBorder(true) + list.SetFocusFunc(func() { + list.SetBorderColor(config.ColorSelectedBoarder) + list.SetSelectable(true, false) + }) + list.SetBlurFunc(func() { + list.SetBorderColor(config.ColorBluredBoarder) + list.SetSelectable(false, false) + }) + + pls, _ := client.GetPlaylists() + for i, pl := range pls { + cell := tview.NewTableCell(pl.Name).SetExpansion(1) + list.SetCell(i, 0, cell) + } + + list.SetSelectedFunc(func(row, column int) { + obj.callback(pls[row]) + }) + + obj.view = list + + return obj +} + +func (p *playlists) SetCallback(f func(playlist *subsonic.Playlist)) { + p.callback = f +} + +func (p *playlists) Update() { + +} + +func (p *playlists) GetView() tview.Primitive { + return p.view +} diff --git a/internal/tui/views/queue.go b/internal/tui/views/queue.go new file mode 100644 index 0000000..8ef7bb7 --- /dev/null +++ b/internal/tui/views/queue.go @@ -0,0 +1,81 @@ +package views + +import ( + "fmt" + "time" + + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "github.com/delucks/go-subsonic" + "github.com/rivo/tview" +) + +type queue struct { + view *tview.Table + playFunc func(index int) + currentSong int + songQueue []*subsonic.Child +} + +func NewQueue() *queue { + table := tview.NewTable() + table.SetBackgroundColor(config.ColorBackground) + table.SetTitle("Queue [4]") + table.SetBorder(true) + table.SetFocusFunc(func() { + table.SetBorderColor(config.ColorSelectedBoarder) + }) + table.SetBlurFunc(func() { + table.SetBorderColor(config.ColorBluredBoarder) + }) + + return &queue{ + view: table, + currentSong: 0, + } +} + +func (q *queue) SetPlayFunc(f func(index int)) { + q.playFunc = f +} + +func (q *queue) drawQueue() { + q.view.Clear() + list := q.view + list.SetWrapSelection(true, false) + list.SetSelectable(true, false) + for i, song := range q.songQueue { + isCurrentSong := q.currentSong == i + isPlayed := i < q.currentSong + bgColor := config.ColorBackground + if isCurrentSong { + bgColor = config.ColorQueuePlayingBg + } else if isPlayed { + bgColor = config.ColorQueuePlayedBg + } + num := tview.NewTableCell(fmt.Sprintf("%d", i+1)).SetTextColor(config.ColorTextAccent).SetBackgroundColor(bgColor) + title := tview.NewTableCell(song.Title).SetMaxWidth(15).SetExpansion(2).SetBackgroundColor(bgColor) + artist := tview.NewTableCell(song.Artist).SetMaxWidth(15).SetExpansion(1).SetBackgroundColor(bgColor) + d := time.Second * time.Duration(song.Duration) + duration := tview.NewTableCell(d.String()).SetAlign(tview.AlignRight).SetExpansion(1).SetBackgroundColor(bgColor) + list.SetCell(i, 0, num) + list.SetCell(i, 1, title) + list.SetCell(i, 2, artist) + list.SetCell(i, 3, duration) + } + + list.SetSelectedFunc(func(row, column int) { + q.currentSong = row + q.playFunc(row) + }) +} + +func (q *queue) Update(songs []*subsonic.Child, currentSong int) { + q.songQueue = songs + q.currentSong = currentSong + q.drawQueue() + +} + +func (q *queue) GetView() tview.Primitive { + return q.view +} diff --git a/internal/tui/views/status_line.go b/internal/tui/views/status_line.go new file mode 100644 index 0000000..4de74b6 --- /dev/null +++ b/internal/tui/views/status_line.go @@ -0,0 +1,98 @@ +package views + +import ( + "fmt" + + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +var _ View = &statusLine{} + +type statusLine struct { + view *tview.Flex + mode Statusmode + onUpdateFunc func() + onSearchFunc func(quary string) +} + +type Statusmode int + +const ( + StatusModeLog Statusmode = iota + StatusModeSearch Statusmode = iota +) + +func NewStatusLine() *statusLine { + status := tview.NewFlex() + status.SetBackgroundColor(config.ColorBackground) + // Default empty box + status.AddItem(EmptyBox, 0, 1, false) + + return &statusLine{ + view: status, + mode: StatusModeLog, + } +} + +func (s *statusLine) SetSearchFunc(f func(quary string)) { + s.onSearchFunc = f +} + +func (s *statusLine) Mode() Statusmode { + return s.mode +} + +func (s *statusLine) Search() { + s.mode = StatusModeSearch + s.view.Clear() + label := "Search: " + _, _, w, _ := s.view.GetRect() + query := "" + inputField := tview.NewInputField() + inputField. + SetLabel(label). + SetFieldWidth(w - len(label)). + SetDoneFunc(func(key tcell.Key) { + if key == tcell.KeyEnter { + s.mode = StatusModeLog + s.onSearchFunc(query) + } else if key == tcell.KeyEsc { + s.mode = StatusModeLog + s.onSearchFunc("") + } + }). + SetChangedFunc(func(text string) { + query = text + }) + inputField.Focus(nil) + inputField.SetBackgroundColor(config.ColorBackground) + inputField.SetFieldBackgroundColor(config.ColorBackground) + s.view.AddItem(inputField, 0, 1, true) + s.Update() +} + +func (s *statusLine) SetOnUpdateFunc(f func()) { + s.onUpdateFunc = f +} + +func (s *statusLine) Log(format string, a ...any) { + if s.mode != StatusModeLog { + return + } + str := fmt.Sprintf(format, a...) + s.view.Clear() + txt := tview.NewTextView().SetDynamicColors(true) + txt.SetBackgroundColor(config.ColorBackground) + txt.SetText(str) + s.view.AddItem(txt, 0, 1, false) + s.Update() +} + +func (s *statusLine) GetView() tview.Primitive { + return s.view +} +func (s *statusLine) Update() { + s.onUpdateFunc() +} diff --git a/internal/tui/views/view.go b/internal/tui/views/view.go new file mode 100644 index 0000000..cc5cb52 --- /dev/null +++ b/internal/tui/views/view.go @@ -0,0 +1,15 @@ +package views + +import ( + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "github.com/rivo/tview" +) + +type ViewEvent string + +var EmptyBox tview.Primitive = tview.NewBox().SetBackgroundColor(config.ColorBackground) + +type View interface { + GetView() tview.Primitive + Update() +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..3c8c8b6 --- /dev/null +++ b/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + + "git.dayanhub.com/sagi/subsonic-tui/internal/client" + "git.dayanhub.com/sagi/subsonic-tui/internal/config" + "git.dayanhub.com/sagi/subsonic-tui/internal/playback" + "git.dayanhub.com/sagi/subsonic-tui/internal/tui" +) + +func main() { + defer client.ArtCache.Destroy() + // Create Client + subsonicClient := client.NewClient(config.URL()) + err := subsonicClient.Authenticate(config.Username(), config.Password()) + if err != nil { + // We need to show Login... + login := tui.NewLogin() + err := login.Run() + if err != nil { + panic(err) + } + + } + fmt.Println("Trying to login...") + subsonicClient = client.NewClient(config.URL()) + err = subsonicClient.Authenticate(config.Username(), config.Password()) + if err != nil { + panic(err) + } + // Saving config - will result in adding new defaults to the file + config.SaveConfig() + + playbackCtl := playback.NewController(subsonicClient) + defer playbackCtl.Close() + + tui := tui.NewPlayer(subsonicClient, playbackCtl) + if err := tui.Run(); err != nil { + panic(err) + } +} diff --git a/renovate.json b/renovate.json new file mode 100644 index 0000000..c5f8d63 --- /dev/null +++ b/renovate.json @@ -0,0 +1,6 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "reviewers": [ + "sagi" + ] +}