├── .github ├── dependabot.yml └── workflows │ ├── rust-check.yml │ ├── rust-release-plz.yml │ └── rust-test.yml ├── .gitignore ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE ├── LICENSE-Apache ├── README.md ├── demo.tape ├── examples ├── basic.rs └── nested_group.rs └── src └── lib.rs /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 2 | 3 | version: 2 4 | updates: 5 | - package-ecosystem: "github-actions" 6 | directory: "/" 7 | schedule: 8 | interval: "weekly" 9 | - package-ecosystem: "cargo" 10 | directory: "/" 11 | schedule: 12 | interval: "weekly" 13 | -------------------------------------------------------------------------------- /.github/workflows/rust-check.yml: -------------------------------------------------------------------------------- 1 | name: Check 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | 9 | env: 10 | CARGO_TERM_COLOR: always 11 | 12 | # ensure that the workflow is only triggered once per PR, subsequent pushes to the PR will cancel 13 | # and restart the workflow. See https://docs.github.com/en/actions/using-jobs/using-concurrency 14 | concurrency: 15 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} 16 | cancel-in-progress: true 17 | 18 | jobs: 19 | fmt: 20 | name: stable / fmt 21 | runs-on: ubuntu-latest 22 | steps: 23 | - name: Checkout 24 | uses: actions/checkout@v4 25 | - name: Install Rust stable 26 | uses: dtolnay/rust-toolchain@stable 27 | with: 28 | components: rustfmt 29 | - name: Run cargo fmt 30 | run: cargo fmt -- --check 31 | - name: Cache Cargo dependencies 32 | uses: Swatinem/rust-cache@v2 33 | clippy: 34 | name: ${{ matrix.toolchain }} / clippy 35 | runs-on: ubuntu-latest 36 | permissions: 37 | checks: write 38 | strategy: 39 | fail-fast: false 40 | matrix: 41 | # Get early warnings about new lints introduced in the beta channel 42 | toolchain: [stable, beta] 43 | steps: 44 | - name: Checkout 45 | uses: actions/checkout@v4 46 | - name: Install Rust stable 47 | uses: dtolnay/rust-toolchain@stable 48 | with: 49 | components: clippy 50 | - name: Run clippy action 51 | uses: clechasseur/rs-clippy-check@v3 52 | - name: Cache Cargo dependencies 53 | uses: Swatinem/rust-cache@v2 54 | doc: 55 | # run docs generation on nightly rather than stable. This enables features like 56 | # https://doc.rust-lang.org/beta/unstable-book/language-features/doc-cfg.html which allows an 57 | # API be documented as only available in some specific platforms. 58 | name: nightly / doc 59 | runs-on: ubuntu-latest 60 | steps: 61 | - uses: actions/checkout@v4 62 | - name: Install Rust nightly 63 | uses: dtolnay/rust-toolchain@nightly 64 | - name: Run cargo doc 65 | run: cargo doc --no-deps --all-features 66 | env: 67 | RUSTDOCFLAGS: --cfg docsrs 68 | -------------------------------------------------------------------------------- /.github/workflows/rust-release-plz.yml: -------------------------------------------------------------------------------- 1 | name: Release-plz 2 | # see https://release-plz.ieni.dev/docs/github 3 | # for more information 4 | 5 | permissions: 6 | pull-requests: write 7 | contents: write 8 | 9 | on: 10 | push: 11 | branches: 12 | - main 13 | jobs: 14 | release-plz: 15 | name: Release-plz 16 | runs-on: ubuntu-latest 17 | steps: 18 | - name: Checkout 19 | uses: actions/checkout@v4 20 | with: 21 | fetch-depth: 0 22 | - name: Install Rust stable 23 | uses: dtolnay/rust-toolchain@stable 24 | - name: Run release-plz 25 | uses: MarcoIeni/release-plz-action@v0.5 26 | env: 27 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 28 | CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} 29 | -------------------------------------------------------------------------------- /.github/workflows/rust-test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | # This is the main CI workflow that runs the test suite on all pushes to main and all pull requests. 4 | # It runs the following jobs: 5 | # - required: runs the test suite on ubuntu with stable and beta rust toolchains 6 | # - minimal: runs the test suite with the minimal versions of the dependencies that satisfy the 7 | # requirements of this crate, and its dependencies 8 | # - os-check: runs the test suite on mac and windows 9 | # - coverage: runs the test suite and collects coverage information 10 | on: 11 | push: 12 | branches: 13 | - main 14 | pull_request: 15 | 16 | # ensure that the workflow is only triggered once per PR, subsequent pushes to the PR will cancel 17 | # and restart the workflow. See https://docs.github.com/en/actions/using-jobs/using-concurrency 18 | concurrency: 19 | group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} 20 | cancel-in-progress: true 21 | 22 | jobs: 23 | required: 24 | runs-on: ubuntu-latest 25 | name: ubuntu / ${{ matrix.toolchain }} 26 | strategy: 27 | matrix: 28 | # run on stable and beta to ensure that tests won't break on the next version of the rust 29 | # toolchain 30 | toolchain: [stable, beta] 31 | steps: 32 | - uses: actions/checkout@v4 33 | - name: Install ${{ matrix.toolchain }} 34 | uses: dtolnay/rust-toolchain@master 35 | with: 36 | toolchain: ${{ matrix.toolchain }} 37 | # enable this ci template to run regardless of whether the lockfile is checked in or not 38 | - name: cargo generate-lockfile 39 | if: hashFiles('Cargo.lock') == '' 40 | run: cargo generate-lockfile 41 | - name: cargo test --locked 42 | run: cargo test --locked --all-features --all-targets 43 | - name: cargo test --doc 44 | run: cargo test --locked --all-features --doc 45 | minimal-versions: 46 | # This action chooses the oldest version of the dependencies permitted by Cargo.toml to ensure 47 | # that this crate is compatible with the minimal version that this crate and its dependencies 48 | # require. This will pickup issues where this create relies on functionality that was introduced 49 | # later than the actual version specified (e.g., when we choose just a major version, but a 50 | # method was added after this version). 51 | # 52 | # This particular check can be difficult to get to succeed as often transitive dependencies may 53 | # be incorrectly specified (e.g., a dependency specifies 1.0 but really requires 1.1.5). There 54 | # is an alternative flag available -Zdirect-minimal-versions that uses the minimal versions for 55 | # direct dependencies of this crate, while selecting the maximal versions for the transitive 56 | # dependencies. Alternatively, you can add a line in your Cargo.toml to artificially increase 57 | # the minimal dependency, which you do with e.g.: 58 | # ```toml 59 | # # for minimal-versions 60 | # [target.'cfg(any())'.dependencies] 61 | # openssl = { version = "0.10.55", optional = true } # needed to allow foo to build with -Zminimal-versions 62 | # ``` 63 | # The optional = true is necessary in case that dependency isn't otherwise transitively required 64 | # by your library, and the target bit is so that this dependency edge never actually affects 65 | # Cargo build order. See also 66 | # https://github.com/jonhoo/fantoccini/blob/fde336472b712bc7ebf5b4e772023a7ba71b2262/Cargo.toml#L47-L49. 67 | # This action is run on ubuntu with the stable toolchain, as it is not expected to fail 68 | runs-on: ubuntu-latest 69 | name: ubuntu / stable / minimal-versions 70 | steps: 71 | - uses: actions/checkout@v4 72 | - name: Install Rust stable 73 | uses: dtolnay/rust-toolchain@stable 74 | - name: Install nightly for -Zdirect-minimal-versions 75 | uses: dtolnay/rust-toolchain@nightly 76 | - name: rustup default stable 77 | run: rustup default stable 78 | - name: cargo update -Zdirect-minimal-versions 79 | run: cargo +nightly update -Zdirect-minimal-versions 80 | - name: cargo test 81 | run: cargo test --locked --all-features --all-targets 82 | - name: Cache Cargo dependencies 83 | uses: Swatinem/rust-cache@v2 84 | os-check: 85 | # run cargo test on mac and windows 86 | runs-on: ${{ matrix.os }} 87 | name: ${{ matrix.os }} / stable 88 | strategy: 89 | fail-fast: false 90 | matrix: 91 | os: [macos-latest, windows-latest] 92 | steps: 93 | # if your project needs OpenSSL, uncomment this to fix Windows builds. 94 | # it's commented out by default as the install command takes 5-10m. 95 | # - run: echo "VCPKG_ROOT=$env:VCPKG_INSTALLATION_ROOT" | Out-File -FilePath $env:GITHUB_ENV -Append 96 | # if: runner.os == 'Windows' 97 | # - run: vcpkg install openssl:x64-windows-static-md 98 | # if: runner.os == 'Windows' 99 | - name: Checkout 100 | uses: actions/checkout@v4 101 | - name: Install Rust stable 102 | uses: dtolnay/rust-toolchain@stable 103 | - name: cargo generate-lockfile 104 | if: hashFiles('Cargo.lock') == '' 105 | run: cargo generate-lockfile 106 | - name: cargo test 107 | run: cargo test --locked --all-features --all-targets 108 | - name: Cache Cargo dependencies 109 | uses: Swatinem/rust-cache@v2 110 | coverage: 111 | # use llvm-cov to build and collect coverage and outputs in a format that 112 | # is compatible with codecov.io 113 | # 114 | # note that codecov as of v4 requires that CODECOV_TOKEN from 115 | # 116 | # https://app.codecov.io/gh///settings 117 | # 118 | # is set in two places on your repo: 119 | # 120 | # - https://github.com/jonhoo/guardian/settings/secrets/actions 121 | # - https://github.com/jonhoo/guardian/settings/secrets/dependabot 122 | # 123 | # (the former is needed for codecov uploads to work with Dependabot PRs) 124 | # 125 | # PRs coming from forks of your repo will not have access to the token, but 126 | # for those, codecov allows uploading coverage reports without a token. 127 | # it's all a little weird and inconvenient. see 128 | # 129 | # https://github.com/codecov/feedback/issues/112 130 | # 131 | # for lots of more discussion 132 | runs-on: ubuntu-latest 133 | name: ubuntu / stable / coverage 134 | steps: 135 | - name: Checkout 136 | uses: actions/checkout@v4 137 | - name: Install Rust stable 138 | uses: dtolnay/rust-toolchain@stable 139 | with: 140 | components: llvm-tools-preview 141 | - name: cargo install cargo-llvm-cov 142 | uses: taiki-e/install-action@cargo-llvm-cov 143 | - name: cargo generate-lockfile 144 | if: hashFiles('Cargo.lock') == '' 145 | run: cargo generate-lockfile 146 | - name: cargo llvm-cov 147 | run: cargo llvm-cov --locked --all-features --lcov --output-path lcov.info 148 | - name: Record Rust version 149 | run: echo "RUST=$(rustc --version)" >> "$GITHUB_ENV" 150 | - name: Cache Cargo dependencies 151 | uses: Swatinem/rust-cache@v2 152 | - name: Upload to codecov.io 153 | uses: codecov/codecov-action@v5 154 | with: 155 | fail_ci_if_error: true 156 | token: ${{ secrets.CODECOV_TOKEN }} 157 | env_vars: OS,RUST 158 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 8 | Cargo.lock 9 | 10 | # These are backup files generated by rustfmt 11 | **/*.rs.bk 12 | 13 | # MSVC Windows builds of rustc generate these, which store debugging information 14 | *.pdb 15 | 16 | # RustRover 17 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 18 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 19 | # and can be added to the global gitignore or merged into this file. For a more nuclear 20 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 21 | .idea/ 22 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 6 | 7 | ## [Unreleased] 8 | 9 | ## [0.3.0](https://github.com/shuoli84/tui-menu/compare/v0.2.4...v0.3.0) - 2024-12-09 10 | 11 | ### Other 12 | 13 | - *(deps)* update ratatui requirement from 0.28.0 to 0.29.0 (#21) 14 | 15 | ## [0.2.4](https://github.com/shuoli84/tui-menu/compare/v0.2.3...v0.2.4) - 2024-08-26 16 | 17 | ### Other 18 | - *(deps)* update ratatui requirement from 0.27.0 to 0.28.0 ([#19](https://github.com/shuoli84/tui-menu/pull/19)) 19 | 20 | ## [0.2.3](https://github.com/shuoli84/tui-menu/compare/v0.2.2...v0.2.3) - 2024-08-06 21 | 22 | ### Fixed 23 | - clippy lints ([#11](https://github.com/shuoli84/tui-menu/pull/11)) 24 | 25 | ### Other 26 | - *(deps)* use ratatui::crossterm instead of importing crossterm ([#16](https://github.com/shuoli84/tui-menu/pull/16)) 27 | - Fix bar being freezed after moving left or right ([#18](https://github.com/shuoli84/tui-menu/pull/18)) 28 | - add a nested group example, this demos how to handle deeply nested me… ([#14](https://github.com/shuoli84/tui-menu/pull/14)) 29 | - add more items 30 | - don't run release-plz on pull requests ([#10](https://github.com/shuoli84/tui-menu/pull/10)) 31 | 32 | ## [0.2.2](https://github.com/shuoli84/tui-menu/compare/v0.2.1...v0.2.2) - 2024-06-25 33 | 34 | ### Other 35 | - Update ratatui requirement from 0.26.1 to 0.27.0 ([#7](https://github.com/shuoli84/tui-menu/pull/7)) 36 | 37 | ## [0.2.1](https://github.com/shuoli84/tui-menu/compare/v0.2.0...v0.2.1) - 2024-04-05 38 | 39 | ### Other 40 | - Create demo and refactor basic.rs ([#3](https://github.com/shuoli84/tui-menu/pull/3)) 41 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "tui-menu" 3 | version = "0.3.0" 4 | edition = "2021" 5 | description = "A menu widget for Ratatui" 6 | license = "MIT OR Apache-2.0" 7 | keywords = ["tui"] 8 | repository = "https://github.com/shuoli84/tui-menu" 9 | 10 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 11 | 12 | [dependencies] 13 | ratatui = "0.29.0" 14 | 15 | [dev-dependencies] 16 | color-eyre = "0.6.3" 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 shuo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LICENSE-Apache: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tui-menu 2 | 3 | A menu widget for [Ratatui](https://crates.io/crates/ratatui). 4 | 5 | ![Demo](https://vhs.charm.sh/vhs-3pN84WzBTmPTbU2SCtvUiV.gif) 6 | 7 | ## Features 8 | 9 | - Sub menu groups. 10 | - Intuitive movement. 11 | - Item's data is generic as long as it ```Clone```able. 12 | 13 | ## Try 14 | 15 | ``` bash 16 | cargo run --example basic 17 | ``` 18 | 19 | ## Example 20 | 21 | take a look at examples/basic.rs 22 | 23 | ### Render 24 | 25 | ```rust 26 | // menu should be draw at last, so it can stay on top of other content 27 | let menu = Menu::new(); 28 | frame.render_stateful_widget(menu, chunks[0], &mut app.menu); 29 | ``` 30 | 31 | ### Create nested menu tree 32 | 33 | Note: MenuItems can be created from any type that implements `Clone`. Using an enum is just one 34 | option which can work. You could use strings or your own state types. 35 | 36 | ```rust 37 | #[derive(Debug, Clone)] 38 | enum Action { 39 | FileNew, 40 | FileOpen, 41 | FileOpenRecent(String), 42 | FileSaveAs, 43 | Exit, 44 | EditCopy, 45 | EditCut, 46 | EditPaste, 47 | AboutAuthor, 48 | AboutHelp, 49 | } 50 | 51 | let menu = MenuState::new(vec![ 52 | MenuItem::group( 53 | "File", 54 | vec![ 55 | MenuItem::item("New", Action::FileNew), 56 | MenuItem::item("Open", Action::FileOpen), 57 | MenuItem::group( 58 | "Open recent", 59 | ["file_1.txt", "file_2.txt"] 60 | .iter() 61 | .map(|&f| MenuItem::item(f, Action::FileOpenRecent(f.into()))) 62 | .collect(), 63 | ), 64 | MenuItem::item("Save as", Action::FileSaveAs), 65 | MenuItem::item("Exit", Action::Exit), 66 | ], 67 | ), 68 | MenuItem::group( 69 | "Edit", 70 | vec![ 71 | MenuItem::item("Copy", Action::EditCopy), 72 | MenuItem::item("Cut", Action::EditCut), 73 | MenuItem::item("Paste", Action::EditPaste), 74 | ], 75 | ), 76 | MenuItem::group( 77 | "About", 78 | vec![ 79 | MenuItem::item("Author", Action::AboutAuthor), 80 | MenuItem::item("Help", Action::AboutHelp), 81 | ], 82 | ), 83 | ]), 84 | ``` 85 | 86 | ### Consume events 87 | 88 | ``` rust 89 | for e in menu.drain_events() { 90 | match e { 91 | MenuEvent::Selected(item) => match item { 92 | Action::Exit => { 93 | return Ok(()); 94 | } 95 | Action::FileNew => { 96 | self.content.clear(); 97 | } 98 | Action::FileOpenRecent(file) => { 99 | self.content = format!("content of {file}"); 100 | } 101 | action => { 102 | self.content = format!("{action:?} not implemented"); 103 | } 104 | }, 105 | } 106 | // close the menu once the event has been handled. 107 | menu.reset(); 108 | } 109 | ``` 110 | -------------------------------------------------------------------------------- /demo.tape: -------------------------------------------------------------------------------- 1 | # VHS documentation 2 | # 3 | # Output: 4 | # Output .gif Create a GIF output at the given 5 | # Output .mp4 Create an MP4 output at the given 6 | # Output .webm Create a WebM output at the given 7 | # 8 | # Require: 9 | # Require Ensure a program is on the $PATH to proceed 10 | # 11 | # Settings: 12 | # Set FontSize Set the font size of the terminal 13 | # Set FontFamily Set the font family of the terminal 14 | # Set Height Set the height of the terminal 15 | # Set Width Set the width of the terminal 16 | # Set LetterSpacing Set the font letter spacing (tracking) 17 | # Set LineHeight Set the font line height 18 | # Set LoopOffset % Set the starting frame offset for the GIF loop 19 | # Set Theme Set the theme of the terminal 20 | # Set Padding Set the padding of the terminal 21 | # Set Framerate Set the framerate of the recording 22 | # Set PlaybackSpeed Set the playback speed of the recording 23 | # Set MarginFill Set the file or color the margin will be filled with. 24 | # Set Margin Set the size of the margin. Has no effect if MarginFill isn't set. 25 | # Set BorderRadius Set terminal border radius, in pixels. 26 | # Set WindowBar Set window bar type. (one of: Rings, RingsRight, Colorful, ColorfulRight) 27 | # Set WindowBarSize Set window bar size, in pixels. Default is 40. 28 | # Set TypingSpeed