Skip to content

Commit 5acc71e

Browse files
committed
Add basic-demo program, move src to weather-app
1 parent 9a3b29e commit 5acc71e

File tree

10 files changed

+60
-17
lines changed

10 files changed

+60
-17
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ install_manifest.txt
99
compile_commands.json
1010
CTestTestfile.cmake
1111
_deps
12-
cpp-tui-workshop
12+
*.out

CMakeLists.txt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ project(cpp-tui-workshop LANGUAGES CXX)
55
find_package(ftxui REQUIRED)
66
find_package(cpr REQUIRED)
77

8-
file(GLOB_RECURSE SRC_FILES "src/*.cpp")
9-
add_executable(cpp-tui-workshop ${SRC_FILES})
10-
target_include_directories(cpp-tui-workshop PRIVATE src)
8+
function(register_executable TARGET_NAME SRC_PATH LIBRARIES)
9+
file(GLOB_RECURSE SRC_FILES "${SRC_PATH}/*.cpp")
10+
add_executable("${TARGET_NAME}.out" ${SRC_FILES})
11+
target_link_libraries("${TARGET_NAME}.out" ${LIBRARIES})
12+
endfunction()
1113

12-
target_link_libraries(cpp-tui-workshop
13-
PRIVATE ftxui::screen
14-
PRIVATE ftxui::dom
15-
PRIVATE ftxui::component
16-
PRIVATE cpr::cpr
14+
register_executable(
15+
"weather-app"
16+
"weather-app"
17+
"ftxui::screen;ftxui::dom;ftxui::component;cpr::cpr"
18+
)
19+
20+
register_executable(
21+
"basic-demo"
22+
"basic-demo"
23+
"ftxui::screen;ftxui::dom;ftxui::component"
1724
)

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ using [CMake](https://cmake.org/).
2121
nix build .#
2222
```
2323

24+
This gives you a `result` folder containing the binaries in `result/bin`.
25+
26+
If you want to directly run the program without running the build command, you
27+
can do:
28+
29+
```sh
30+
nix run .#weather-app
31+
nix run .#basic-demo
32+
```
33+
2434
To run, you can either use `nix run` and skip building entirely or run
2535
`./result/bin/cpp-tui-workshop`.
2636

@@ -40,4 +50,4 @@ cmake .
4050
cmake --build .
4151
```
4252

43-
To run, you can run `./cpp-tui-workshop`.
53+
To run, you can run any of the `.out` files.

basic-demo/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <ftxui/component/captured_mouse.hpp>
2+
#include <ftxui/component/component.hpp>
3+
#include <ftxui/component/component_base.hpp>
4+
#include <ftxui/component/screen_interactive.hpp>
5+
#include <ftxui/dom/elements.hpp>
6+
7+
using namespace ftxui;
8+
9+
int main() {
10+
auto interface = [&] {
11+
return window(text("Title") | center, vbox({
12+
text("Hello!!") | center,
13+
}));
14+
};
15+
16+
auto screen = ScreenInteractive::Fullscreen();
17+
screen.Loop(Renderer(interface));
18+
}

flake.nix

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,20 @@
3434

3535
installPhase = ''
3636
mkdir -p $out/bin
37-
mv cpp-tui-workshop $out/bin
37+
for bin in *.out; do
38+
dst="$out/bin/$(basename $bin .out)"
39+
mv $bin $dst
40+
done
3841
'';
42+
};
3943

40-
meta = {
41-
mainProgram = "cpp-tui-workshop";
44+
apps = builtins.listToAttrs (map (exe: {
45+
name = exe;
46+
value = {
47+
type = "app";
48+
program = "${self.packages.${system}.default}/bin/${exe}";
4249
};
43-
};
50+
}) ["weather-app" "basic-demo"]);
4451
}
4552
);
4653
}
File renamed without changes.
File renamed without changes.

src/main.cpp renamed to weather-app/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,18 @@ int main() {
131131

132132
if (event == Event::Custom) {
133133
// Check if the weather data is ready.
134-
weather_future.wait();
135134
if (weather_future.valid()) {
136135
try {
137136
weather_maybe =
138137
std::make_unique<Weather::Conditions>(weather_future.get());
139138
} catch (...) {
140139
weather_maybe = std::current_exception();
141140
}
141+
return true;
142+
} else if (weather_maybe.index() == 0) {
143+
// Data is still loading. Try again later.
144+
screen.PostEvent(Event::Custom);
142145
}
143-
144-
return true;
145146
}
146147

147148
return false;
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)