mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-12-16 01:48:56 +08:00
Compare commits
2 Commits
2b9913e2eb
...
copilot/fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac93764bbf | ||
|
|
c0cb3e84cc |
212
.github/copilot-instructions.md
vendored
212
.github/copilot-instructions.md
vendored
@@ -1,212 +0,0 @@
|
|||||||
# FTXUI - Functional Terminal (X) User Interface
|
|
||||||
|
|
||||||
FTXUI is a cross-platform C++ library for terminal-based user interfaces with a functional programming approach, inspired by React.
|
|
||||||
|
|
||||||
**ALWAYS reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the information here.**
|
|
||||||
|
|
||||||
## Working Effectively
|
|
||||||
|
|
||||||
### Build System Setup and Commands
|
|
||||||
- Bootstrap and build the repository:
|
|
||||||
```bash
|
|
||||||
# Basic build (library only) - fast
|
|
||||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
|
|
||||||
cmake --build build --parallel $(nproc)
|
|
||||||
# Build time: ~30 seconds. NEVER CANCEL. Set timeout to 120+ seconds.
|
|
||||||
|
|
||||||
# Build with examples
|
|
||||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DFTXUI_BUILD_EXAMPLES=ON
|
|
||||||
cmake --build build --parallel $(nproc)
|
|
||||||
# Build time: ~70 seconds. NEVER CANCEL. Set timeout to 180+ seconds.
|
|
||||||
|
|
||||||
# Build with examples and tests
|
|
||||||
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DFTXUI_BUILD_EXAMPLES=ON -DFTXUI_BUILD_TESTS=ON
|
|
||||||
cmake --build build --parallel $(nproc)
|
|
||||||
# Build time: ~113 seconds (includes GoogleTest download). NEVER CANCEL. Set timeout to 300+ seconds.
|
|
||||||
```
|
|
||||||
|
|
||||||
- Alternative build with Ninja (faster):
|
|
||||||
```bash
|
|
||||||
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DFTXUI_BUILD_EXAMPLES=ON
|
|
||||||
ninja -C build
|
|
||||||
# Build time: ~62 seconds. NEVER CANCEL. Set timeout to 180+ seconds.
|
|
||||||
```
|
|
||||||
|
|
||||||
- Run unit tests:
|
|
||||||
```bash
|
|
||||||
# Configure with tests enabled first, then:
|
|
||||||
cd build && ctest --output-on-failure
|
|
||||||
# Test time: ~4 seconds (302 tests). NEVER CANCEL. Set timeout to 60+ seconds.
|
|
||||||
```
|
|
||||||
|
|
||||||
### Bazel Support
|
|
||||||
- FTXUI also supports Bazel build system
|
|
||||||
- **WARNING**: Bazel may fail due to network connectivity issues in sandboxed environments
|
|
||||||
- If Bazel is available:
|
|
||||||
```bash
|
|
||||||
bazel build //... # Build everything
|
|
||||||
bazel test //... # Run tests
|
|
||||||
```
|
|
||||||
|
|
||||||
## Validation
|
|
||||||
|
|
||||||
### Manual Testing After Changes
|
|
||||||
- **ALWAYS manually validate changes by building and running examples after making code modifications**
|
|
||||||
- Run example applications to verify functionality:
|
|
||||||
```bash
|
|
||||||
# Build an example first
|
|
||||||
cmake --build build --target ftxui_example_border
|
|
||||||
|
|
||||||
# Run examples (they are interactive, use timeout to terminate)
|
|
||||||
timeout 2s build/examples/dom/ftxui_example_border
|
|
||||||
timeout 2s build/examples/dom/ftxui_example_color_gallery
|
|
||||||
timeout 2s build/examples/component/ftxui_example_button
|
|
||||||
```
|
|
||||||
- Examples should produce visual terminal output with borders, colors, and UI components
|
|
||||||
- **CRITICAL**: Always run at least one DOM example and one Component example to verify both modules work
|
|
||||||
|
|
||||||
### Code Quality and Formatting
|
|
||||||
- Always run formatting before committing:
|
|
||||||
```bash
|
|
||||||
./tools/format.sh
|
|
||||||
# Format time: ~7 seconds. NEVER CANCEL. Set timeout to 60+ seconds.
|
|
||||||
```
|
|
||||||
- The format script adds license headers and runs clang-format on all source files
|
|
||||||
- **Required**: Run formatting or the CI (.github/workflows/build.yaml) will fail
|
|
||||||
|
|
||||||
### Build Validation Requirements
|
|
||||||
- ALWAYS build with both `-DFTXUI_BUILD_EXAMPLES=ON` and `-DFTXUI_BUILD_TESTS=ON` when making changes
|
|
||||||
- Run the complete test suite with `ctest --output-on-failure`
|
|
||||||
- All 302 tests must pass
|
|
||||||
- **Scenario Testing**: Run at least these validation scenarios:
|
|
||||||
1. Build library only (basic validation)
|
|
||||||
2. Build with examples and run 2-3 different examples
|
|
||||||
3. Build with tests and run complete test suite
|
|
||||||
4. Run formatting tool to ensure code style compliance
|
|
||||||
|
|
||||||
## Project Structure
|
|
||||||
|
|
||||||
### Key Directories
|
|
||||||
```
|
|
||||||
/home/runner/work/FTXUI/FTXUI/
|
|
||||||
├── include/ftxui/ # Public header files
|
|
||||||
│ ├── component/ # Interactive component headers
|
|
||||||
│ ├── dom/ # DOM element headers
|
|
||||||
│ ├── screen/ # Screen and rendering headers
|
|
||||||
│ └── util/ # Utility headers
|
|
||||||
├── src/ftxui/ # Implementation files
|
|
||||||
│ ├── component/ # Interactive components (buttons, menus, etc.)
|
|
||||||
│ ├── dom/ # DOM elements (layout, styling, text)
|
|
||||||
│ ├── screen/ # Screen rendering and terminal handling
|
|
||||||
│ └── util/ # Utilities
|
|
||||||
├── examples/ # Example applications
|
|
||||||
│ ├── component/ # Interactive component examples
|
|
||||||
│ └── dom/ # DOM element examples
|
|
||||||
├── cmake/ # CMake configuration files
|
|
||||||
├── tools/ # Development tools (formatting, etc.)
|
|
||||||
└── .github/workflows/ # CI/CD configuration
|
|
||||||
```
|
|
||||||
|
|
||||||
### Core Library Modules
|
|
||||||
FTXUI is organized into three main modules that depend on each other:
|
|
||||||
```
|
|
||||||
┌component──┐ (Interactive UI components)
|
|
||||||
│┌dom──────┐│ (Layout and styling elements)
|
|
||||||
││┌screen─┐││ (Terminal rendering and input)
|
|
||||||
└┴┴───────┴┴┘
|
|
||||||
```
|
|
||||||
|
|
||||||
1. **screen**: Low-level terminal handling, colors, pixels, input
|
|
||||||
2. **dom**: Layout elements (hbox, vbox, text, borders, etc.)
|
|
||||||
3. **component**: Interactive components (buttons, menus, input fields)
|
|
||||||
|
|
||||||
### CMake Build Options
|
|
||||||
| Option | Description | Default |
|
|
||||||
|-----------------------------------|----------------------------------|---------|
|
|
||||||
| FTXUI_BUILD_EXAMPLES | Build example applications | OFF |
|
|
||||||
| FTXUI_BUILD_DOCS | Build documentation | OFF |
|
|
||||||
| FTXUI_BUILD_TESTS | Build and enable tests | OFF |
|
|
||||||
| FTXUI_BUILD_MODULES | Build C++20 modules | OFF |
|
|
||||||
| FTXUI_ENABLE_INSTALL | Generate install targets | ON |
|
|
||||||
| FTXUI_MICROSOFT_TERMINAL_FALLBACK | Windows terminal compatibility | ON/OFF |
|
|
||||||
|
|
||||||
## Common Tasks
|
|
||||||
|
|
||||||
### Building Examples
|
|
||||||
```bash
|
|
||||||
# Build specific examples
|
|
||||||
cmake --build build --target ftxui_example_border
|
|
||||||
cmake --build build --target ftxui_example_button
|
|
||||||
cmake --build build --target ftxui_example_menu
|
|
||||||
|
|
||||||
# List all available examples
|
|
||||||
find build -name "ftxui_example_*" -type f
|
|
||||||
```
|
|
||||||
|
|
||||||
### Running Tests
|
|
||||||
```bash
|
|
||||||
# Run all tests
|
|
||||||
cd build && ctest
|
|
||||||
|
|
||||||
# Run tests with verbose output
|
|
||||||
cd build && ctest --verbose
|
|
||||||
|
|
||||||
# Run specific test pattern
|
|
||||||
cd build && ctest -R "Button" --verbose
|
|
||||||
```
|
|
||||||
|
|
||||||
### Working with Source Code
|
|
||||||
- **Component Development**: Modify files in `src/ftxui/component/` for interactive elements
|
|
||||||
- **DOM Development**: Modify files in `src/ftxui/dom/` for layout and styling
|
|
||||||
- **Screen Development**: Modify files in `src/ftxui/screen/` for terminal rendering
|
|
||||||
- **Adding Examples**: Add new `.cpp` files in `examples/component/` or `examples/dom/`
|
|
||||||
- **Header Files**: Public APIs are in `include/ftxui/[module]/`
|
|
||||||
|
|
||||||
### Integration Patterns
|
|
||||||
When adding FTXUI to a project, use CMake FetchContent (recommended):
|
|
||||||
```cmake
|
|
||||||
include(FetchContent)
|
|
||||||
FetchContent_Declare(ftxui
|
|
||||||
GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui
|
|
||||||
GIT_TAG v6.1.9
|
|
||||||
)
|
|
||||||
FetchContent_MakeAvailable(ftxui)
|
|
||||||
|
|
||||||
target_link_libraries(your_target PRIVATE
|
|
||||||
ftxui::component # For interactive components
|
|
||||||
ftxui::dom # For layout elements
|
|
||||||
ftxui::screen # For basic rendering
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Troubleshooting
|
|
||||||
|
|
||||||
### Build Issues
|
|
||||||
- If CMake configuration fails, ensure C++20 support: `cmake --version` (need 3.12+)
|
|
||||||
- If Ninja build fails, fall back to Make: `cmake -S . -B build` (without `-G Ninja`)
|
|
||||||
- If tests fail to build, GoogleTest download might have failed - check network connectivity
|
|
||||||
- Build artifacts are in `build/` directory - delete with `rm -rf build` to clean
|
|
||||||
|
|
||||||
### Example Issues
|
|
||||||
- Examples are interactive terminal applications - use `timeout` to terminate them
|
|
||||||
- If examples don't display correctly, terminal might not support colors/Unicode
|
|
||||||
- Examples require terminal size of at least 80x24 for proper display
|
|
||||||
|
|
||||||
### Formatting Issues
|
|
||||||
- Format script requires clang-format to be installed
|
|
||||||
- If format script fails, check that source files are not read-only
|
|
||||||
- Format script modifies files in-place - commit changes afterwards
|
|
||||||
|
|
||||||
## Critical Reminders
|
|
||||||
|
|
||||||
- **NEVER CANCEL long-running builds** - they may take 2-3 minutes
|
|
||||||
- **ALWAYS run the formatting tool** before committing changes
|
|
||||||
- **ALWAYS build and test examples** when making component/dom changes
|
|
||||||
- **SET APPROPRIATE TIMEOUTS**: 300+ seconds for builds, 60+ seconds for tests
|
|
||||||
- **BUILD TIMING EXPECTATIONS**:
|
|
||||||
- Basic library: ~30 seconds
|
|
||||||
- With examples: ~70 seconds
|
|
||||||
- With examples + tests: ~113 seconds (first time, includes GoogleTest download)
|
|
||||||
- Subsequent builds: ~60-70 seconds
|
|
||||||
- Tests execution: ~4 seconds
|
|
||||||
- Formatting: ~7 seconds
|
|
||||||
16
.github/workflows/documentation.yaml
vendored
16
.github/workflows/documentation.yaml
vendored
@@ -12,10 +12,6 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: "Checkout repository"
|
- name: "Checkout repository"
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
with:
|
|
||||||
fetch-depth: 0 # Need full history.
|
|
||||||
fetch-tags: true # Need tags.
|
|
||||||
|
|
||||||
|
|
||||||
- name: "Install cmake"
|
- name: "Install cmake"
|
||||||
uses: lukka/get-cmake@latest
|
uses: lukka/get-cmake@latest
|
||||||
@@ -34,23 +30,19 @@ jobs:
|
|||||||
sudo apt-get install graphviz;
|
sudo apt-get install graphviz;
|
||||||
|
|
||||||
- name: "Build documentation"
|
- name: "Build documentation"
|
||||||
run: |
|
|
||||||
python3 ./tools/build_multiversion_doc.py
|
|
||||||
|
|
||||||
- name: "Build examples"
|
|
||||||
run: >
|
run: >
|
||||||
mkdir -p multiversion_docs/main/examples;
|
|
||||||
mkdir build;
|
mkdir build;
|
||||||
cd build;
|
cd build;
|
||||||
emcmake cmake ..
|
emcmake cmake ..
|
||||||
-DCMAKE_BUILD_TYPE=Release
|
-DCMAKE_BUILD_TYPE=Release
|
||||||
-DFTXUI_BUILD_DOCS=OFF
|
-DFTXUI_BUILD_DOCS=ON
|
||||||
-DFTXUI_BUILD_EXAMPLES=ON
|
-DFTXUI_BUILD_EXAMPLES=ON
|
||||||
-DFTXUI_BUILD_TESTS=OFF
|
-DFTXUI_BUILD_TESTS=OFF
|
||||||
-DFTXUI_BUILD_TESTS_FUZZER=OFF
|
-DFTXUI_BUILD_TESTS_FUZZER=OFF
|
||||||
-DFTXUI_ENABLE_INSTALL=OFF
|
-DFTXUI_ENABLE_INSTALL=OFF
|
||||||
-DFTXUI_DEV_WARNINGS=OFF;
|
-DFTXUI_DEV_WARNINGS=OFF;
|
||||||
cmake --build . --target doc;
|
cmake --build . --target doc;
|
||||||
|
cmake --build . ;
|
||||||
rsync -amv
|
rsync -amv
|
||||||
--include='*/'
|
--include='*/'
|
||||||
--include='*.html'
|
--include='*.html'
|
||||||
@@ -60,13 +52,13 @@ jobs:
|
|||||||
--include='*.wasm'
|
--include='*.wasm'
|
||||||
--exclude='*'
|
--exclude='*'
|
||||||
examples
|
examples
|
||||||
../multiversion_docs/main/examples;
|
doc/doxygen/html;
|
||||||
|
|
||||||
- name: "Deploy"
|
- name: "Deploy"
|
||||||
uses: peaceiris/actions-gh-pages@v3
|
uses: peaceiris/actions-gh-pages@v3
|
||||||
with:
|
with:
|
||||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
publish_dir: multiversion_docs
|
publish_dir: build/doc/doxygen/html/
|
||||||
enable_jekyll: false
|
enable_jekyll: false
|
||||||
allow_empty_commit: false
|
allow_empty_commit: false
|
||||||
force_orphan: true
|
force_orphan: true
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -28,7 +28,6 @@ out/
|
|||||||
# .github directory:
|
# .github directory:
|
||||||
!.github/**/*.yaml
|
!.github/**/*.yaml
|
||||||
!.github/**/*.yml
|
!.github/**/*.yml
|
||||||
!.github/**/*.md
|
|
||||||
|
|
||||||
# cmake directory:
|
# cmake directory:
|
||||||
!cmake/**/*.in
|
!cmake/**/*.in
|
||||||
@@ -70,6 +69,4 @@ out/
|
|||||||
|
|
||||||
# tools directory:
|
# tools directory:
|
||||||
!tools/**/*.sh
|
!tools/**/*.sh
|
||||||
!tools/**/*.py
|
|
||||||
!tools/**/*.cpp
|
!tools/**/*.cpp
|
||||||
build/
|
|
||||||
|
|||||||
@@ -365,7 +365,6 @@ Feel free to add your projects here:
|
|||||||
- [SHOOT!](https://github.com/ShingZhanho/ENGG1340-Project-25Spring)
|
- [SHOOT!](https://github.com/ShingZhanho/ENGG1340-Project-25Spring)
|
||||||
- [VerifySN (Fast Hash Tool)](https://github.com/d06i/verifySN)
|
- [VerifySN (Fast Hash Tool)](https://github.com/d06i/verifySN)
|
||||||
- [tic-tac-toe](https://github.com/birland/tic-tac-toe)
|
- [tic-tac-toe](https://github.com/birland/tic-tac-toe)
|
||||||
- [typing-speed-test](https://github.com/ymcx/typing-speed-test)
|
|
||||||
|
|
||||||
### [cpp-best-practices/game_jam](https://github.com/cpp-best-practices/game_jam)
|
### [cpp-best-practices/game_jam](https://github.com/cpp-best-practices/game_jam)
|
||||||
|
|
||||||
@@ -417,9 +416,9 @@ cc_binary(
|
|||||||
name = "your_target",
|
name = "your_target",
|
||||||
srcs = ["your_source.cc"],
|
srcs = ["your_source.cc"],
|
||||||
deps = [
|
deps = [
|
||||||
"@ftxui//:component",
|
"@ftxui//:ftxui_component",
|
||||||
"@ftxui//:dom",
|
"@ftxui//:ftxui_dom",
|
||||||
"@ftxui//:screen",
|
"@ftxui//:ftxui_screen",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|||||||
1
build/_deps/googlebenchmark-src
Submodule
1
build/_deps/googlebenchmark-src
Submodule
Submodule build/_deps/googlebenchmark-src added at 015d1a091a
42
build/_deps/googlebenchmark-subbuild/CMakeLists.txt
Normal file
42
build/_deps/googlebenchmark-subbuild/CMakeLists.txt
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
||||||
|
# file Copyright.txt or https://cmake.org/licensing for details.
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.31.6)
|
||||||
|
|
||||||
|
# Reject any attempt to use a toolchain file. We must not use one because
|
||||||
|
# we could be downloading it here. If the CMAKE_TOOLCHAIN_FILE environment
|
||||||
|
# variable is set, the cache variable will have been initialized from it.
|
||||||
|
unset(CMAKE_TOOLCHAIN_FILE CACHE)
|
||||||
|
unset(ENV{CMAKE_TOOLCHAIN_FILE})
|
||||||
|
|
||||||
|
# We name the project and the target for the ExternalProject_Add() call
|
||||||
|
# to something that will highlight to the user what we are working on if
|
||||||
|
# something goes wrong and an error message is produced.
|
||||||
|
|
||||||
|
project(googlebenchmark-populate NONE)
|
||||||
|
|
||||||
|
|
||||||
|
# Pass through things we've already detected in the main project to avoid
|
||||||
|
# paying the cost of redetecting them again in ExternalProject_Add()
|
||||||
|
set(GIT_EXECUTABLE [==[/usr/bin/git]==])
|
||||||
|
set(GIT_VERSION_STRING [==[2.51.0]==])
|
||||||
|
set_property(GLOBAL PROPERTY _CMAKE_FindGit_GIT_EXECUTABLE_VERSION
|
||||||
|
[==[/usr/bin/git;2.51.0]==]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
include(ExternalProject)
|
||||||
|
ExternalProject_Add(googlebenchmark-populate
|
||||||
|
"UPDATE_DISCONNECTED" "False" "GIT_REPOSITORY" "https://github.com/google/benchmark" "EXTERNALPROJECT_INTERNAL_ARGUMENT_SEPARATOR" "GIT_TAG" "015d1a091af6937488242b70121858bce8fd40e9" "GIT_PROGRESS" "TRUE"
|
||||||
|
SOURCE_DIR "/home/runner/work/FTXUI/FTXUI/build/_deps/googlebenchmark-src"
|
||||||
|
BINARY_DIR "/home/runner/work/FTXUI/FTXUI/build/_deps/googlebenchmark-build"
|
||||||
|
CONFIGURE_COMMAND ""
|
||||||
|
BUILD_COMMAND ""
|
||||||
|
INSTALL_COMMAND ""
|
||||||
|
TEST_COMMAND ""
|
||||||
|
USES_TERMINAL_DOWNLOAD YES
|
||||||
|
USES_TERMINAL_UPDATE YES
|
||||||
|
USES_TERMINAL_PATCH YES
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
1
build/_deps/googletest-src
Submodule
1
build/_deps/googletest-src
Submodule
Submodule build/_deps/googletest-src added at 23ef29555e
42
build/_deps/googletest-subbuild/CMakeLists.txt
Normal file
42
build/_deps/googletest-subbuild/CMakeLists.txt
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
||||||
|
# file Copyright.txt or https://cmake.org/licensing for details.
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.31.6)
|
||||||
|
|
||||||
|
# Reject any attempt to use a toolchain file. We must not use one because
|
||||||
|
# we could be downloading it here. If the CMAKE_TOOLCHAIN_FILE environment
|
||||||
|
# variable is set, the cache variable will have been initialized from it.
|
||||||
|
unset(CMAKE_TOOLCHAIN_FILE CACHE)
|
||||||
|
unset(ENV{CMAKE_TOOLCHAIN_FILE})
|
||||||
|
|
||||||
|
# We name the project and the target for the ExternalProject_Add() call
|
||||||
|
# to something that will highlight to the user what we are working on if
|
||||||
|
# something goes wrong and an error message is produced.
|
||||||
|
|
||||||
|
project(googletest-populate NONE)
|
||||||
|
|
||||||
|
|
||||||
|
# Pass through things we've already detected in the main project to avoid
|
||||||
|
# paying the cost of redetecting them again in ExternalProject_Add()
|
||||||
|
set(GIT_EXECUTABLE [==[/usr/bin/git]==])
|
||||||
|
set(GIT_VERSION_STRING [==[2.51.0]==])
|
||||||
|
set_property(GLOBAL PROPERTY _CMAKE_FindGit_GIT_EXECUTABLE_VERSION
|
||||||
|
[==[/usr/bin/git;2.51.0]==]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
include(ExternalProject)
|
||||||
|
ExternalProject_Add(googletest-populate
|
||||||
|
"UPDATE_DISCONNECTED" "False" "GIT_REPOSITORY" "https://github.com/google/googletest" "EXTERNALPROJECT_INTERNAL_ARGUMENT_SEPARATOR" "GIT_TAG" "23ef29555ef4789f555f1ba8c51b4c52975f0907" "GIT_PROGRESS" "TRUE"
|
||||||
|
SOURCE_DIR "/home/runner/work/FTXUI/FTXUI/build/_deps/googletest-src"
|
||||||
|
BINARY_DIR "/home/runner/work/FTXUI/FTXUI/build/_deps/googletest-build"
|
||||||
|
CONFIGURE_COMMAND ""
|
||||||
|
BUILD_COMMAND ""
|
||||||
|
INSTALL_COMMAND ""
|
||||||
|
TEST_COMMAND ""
|
||||||
|
USES_TERMINAL_DOWNLOAD YES
|
||||||
|
USES_TERMINAL_UPDATE YES
|
||||||
|
USES_TERMINAL_PATCH YES
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -2,9 +2,16 @@
|
|||||||
<!-- start footer part -->
|
<!-- start footer part -->
|
||||||
<!--BEGIN GENERATE_TREEVIEW-->
|
<!--BEGIN GENERATE_TREEVIEW-->
|
||||||
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
||||||
|
<ul>
|
||||||
|
$navpath
|
||||||
|
<li class="footer">$generatedby <a href="https://www.doxygen.org/index.html"><img class="footer" src="$relpath^doxygen.svg" width="104" height="31" alt="doxygen"/></a> $doxygenversion </li>
|
||||||
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<!--END GENERATE_TREEVIEW-->
|
<!--END GENERATE_TREEVIEW-->
|
||||||
<!--BEGIN !GENERATE_TREEVIEW-->
|
<!--BEGIN !GENERATE_TREEVIEW-->
|
||||||
|
<hr class="footer"/><address class="footer"><small>
|
||||||
|
$generatedby <a href="https://www.doxygen.org/index.html"><img class="footer" src="$relpath^doxygen.svg" width="104" height="31" alt="doxygen"/></a> $doxygenversion
|
||||||
|
</small></address>
|
||||||
<!--END !GENERATE_TREEVIEW-->
|
<!--END !GENERATE_TREEVIEW-->
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ example(radiobox)
|
|||||||
example(radiobox_in_frame)
|
example(radiobox_in_frame)
|
||||||
example(renderer)
|
example(renderer)
|
||||||
example(resizable_split)
|
example(resizable_split)
|
||||||
example(resizable_split_clamp)
|
|
||||||
example(scrollbar)
|
example(scrollbar)
|
||||||
example(selection)
|
example(selection)
|
||||||
example(slider)
|
example(slider)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
// the LICENSE file.
|
// the LICENSE file.
|
||||||
#include <memory> // for shared_ptr, allocator, __shared_ptr_access
|
#include <memory> // for shared_ptr, allocator, __shared_ptr_access
|
||||||
|
|
||||||
|
#include "ftxui/component/captured_mouse.hpp" // for ftxui
|
||||||
#include "ftxui/component/component.hpp" // for Renderer, ResizableSplitBottom, ResizableSplitLeft, ResizableSplitRight, ResizableSplitTop
|
#include "ftxui/component/component.hpp" // for Renderer, ResizableSplitBottom, ResizableSplitLeft, ResizableSplitRight, ResizableSplitTop
|
||||||
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
||||||
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
|
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
|
||||||
@@ -13,24 +14,17 @@ using namespace ftxui;
|
|||||||
int main() {
|
int main() {
|
||||||
auto screen = ScreenInteractive::Fullscreen();
|
auto screen = ScreenInteractive::Fullscreen();
|
||||||
|
|
||||||
// State:
|
auto middle = Renderer([] { return text("middle") | center; });
|
||||||
|
auto left = Renderer([] { return text("Left") | center; });
|
||||||
|
auto right = Renderer([] { return text("right") | center; });
|
||||||
|
auto top = Renderer([] { return text("top") | center; });
|
||||||
|
auto bottom = Renderer([] { return text("bottom") | center; });
|
||||||
|
|
||||||
int left_size = 20;
|
int left_size = 20;
|
||||||
int right_size = 20;
|
int right_size = 20;
|
||||||
int top_size = 10;
|
int top_size = 10;
|
||||||
int bottom_size = 10;
|
int bottom_size = 10;
|
||||||
|
|
||||||
// Renderers:
|
|
||||||
auto RendererInfo = [](const std::string& name, int* size) {
|
|
||||||
return Renderer([name, size] {
|
|
||||||
return text(name + ": " + std::to_string(*size)) | center;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
auto middle = Renderer([] { return text("Middle") | center; });
|
|
||||||
auto left = RendererInfo("Left", &left_size);
|
|
||||||
auto right = RendererInfo("Right", &right_size);
|
|
||||||
auto top = RendererInfo("Top", &top_size);
|
|
||||||
auto bottom = RendererInfo("Bottom", &bottom_size);
|
|
||||||
|
|
||||||
auto container = middle;
|
auto container = middle;
|
||||||
container = ResizableSplitLeft(left, container, &left_size);
|
container = ResizableSplitLeft(left, container, &left_size);
|
||||||
container = ResizableSplitRight(right, container, &right_size);
|
container = ResizableSplitRight(right, container, &right_size);
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
// Copyright 2025 Arthur Sonzogni. All rights reserved.
|
|
||||||
// Use of this source code is governed by the MIT license that can be found in
|
|
||||||
// the LICENSE file.
|
|
||||||
#include <memory> // for shared_ptr, allocator, __shared_ptr_access
|
|
||||||
|
|
||||||
#include "ftxui/component/component.hpp" // for Renderer, ResizableSplitBottom, ResizableSplitLeft, ResizableSplitRight, ResizableSplitTop
|
|
||||||
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
|
||||||
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
|
|
||||||
#include "ftxui/dom/elements.hpp" // for Element, operator|, text, center, border
|
|
||||||
|
|
||||||
using namespace ftxui;
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
auto screen = ScreenInteractive::Fullscreen();
|
|
||||||
|
|
||||||
// State:
|
|
||||||
int size = 40;
|
|
||||||
int size_min = 10;
|
|
||||||
int size_max = 80;
|
|
||||||
|
|
||||||
// Renderers:
|
|
||||||
auto split = ResizableSplit({
|
|
||||||
.main = Renderer([] { return text("Left") | center; }),
|
|
||||||
.back = Renderer([] { return text("Right") | center; }),
|
|
||||||
.direction = Direction::Left,
|
|
||||||
.main_size = &size,
|
|
||||||
.min = &size_min,
|
|
||||||
.max = &size_max,
|
|
||||||
});
|
|
||||||
|
|
||||||
auto renderer = Renderer(split, [&] {
|
|
||||||
return window(text("Drag the separator with the mouse"),
|
|
||||||
vbox({
|
|
||||||
text("Min: " + std::to_string(size_min)),
|
|
||||||
text("Max: " + std::to_string(size_max)),
|
|
||||||
text("Size: " + std::to_string(size)),
|
|
||||||
separator(),
|
|
||||||
split->Render() | flex,
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
screen.Loop(renderer);
|
|
||||||
}
|
|
||||||
@@ -11,7 +11,6 @@
|
|||||||
#include <ftxui/util/ref.hpp> // for Ref, ConstRef, StringRef
|
#include <ftxui/util/ref.hpp> // for Ref, ConstRef, StringRef
|
||||||
#include <ftxui/util/warn_windows_macro.hpp>
|
#include <ftxui/util/warn_windows_macro.hpp>
|
||||||
#include <functional> // for function
|
#include <functional> // for function
|
||||||
#include <limits> // for numeric_limits
|
|
||||||
#include <string> // for string
|
#include <string> // for string
|
||||||
|
|
||||||
#include "ftxui/component/component_base.hpp" // for Component
|
#include "ftxui/component/component_base.hpp" // for Component
|
||||||
@@ -218,10 +217,6 @@ struct ResizableSplitOption {
|
|||||||
(direction() == Direction::Left || direction() == Direction::Right) ? 20
|
(direction() == Direction::Left || direction() == Direction::Right) ? 20
|
||||||
: 10;
|
: 10;
|
||||||
std::function<Element()> separator_func = [] { return ::ftxui::separator(); };
|
std::function<Element()> separator_func = [] { return ::ftxui::separator(); };
|
||||||
|
|
||||||
// Constraints on main_size:
|
|
||||||
Ref<int> min = 0;
|
|
||||||
Ref<int> max = std::numeric_limits<int>::max();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// @brief Option for the `Slider` component.
|
// @brief Option for the `Slider` component.
|
||||||
|
|||||||
@@ -4,10 +4,10 @@
|
|||||||
#ifndef FTXUI_COMPONENT_RECEIVER_HPP_
|
#ifndef FTXUI_COMPONENT_RECEIVER_HPP_
|
||||||
#define FTXUI_COMPONENT_RECEIVER_HPP_
|
#define FTXUI_COMPONENT_RECEIVER_HPP_
|
||||||
|
|
||||||
|
#include <ftxui/util/warn_windows_macro.hpp>
|
||||||
#include <algorithm> // for copy, max
|
#include <algorithm> // for copy, max
|
||||||
#include <atomic> // for atomic, __atomic_base
|
#include <atomic> // for atomic, __atomic_base
|
||||||
#include <condition_variable> // for condition_variable
|
#include <condition_variable> // for condition_variable
|
||||||
#include <ftxui/util/warn_windows_macro.hpp>
|
|
||||||
#include <memory> // for unique_ptr, make_unique
|
#include <memory> // for unique_ptr, make_unique
|
||||||
#include <mutex> // for mutex, unique_lock
|
#include <mutex> // for mutex, unique_lock
|
||||||
#include <queue> // for queue
|
#include <queue> // for queue
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
// Copyright 2021 Arthur Sonzogni. All rights reserved.
|
// Copyright 2021 Arthur Sonzogni. All rights reserved.
|
||||||
// Use of this source code is governed by the MIT license that can be found in
|
// Use of this source code is governed by the MIT license that can be found in
|
||||||
// the LICENSE file.
|
// the LICENSE file.
|
||||||
#include <algorithm> // for max
|
|
||||||
#include <ftxui/component/component_options.hpp> // for ResizableSplitOption
|
#include <ftxui/component/component_options.hpp> // for ResizableSplitOption
|
||||||
#include <ftxui/dom/direction.hpp> // for Direction, Direction::Down, Direction::Left, Direction::Right, Direction::Up
|
#include <ftxui/dom/direction.hpp> // for Direction, Direction::Down, Direction::Left, Direction::Right, Direction::Up
|
||||||
#include <ftxui/util/ref.hpp> // for Ref
|
#include <ftxui/util/ref.hpp> // for Ref
|
||||||
@@ -19,22 +18,34 @@
|
|||||||
namespace ftxui {
|
namespace ftxui {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
class ResizableSplitBase : public ComponentBase, public ResizableSplitOption {
|
class ResizableSplitBase : public ComponentBase {
|
||||||
public:
|
public:
|
||||||
explicit ResizableSplitBase(ResizableSplitOption options)
|
explicit ResizableSplitBase(ResizableSplitOption options)
|
||||||
: ResizableSplitOption(std::move(options)) {
|
: options_(std::move(options)) {
|
||||||
switch (direction()) {
|
switch (options_->direction()) {
|
||||||
case Direction::Left:
|
case Direction::Left:
|
||||||
Add(Container::Horizontal({main, back}));
|
Add(Container::Horizontal({
|
||||||
|
options_->main,
|
||||||
|
options_->back,
|
||||||
|
}));
|
||||||
break;
|
break;
|
||||||
case Direction::Right:
|
case Direction::Right:
|
||||||
Add(Container::Horizontal({back, main}));
|
Add(Container::Horizontal({
|
||||||
|
options_->back,
|
||||||
|
options_->main,
|
||||||
|
}));
|
||||||
break;
|
break;
|
||||||
case Direction::Up:
|
case Direction::Up:
|
||||||
Add(Container::Vertical({main, back}));
|
Add(Container::Vertical({
|
||||||
|
options_->main,
|
||||||
|
options_->back,
|
||||||
|
}));
|
||||||
break;
|
break;
|
||||||
case Direction::Down:
|
case Direction::Down:
|
||||||
Add(Container::Vertical({back, main}));
|
Add(Container::Vertical({
|
||||||
|
options_->back,
|
||||||
|
options_->main,
|
||||||
|
}));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,27 +75,27 @@ class ResizableSplitBase : public ComponentBase, public ResizableSplitOption {
|
|||||||
return ComponentBase::OnEvent(event);
|
return ComponentBase::OnEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (direction()) {
|
switch (options_->direction()) {
|
||||||
case Direction::Left:
|
case Direction::Left:
|
||||||
main_size() = std::max(0, event.mouse().x - box_.x_min);
|
options_->main_size() = std::max(0, event.mouse().x - box_.x_min);
|
||||||
break;
|
return true;
|
||||||
case Direction::Right:
|
case Direction::Right:
|
||||||
main_size() = std::max(0, box_.x_max - event.mouse().x);
|
options_->main_size() = std::max(0, box_.x_max - event.mouse().x);
|
||||||
break;
|
return true;
|
||||||
case Direction::Up:
|
case Direction::Up:
|
||||||
main_size() = std::max(0, event.mouse().y - box_.y_min);
|
options_->main_size() = std::max(0, event.mouse().y - box_.y_min);
|
||||||
break;
|
return true;
|
||||||
case Direction::Down:
|
case Direction::Down:
|
||||||
main_size() = std::max(0, box_.y_max - event.mouse().y);
|
options_->main_size() = std::max(0, box_.y_max - event.mouse().y);
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
main_size() = std::clamp(main_size(), min(), max());
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTREACHED()
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
Element OnRender() final {
|
Element OnRender() final {
|
||||||
switch (direction()) {
|
switch (options_->direction()) {
|
||||||
case Direction::Left:
|
case Direction::Left:
|
||||||
return RenderLeft();
|
return RenderLeft();
|
||||||
case Direction::Right:
|
case Direction::Right:
|
||||||
@@ -100,41 +111,46 @@ class ResizableSplitBase : public ComponentBase, public ResizableSplitOption {
|
|||||||
|
|
||||||
Element RenderLeft() {
|
Element RenderLeft() {
|
||||||
return hbox({
|
return hbox({
|
||||||
main->Render() | size(WIDTH, EQUAL, main_size()),
|
options_->main->Render() |
|
||||||
separator_func() | reflect(separator_box_),
|
size(WIDTH, EQUAL, options_->main_size()),
|
||||||
back->Render() | xflex,
|
options_->separator_func() | reflect(separator_box_),
|
||||||
|
options_->back->Render() | xflex,
|
||||||
}) |
|
}) |
|
||||||
reflect(box_);
|
reflect(box_);
|
||||||
}
|
}
|
||||||
|
|
||||||
Element RenderRight() {
|
Element RenderRight() {
|
||||||
return hbox({
|
return hbox({
|
||||||
back->Render() | xflex,
|
options_->back->Render() | xflex,
|
||||||
separator_func() | reflect(separator_box_),
|
options_->separator_func() | reflect(separator_box_),
|
||||||
main->Render() | size(WIDTH, EQUAL, main_size()),
|
options_->main->Render() |
|
||||||
|
size(WIDTH, EQUAL, options_->main_size()),
|
||||||
}) |
|
}) |
|
||||||
reflect(box_);
|
reflect(box_);
|
||||||
}
|
}
|
||||||
|
|
||||||
Element RenderTop() {
|
Element RenderTop() {
|
||||||
return vbox({
|
return vbox({
|
||||||
main->Render() | size(HEIGHT, EQUAL, main_size()),
|
options_->main->Render() |
|
||||||
separator_func() | reflect(separator_box_),
|
size(HEIGHT, EQUAL, options_->main_size()),
|
||||||
back->Render() | yflex,
|
options_->separator_func() | reflect(separator_box_),
|
||||||
|
options_->back->Render() | yflex,
|
||||||
}) |
|
}) |
|
||||||
reflect(box_);
|
reflect(box_);
|
||||||
}
|
}
|
||||||
|
|
||||||
Element RenderBottom() {
|
Element RenderBottom() {
|
||||||
return vbox({
|
return vbox({
|
||||||
back->Render() | yflex,
|
options_->back->Render() | yflex,
|
||||||
separator_func() | reflect(separator_box_),
|
options_->separator_func() | reflect(separator_box_),
|
||||||
main->Render() | size(HEIGHT, EQUAL, main_size()),
|
options_->main->Render() |
|
||||||
|
size(HEIGHT, EQUAL, options_->main_size()),
|
||||||
}) |
|
}) |
|
||||||
reflect(box_);
|
reflect(box_);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Ref<ResizableSplitOption> options_;
|
||||||
CapturedMouse captured_mouse_;
|
CapturedMouse captured_mouse_;
|
||||||
Box separator_box_;
|
Box separator_box_;
|
||||||
Box box_;
|
Box box_;
|
||||||
|
|||||||
@@ -233,105 +233,5 @@ TEST(ResizableSplit, NavigationVertical) {
|
|||||||
EXPECT_FALSE(component_bottom->Active());
|
EXPECT_FALSE(component_bottom->Active());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ResizableSplit, MinMaxSizeLeft) {
|
|
||||||
int position = 5;
|
|
||||||
auto component = ResizableSplit({
|
|
||||||
.main = BasicComponent(),
|
|
||||||
.back = BasicComponent(),
|
|
||||||
.direction = Direction::Left,
|
|
||||||
.main_size = &position,
|
|
||||||
.separator_func = [] { return separatorDouble(); },
|
|
||||||
.min = 3,
|
|
||||||
.max = 8,
|
|
||||||
});
|
|
||||||
auto screen = Screen(20, 20);
|
|
||||||
Render(screen, component->Render());
|
|
||||||
EXPECT_EQ(position, 5);
|
|
||||||
EXPECT_TRUE(component->OnEvent(MousePressed(5, 1)));
|
|
||||||
EXPECT_EQ(position, 5);
|
|
||||||
// Try to resize below min
|
|
||||||
EXPECT_TRUE(component->OnEvent(MousePressed(2, 1)));
|
|
||||||
EXPECT_EQ(position, 3); // Clamped to min
|
|
||||||
// Try to resize above max
|
|
||||||
EXPECT_TRUE(component->OnEvent(MousePressed(10, 1)));
|
|
||||||
EXPECT_EQ(position, 8); // Clamped to max
|
|
||||||
EXPECT_TRUE(component->OnEvent(MouseReleased(10, 1)));
|
|
||||||
EXPECT_EQ(position, 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(ResizableSplit, MinMaxSizeRight) {
|
|
||||||
int position = 5;
|
|
||||||
auto component = ResizableSplit({
|
|
||||||
.main = BasicComponent(),
|
|
||||||
.back = BasicComponent(),
|
|
||||||
.direction = Direction::Right,
|
|
||||||
.main_size = &position,
|
|
||||||
.separator_func = [] { return separatorDouble(); },
|
|
||||||
.min = 3,
|
|
||||||
.max = 8,
|
|
||||||
});
|
|
||||||
auto screen = Screen(20, 20);
|
|
||||||
Render(screen, component->Render());
|
|
||||||
EXPECT_EQ(position, 5);
|
|
||||||
EXPECT_TRUE(component->OnEvent(MousePressed(14, 1)));
|
|
||||||
EXPECT_EQ(position, 5);
|
|
||||||
// Try to resize below min
|
|
||||||
EXPECT_TRUE(component->OnEvent(MousePressed(18, 1)));
|
|
||||||
EXPECT_EQ(position, 3); // Clamped to min
|
|
||||||
// Try to resize above max
|
|
||||||
EXPECT_TRUE(component->OnEvent(MousePressed(10, 1)));
|
|
||||||
EXPECT_EQ(position, 8); // Clamped to max
|
|
||||||
EXPECT_TRUE(component->OnEvent(MouseReleased(10, 1)));
|
|
||||||
EXPECT_EQ(position, 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(ResizableSplit, MinMaxSizeTop) {
|
|
||||||
int position = 5;
|
|
||||||
auto component = ResizableSplit({
|
|
||||||
.main = BasicComponent(),
|
|
||||||
.back = BasicComponent(),
|
|
||||||
.direction = Direction::Up,
|
|
||||||
.main_size = &position,
|
|
||||||
.separator_func = [] { return separatorDouble(); },
|
|
||||||
.min = 2,
|
|
||||||
.max = 10,
|
|
||||||
});
|
|
||||||
auto screen = Screen(20, 20);
|
|
||||||
Render(screen, component->Render());
|
|
||||||
EXPECT_EQ(position, 5);
|
|
||||||
EXPECT_TRUE(component->OnEvent(MousePressed(1, 5)));
|
|
||||||
EXPECT_EQ(position, 5);
|
|
||||||
// Try to resize below min
|
|
||||||
EXPECT_TRUE(component->OnEvent(MousePressed(1, 1)));
|
|
||||||
EXPECT_EQ(position, 2); // Clamped to min
|
|
||||||
// Try to resize above max
|
|
||||||
EXPECT_TRUE(component->OnEvent(MousePressed(1, 15)));
|
|
||||||
EXPECT_EQ(position, 10); // Clamped to max
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(ResizableSplit, MinMaxSizeBottom) {
|
|
||||||
int position = 5;
|
|
||||||
auto component = ResizableSplit({
|
|
||||||
.main = BasicComponent(),
|
|
||||||
.back = BasicComponent(),
|
|
||||||
.direction = Direction::Down,
|
|
||||||
.main_size = &position,
|
|
||||||
.separator_func = [] { return separatorDouble(); },
|
|
||||||
.min = 3,
|
|
||||||
.max = 12,
|
|
||||||
});
|
|
||||||
auto screen = Screen(20, 20);
|
|
||||||
Render(screen, component->Render());
|
|
||||||
EXPECT_EQ(position, 5);
|
|
||||||
EXPECT_TRUE(component->OnEvent(MousePressed(1, 14)));
|
|
||||||
EXPECT_EQ(position, 5);
|
|
||||||
// Try to resize below min
|
|
||||||
EXPECT_TRUE(component->OnEvent(MousePressed(1, 18)));
|
|
||||||
EXPECT_EQ(position, 3); // Clamped to min
|
|
||||||
// Try to resize above max
|
|
||||||
EXPECT_TRUE(component->OnEvent(MousePressed(1, 5)));
|
|
||||||
EXPECT_EQ(position, 12); // Clamped to max
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ftxui
|
} // namespace ftxui
|
||||||
// NOLINTEND
|
// NOLINTEND
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ class TaskRunner {
|
|||||||
auto PostTask(Task task) -> void;
|
auto PostTask(Task task) -> void;
|
||||||
|
|
||||||
/// Schedules a task to be executed after a certain duration.
|
/// Schedules a task to be executed after a certain duration.
|
||||||
auto PostDelayedTask(Task task,
|
auto PostDelayedTask(Task task, std::chrono::steady_clock::duration duration)
|
||||||
std::chrono::steady_clock::duration duration) -> void;
|
-> void;
|
||||||
|
|
||||||
/// Runs the tasks in the queue, return the delay until the next delayed task
|
/// Runs the tasks in the queue, return the delay until the next delayed task
|
||||||
/// can be executed.
|
/// can be executed.
|
||||||
|
|||||||
@@ -1,232 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
import os
|
|
||||||
import subprocess
|
|
||||||
import shutil
|
|
||||||
import tempfile
|
|
||||||
import json
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import List, Dict
|
|
||||||
|
|
||||||
class VersionInfo:
|
|
||||||
"""A structure to hold all information about a single documentation version."""
|
|
||||||
def __init__(self, name: str, is_main: bool, output_root: Path):
|
|
||||||
self.name = name
|
|
||||||
self.is_main = is_main
|
|
||||||
# Destination directory for the built docs, relative to the output root.
|
|
||||||
self.dest_dir = output_root if is_main else output_root / "en" / name
|
|
||||||
# The path to this version's index.html, relative to the output root.
|
|
||||||
self.index_path_from_root = self.dest_dir / "index.html"
|
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
|
||||||
return f"VersionInfo(name='{self.name}', dest_dir='{self.dest_dir}')"
|
|
||||||
|
|
||||||
def run_command(command: List[str], check: bool = True, cwd: Path = None):
|
|
||||||
"""
|
|
||||||
Runs a command, prints its output, and handles errors.
|
|
||||||
"""
|
|
||||||
command_str = ' '.join(command)
|
|
||||||
print(f"Executing: {command_str} in {cwd or Path.cwd()}")
|
|
||||||
try:
|
|
||||||
# Using capture_output=True to get stdout/stderr
|
|
||||||
result = subprocess.run(
|
|
||||||
command,
|
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
check=check,
|
|
||||||
cwd=cwd
|
|
||||||
)
|
|
||||||
if result.stdout:
|
|
||||||
print(result.stdout)
|
|
||||||
if result.stderr:
|
|
||||||
print(result.stderr)
|
|
||||||
return result
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
print(f"ERROR: Command failed with exit code {e.returncode}")
|
|
||||||
print(f"Command: {command_str}")
|
|
||||||
if e.stdout:
|
|
||||||
print("--- STDOUT ---")
|
|
||||||
print(e.stdout)
|
|
||||||
if e.stderr:
|
|
||||||
print("--- STDERR ---")
|
|
||||||
print(e.stderr)
|
|
||||||
raise # Re-raise the exception to halt the script
|
|
||||||
|
|
||||||
def get_version_switcher_js(
|
|
||||||
current_version: VersionInfo,
|
|
||||||
all_versions: List[VersionInfo],
|
|
||||||
current_html_file: Path
|
|
||||||
) -> str:
|
|
||||||
"""
|
|
||||||
Generates the JavaScript for the version switcher dropdown.
|
|
||||||
|
|
||||||
This version pre-calculates the relative path from the current HTML file
|
|
||||||
to the index.html of every other version, simplifying the JS logic.
|
|
||||||
"""
|
|
||||||
version_names = [v.name for v in all_versions]
|
|
||||||
|
|
||||||
# Create a dictionary mapping version names to their relative URLs.
|
|
||||||
relative_paths: Dict[str, str] = {}
|
|
||||||
for version in all_versions:
|
|
||||||
# Calculate the relative path from the *parent directory* of the current HTML file
|
|
||||||
# to the target version's index.html.
|
|
||||||
path = os.path.relpath(version.index_path_from_root, current_html_file.parent)
|
|
||||||
relative_paths[version.name] = path
|
|
||||||
|
|
||||||
# Use json.dumps for safe serialization of data into JavaScript.
|
|
||||||
versions_json = json.dumps(version_names)
|
|
||||||
paths_json = json.dumps(relative_paths)
|
|
||||||
current_version_json = json.dumps(current_version.name)
|
|
||||||
|
|
||||||
return f"""
|
|
||||||
document.addEventListener('DOMContentLoaded', function() {{
|
|
||||||
const projectNumber = document.getElementById('projectnumber');
|
|
||||||
if (!projectNumber) {{
|
|
||||||
console.warn('Doxygen element with ID "projectnumber" not found. Cannot add version switcher.');
|
|
||||||
return;
|
|
||||||
}}
|
|
||||||
|
|
||||||
const versions = {versions_json};
|
|
||||||
const version_paths = {paths_json};
|
|
||||||
const currentVersion = {current_version_json};
|
|
||||||
|
|
||||||
// Sort versions: 'main' first, then others numerically descending.
|
|
||||||
versions.sort((a, b) => {{
|
|
||||||
if (a === 'main') return -1;
|
|
||||||
if (b === 'main') return 1;
|
|
||||||
return b.localeCompare(a, undefined, {{ numeric: true, sensitivity: 'base' }});
|
|
||||||
}});
|
|
||||||
|
|
||||||
const select = document.createElement('select');
|
|
||||||
select.onchange = function() {{
|
|
||||||
const selectedVersion = this.value;
|
|
||||||
// Navigate directly to the pre-calculated relative path.
|
|
||||||
if (selectedVersion !== currentVersion) {{
|
|
||||||
window.location.href = version_paths[selectedVersion];
|
|
||||||
}}
|
|
||||||
}};
|
|
||||||
|
|
||||||
versions.forEach(v => {{
|
|
||||||
const option = document.createElement('option');
|
|
||||||
option.value = v;
|
|
||||||
option.textContent = v;
|
|
||||||
if (v === currentVersion) {{
|
|
||||||
option.selected = true;
|
|
||||||
}}
|
|
||||||
select.appendChild(option);
|
|
||||||
}});
|
|
||||||
|
|
||||||
// Replace the Doxygen project number element with our dropdown.
|
|
||||||
projectNumber.replaceWith(select);
|
|
||||||
|
|
||||||
// Apply some styling to make it look good.
|
|
||||||
Object.assign(select.style, {{
|
|
||||||
backgroundColor: 'rgba(0, 0, 0, 0.8)',
|
|
||||||
color: 'white',
|
|
||||||
border: '1px solid rgba(255, 255, 255, 0.2)',
|
|
||||||
padding: '5px',
|
|
||||||
borderRadius: '5px',
|
|
||||||
fontSize: '14px',
|
|
||||||
fontFamily: 'inherit',
|
|
||||||
marginLeft: '10px',
|
|
||||||
cursor: 'pointer'
|
|
||||||
}});
|
|
||||||
}});
|
|
||||||
"""
|
|
||||||
|
|
||||||
def main():
|
|
||||||
"""Main function to build multi-version documentation."""
|
|
||||||
root_dir = Path.cwd()
|
|
||||||
output_dir = root_dir / "multiversion_docs"
|
|
||||||
|
|
||||||
print("--- 1. Cleaning up old documentation ---")
|
|
||||||
if output_dir.exists():
|
|
||||||
shutil.rmtree(output_dir)
|
|
||||||
output_dir.mkdir(parents=True, exist_ok=True)
|
|
||||||
|
|
||||||
print("--- 2. Getting versions from git ---")
|
|
||||||
git_tags_result = run_command(["git", "tag", "--list", "v*"])
|
|
||||||
# Create a list of version names, starting with 'main'.
|
|
||||||
version_names = ["main"] + sorted(
|
|
||||||
git_tags_result.stdout.splitlines(),
|
|
||||||
reverse=True
|
|
||||||
)
|
|
||||||
print(f"Versions to build: {', '.join(version_names)}")
|
|
||||||
|
|
||||||
# Pre-compute all version information and paths.
|
|
||||||
versions = [
|
|
||||||
VersionInfo(name, name == "main", output_dir)
|
|
||||||
for name in version_names
|
|
||||||
]
|
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as build_dir_str:
|
|
||||||
build_dir = Path(build_dir_str)
|
|
||||||
# --- 3. Build documentation for each version ---
|
|
||||||
for version in versions:
|
|
||||||
print(f"\n--- Building docs for version: {version.name} ---")
|
|
||||||
|
|
||||||
# Create a temporary directory for this version's source code.
|
|
||||||
version_src_dir = build_dir / f"src_{version.name}"
|
|
||||||
version_src_dir.mkdir()
|
|
||||||
|
|
||||||
# Check out the version's source code from git.
|
|
||||||
archive_path = version_src_dir / "source.tar"
|
|
||||||
run_command([
|
|
||||||
"git", "archive", version.name,
|
|
||||||
"--format=tar", f"--output={archive_path}"
|
|
||||||
])
|
|
||||||
run_command(["tar", "-xf", str(archive_path)], cwd=version_src_dir)
|
|
||||||
archive_path.unlink()
|
|
||||||
|
|
||||||
# Configure and build the docs using CMake.
|
|
||||||
version_build_dir = build_dir / f"build_{version.name}"
|
|
||||||
version_build_dir.mkdir()
|
|
||||||
run_command([
|
|
||||||
"cmake", str(version_src_dir), "-DFTXUI_BUILD_DOCS=ON"
|
|
||||||
], cwd=version_build_dir)
|
|
||||||
run_command(["make", "doc"], cwd=version_build_dir)
|
|
||||||
|
|
||||||
# Copy the generated HTML files to the final destination.
|
|
||||||
doxygen_html_dir = version_build_dir / "doc" / "doxygen" / "html"
|
|
||||||
if not doxygen_html_dir.is_dir():
|
|
||||||
print(f"FATAL: Doxygen HTML output not found for version {version.name}")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
print(f"Copying files to: {version.dest_dir}")
|
|
||||||
shutil.copytree(doxygen_html_dir, version.dest_dir, dirs_exist_ok=True)
|
|
||||||
|
|
||||||
# --- 4. Inject version switcher into all HTML files ---
|
|
||||||
print("\n--- Injecting version switcher JavaScript ---")
|
|
||||||
for version in versions:
|
|
||||||
if not version.dest_dir.exists():
|
|
||||||
print(f"Warning: Destination directory for {version.name} does not exist. Skipping JS injection.")
|
|
||||||
continue
|
|
||||||
|
|
||||||
print(f"Processing HTML files in: {version.dest_dir}")
|
|
||||||
|
|
||||||
html_files = []
|
|
||||||
if version.is_main:
|
|
||||||
# For the main version, find all HTML files, but explicitly exclude the 'en' directory.
|
|
||||||
html_files.extend(version.dest_dir.glob("*.html"))
|
|
||||||
for subdir in version.dest_dir.iterdir():
|
|
||||||
if subdir.is_dir() and subdir.name != 'en':
|
|
||||||
html_files.extend(subdir.rglob("*.html"))
|
|
||||||
else:
|
|
||||||
# For other versions, their directory is self-contained.
|
|
||||||
html_files = list(version.dest_dir.rglob("*.html"))
|
|
||||||
|
|
||||||
for html_file in html_files:
|
|
||||||
js_script = get_version_switcher_js(version, versions, html_file)
|
|
||||||
script_tag = f'<script>{js_script}</script>'
|
|
||||||
|
|
||||||
content = html_file.read_text(encoding='utf-8')
|
|
||||||
# Inject the script right before the closing body tag.
|
|
||||||
new_content = content.replace("</body>", f"{script_tag}\n</body>")
|
|
||||||
html_file.write_text(new_content, encoding='utf-8')
|
|
||||||
|
|
||||||
print("\n--- 5. Finalizing ---")
|
|
||||||
print("Multi-version documentation generated successfully!")
|
|
||||||
print(f"Output located in: {output_dir.resolve()}")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
Reference in New Issue
Block a user