146 Commits
v1.0 ... v1.7

Author SHA1 Message Date
Pranav
ec1973607d Merge pull request #32 from p-ranav/feature/dynamic-progress
Feature/dynamic progress
2020-02-22 00:53:39 +05:30
Pranav Srinivas Kumar
5049e70c50 Minor update 2020-02-22 00:53:13 +05:30
Pranav
5bc3183c12 Update README.md 2020-02-22 00:50:19 +05:30
Pranav
fcda546c9c Update README.md 2020-02-22 00:49:12 +05:30
Pranav Srinivas Kumar
f5c1e61676 Minor updates 2020-02-22 00:40:38 +05:30
Pranav Srinivas Kumar
c7b25e7c3a Added dynamic progress GIFs 2020-02-22 00:38:03 +05:30
Pranav Srinivas Kumar
5e75c285fe First pass implementation of dynamic progress bar 2020-02-21 22:40:16 +05:30
Pranav
7298c7140d Update README.md 2020-02-21 16:01:20 +05:30
Pranav
0ace816e52 Merge pull request #31 from madebr/cmake
Update cmake + add conan + fix msvc + add appveyor
2020-02-21 13:24:43 +05:30
Anonymous Maarten
91d51548fc Add appveyor script file 2020-02-21 00:36:05 +01:00
Anonymous Maarten
83da7c3469 Put NOMINMAX just before inclusion of windows.h header 2020-02-21 00:36:03 +01:00
Anonymous Maarten
b13ff53c26 Add conanfile 2020-02-20 23:54:12 +01:00
Anonymous Maarten
7c762ed146 Add indicatorsConfigVersion cmake support + create source packages 2020-02-20 23:40:41 +01:00
Pranav
3c969556c3 Merge pull request #30 from xbreak/pkgconfigfix
Fix indicators.pc.in if explicit includedir is set
2020-02-19 21:32:08 +05:30
xbreak
297790bcd7 Fix indicators.pc.in if explicit includedir is set
This fixes cases where both -DCMAKE_INSTALL_PREFIX and
-DCMAKE_INSTALL_INCLUDEDIR is set.
2020-02-19 14:43:58 +00:00
Pranav Srinivas Kumar
f9d3eb1bcc Minor updates 2020-02-13 14:22:52 +05:30
Pranav Srinivas Kumar
89ae8cec39 Clang format 2020-02-13 14:21:01 +05:30
Pranav Srinivas Kumar
f5b6b4d779 Added single header file v1.7 2020-02-13 14:20:40 +05:30
Pranav Srinivas Kumar
2b27d8f516 Minor update 2020-02-13 13:49:51 +05:30
Pranav Srinivas Kumar
77bc52b591 FIxed merge conflict 2020-02-11 17:30:24 +05:30
Pranav Srinivas Kumar
a6c55b6936 Updated enums to lowercase per the core guidelines. Bumped to wip v1.7 2020-02-11 17:29:41 +05:30
Pranav
e7b3d077f0 Update README.md 2020-02-11 17:23:23 +05:30
Pranav Srinivas Kumar
0588904b9d Closes #24 2020-02-11 16:31:31 +05:30
Pranav Srinivas Kumar
4442e3570f Minor update to sample 2020-02-11 13:45:22 +05:30
Pranav Srinivas Kumar
2698afb34b Bumped version to 1.6 2020-02-11 13:40:30 +05:30
Pranav
764d796f87 Merge pull request #21 from dawidpilarski/dev
API change - progress bar can be constructed with proper settings.
2020-02-11 13:38:14 +05:30
Dawid Pilarski
5c54b69e55 post review changes 2020-02-11 08:57:38 +01:00
Dawid Pilarski
186f1aafc7 What about license? 2020-02-10 23:15:09 +01:00
Dawid Pilarski
cfb1593806 fixed multi_block_progress_bar.cpp 2020-02-10 21:51:20 +01:00
Dawid Pilarski
4a207b8223 fixed multi_block_progress_bar.cpp 2020-02-10 21:48:36 +01:00
Dawid Pilarski
07734c412b Readme updated 2020-02-10 21:25:54 +01:00
Dawid Pilarski
d543453c0c code changes done 2020-02-10 20:54:20 +01:00
Pranav
e279bbb7e7 Merge pull request #28 from svgsponer/hotfix/remove-semicolon
Remove extra semicolon
2020-02-05 22:12:16 -06:00
Severin Gsponer
840743f312 Remove extra ;
Removes a extra ; that breaks build with gcc and `-Werror=pedantic`.
2020-02-06 02:18:55 +00:00
Dawid Pilarski
46c9983fab Progress spinner changed, default values approach changed, templates refactored 2020-02-04 23:36:44 +01:00
Pranav
b1c16d8881 Merge pull request #27 from xbreak/pkgconfig
Added pkg-config file
2020-02-04 14:23:09 -06:00
xbreak
06da3a97f4 Added pkg-config file 2020-02-04 20:07:57 +00:00
Pranav Srinivas Kumar
a2272eba8a Closes #26 2020-02-04 07:45:47 -06:00
pilarski
46bccdcc1c ProgressBar API change - make ProgressBar constructible with options 2020-02-03 23:48:21 +01:00
Dawid Pilarski
3c8975aa34 API change - progress bar can be constructed with proper settings. 2020-02-03 20:50:19 +01:00
Pranav
9a4729f577 Merge pull request #19 from p-ranav/feature/18
Closes #18
2020-01-30 08:27:08 -06:00
Pranav Srinivas Kumar
866c008492 Closes #18 2020-01-30 08:19:25 -06:00
Pranav
dcb7c656cc Merge pull request #17 from hyperxor/extract_progress_scale_writers
Extract progress scale writing logic into separate classes
2020-01-21 12:18:51 -06:00
hyperxor
cfcc9f3b4c small fixes 2020-01-21 20:43:10 +03:00
hyperxor
91ac917bbc fix passed parameters 2020-01-21 08:19:53 +03:00
hyperxor
0939406db4 Extract progress scale writing code into separate classes 2020-01-21 07:49:47 +03:00
Pranav
d4dea6db2a Merge pull request #16 from hyperxor/remove_redundant_code_in_print_duration_and_small_fixes
Remove code duplication: move _print_duration to details + small fixes
2020-01-19 13:06:20 -06:00
hyperxor
0dec563049 Remove code duplication: move _print_duration to details + small fixes 2020-01-19 21:53:43 +03:00
Pranav
4f9796e904 Merge pull request #15 from hyperxor/remove_redundant_code_when_set_stream_color
Remove code duplication: extract set_stream_color function
2020-01-19 07:27:32 -06:00
hyperxor
a91d3b6ff7 Remove code duplication: extract set_stream_color function 2020-01-19 16:20:01 +03:00
Pranav
1cba7fd321 Merge pull request #14 from hyperxor/small_code_improvement_in_locks
Small improvement: using lock_guard instead of unique_lock
2020-01-19 06:42:38 -06:00
hyperxor
b1f01d5e52 Small improvement: using lock_guard instead of unique_lock 2020-01-19 13:36:10 +03:00
Pranav
2291c8c39c Update README.md 2019-12-27 15:22:19 -06:00
Pranav Srinivas Kumar
e697a43fec Clang format 2019-12-18 12:47:10 -06:00
Pranav Srinivas Kumar
358e3763c3 Minor update 2019-12-18 12:46:08 -06:00
Pranav Srinivas Kumar
419737af61 Merge branch 'feature/multiprogress_api' 2019-12-18 11:59:33 -06:00
Pranav Srinivas Kumar
9786633eba Bumped version 2019-12-18 11:59:20 -06:00
Pranav Srinivas Kumar
502ce33af4 Updated README w/ new MultiProgress API 2019-12-18 11:57:50 -06:00
Pranav Srinivas Kumar
8190a1e513 Minor update 2019-12-18 11:57:07 -06:00
Pranav Srinivas Kumar
72644e5134 Setting multiprogress mode in MultiProgress constructor 2019-12-18 11:55:56 -06:00
Pranav Srinivas Kumar
222a8ae4f9 Added explicit constructor for MultiProgress to remove insert API 2019-12-18 11:51:58 -06:00
Pranav Srinivas Kumar
2694badc93 Added locking to .current() 2019-12-18 07:55:22 -06:00
Pranav
ba604b7dd6 Update README.md 2019-12-17 22:04:36 -06:00
Pranav
2853aba409 Update README.md 2019-12-17 22:03:28 -06:00
Pranav Srinivas Kumar
2719604ba9 Updated README 2019-12-17 22:02:40 -06:00
Pranav
a80e4af117 Update README.md 2019-12-17 21:59:29 -06:00
Pranav Srinivas Kumar
d862ccde37 Updated FID 2019-12-17 21:59:17 -06:00
Pranav Srinivas Kumar
63ea50103a Merge branch 'master' of github.com:p-ranav/indicators 2019-12-17 21:56:41 -06:00
Pranav Srinivas Kumar
d9aa064380 Updated GIF 2019-12-17 21:56:36 -06:00
Pranav
5449ad0629 Update README.md 2019-12-17 21:49:37 -06:00
Pranav
c5969eefe4 Update README.md 2019-12-17 21:41:44 -06:00
Pranav Srinivas Kumar
34813c8642 Added progress_bar.tick GIF 2019-12-17 21:40:05 -06:00
Pranav Srinivas Kumar
c2832a5412 Updated GIF 2019-12-17 21:36:13 -06:00
Pranav Srinivas Kumar
67663b9d7d Updated GIF 2019-12-17 21:33:34 -06:00
Pranav
45ad6da847 Update README.md 2019-12-17 21:33:22 -06:00
Pranav Srinivas Kumar
5eb8cc5f91 Merge branch 'master' of github.com:p-ranav/indicators 2019-12-17 21:30:49 -06:00
Pranav Srinivas Kumar
99a14b5563 Updated GIF 2019-12-17 21:30:37 -06:00
Pranav
3bc140f90f Update README.md 2019-12-17 21:26:55 -06:00
Pranav
3f21b1d901 Update README.md 2019-12-17 21:15:47 -06:00
Pranav
7eb31cf24b Update README.md 2019-12-17 21:12:13 -06:00
Pranav
b726255bfd Merge pull request #12 from p-ranav/feature/multi_progress
Feature/multi progress
2019-12-17 21:11:07 -06:00
Pranav Srinivas Kumar
082b7a69e9 Updated README GIF 2019-12-17 21:05:14 -06:00
Pranav Srinivas Kumar
39922dbf80 Updated README 2019-12-17 20:59:50 -06:00
Pranav Srinivas Kumar
4f0cd06969 Minor update 2019-12-17 20:57:28 -06:00
Pranav Srinivas Kumar
70f843ce01 Minor update to API 2019-12-17 20:56:17 -06:00
Pranav Srinivas Kumar
4e5e5cfa19 Added missing includes 2019-12-17 20:40:44 -06:00
Pranav
36e77a64e9 Update README.md 2019-12-17 20:36:39 -06:00
Pranav
b542bedf5b Update README.md 2019-12-17 20:28:18 -06:00
Pranav Srinivas Kumar
8e41f2712e Added sample showing MultiProgress with BlockProgressBar 2019-12-17 20:21:33 -06:00
Pranav Srinivas Kumar
03000c8493 Changed MultiProgress into a template container class 2019-12-17 20:14:05 -06:00
Pranav Srinivas Kumar
92725d6cb2 Draft implementation of MultiProgress for progress bars 2019-12-17 20:06:50 -06:00
Pranav Srinivas Kumar
01966b8239 First attempt at MultiProgress 2019-12-17 19:10:52 -06:00
Pranav
788f6c9e1e Update README.md 2019-12-17 15:41:42 -06:00
Pranav
936043b7d4 Update block_progress_bar.hpp 2019-12-17 15:24:05 -06:00
Pranav
c90f375952 Update progress_spinner.hpp 2019-12-17 15:23:45 -06:00
Pranav
2241c00bb6 Update block_progress_bar.hpp 2019-12-17 15:23:26 -06:00
Pranav
bc128814fc Update progress_bar.hpp 2019-12-17 15:23:11 -06:00
Pranav
5a556bf5e2 Update README.md 2019-12-17 11:35:04 -06:00
Pranav
b5c2e78a72 Merge pull request #11 from offa/travis_ci
Travis CI
2019-12-17 11:31:07 -06:00
offa
4b2635020f Build status added. 2019-12-17 18:25:16 +01:00
offa
5751a58477 Travis CI supported added. 2019-12-17 18:25:16 +01:00
Pranav
a528d0d683 Update README.md 2019-12-17 10:05:44 -06:00
Pranav
74674154f5 Update README.md 2019-12-17 10:01:41 -06:00
Pranav
c42ae6862e Merge pull request #10 from p-ranav/feature/time
Feature/time
2019-12-17 10:00:23 -06:00
Pranav Srinivas Kumar
1e45ef3530 Updated README 2019-12-17 09:59:15 -06:00
Pranav Srinivas Kumar
d5a1c9a440 Update README 2019-12-17 09:48:36 -06:00
Pranav Srinivas Kumar
8c37f2f1dc Update README 2019-12-17 09:44:50 -06:00
Pranav Srinivas Kumar
aa5dffa4e2 Time elapsed/remaining is hidden by default 2019-12-17 09:43:53 -06:00
Pranav
78b7abcfbd Update README.md 2019-12-17 09:37:11 -06:00
Pranav Srinivas Kumar
fdad265e99 Update README 2019-12-17 09:35:52 -06:00
Pranav Srinivas Kumar
f28ab68c60 Updated README 2019-12-17 09:33:08 -06:00
Pranav Srinivas Kumar
6224a46371 Updated samples/demos to show usage of hide/show time elapsed/remaining 2019-12-17 09:31:43 -06:00
Pranav Srinivas Kumar
8198d8a802 Added time elapsed/remaining meter for progress spinner 2019-12-17 09:20:10 -06:00
Pranav Srinivas Kumar
5360fec641 Added time elapsed/remaining meter for block progress bar 2019-12-17 09:13:48 -06:00
Pranav Srinivas Kumar
c770704697 Draft implementation of time elapsed/remaining meter 2019-12-17 09:06:46 -06:00
Pranav Srinivas Kumar
97c89284a9 Clang format 2019-12-17 09:06:36 -06:00
Pranav
24d92beaca Merge pull request #9 from offa/cmake_samples
CMake support for Demo and Samples
2019-12-16 11:58:08 -06:00
offa
fe38859715 CMake support for building the demo added. 2019-12-16 18:49:06 +01:00
offa
ab63e2c45b CMake support for building the samples added. 2019-12-16 18:45:19 +01:00
Pranav
9d951f5e82 Update README.md 2019-12-16 11:07:16 -06:00
Pranav Srinivas Kumar
36ec8bb4d2 Merge branch 'master' of github.com:p-ranav/indicators 2019-12-16 11:03:36 -06:00
Pranav Srinivas Kumar
094f25e012 Added progress bar samples 2019-12-16 11:03:31 -06:00
Pranav
e9feed8aa1 Update README.md 2019-12-16 11:00:38 -06:00
Pranav
2b67c02ba6 Update README.md 2019-12-16 10:53:53 -06:00
Pranav
4c9ed35080 Update README.md 2019-12-16 10:22:14 -06:00
Pranav Srinivas Kumar
5f35000071 Merge branch 'master' of github.com:p-ranav/indicators 2019-12-16 10:19:55 -06:00
Pranav Srinivas Kumar
fb2d9bddbe Added progress spinner sample 2019-12-16 10:19:47 -06:00
Pranav
e7d0135316 Update README.md 2019-12-16 09:48:23 -06:00
Pranav Srinivas Kumar
2edbccce9c Updated README 2019-12-16 09:31:10 -06:00
Pranav
e6ae6c5513 Update README.md 2019-12-16 09:29:19 -06:00
Pranav Srinivas Kumar
1c2e7d5bd1 Updated README 2019-12-16 09:28:55 -06:00
Pranav Srinivas Kumar
b6d0ec0ee0 Updated README 2019-12-16 09:26:49 -06:00
Pranav Srinivas Kumar
cb879b5eb1 Added block progress bar GIF 2019-12-16 09:24:22 -06:00
Pranav
25001ceb1f Merge pull request #8 from p-ranav/feature/block_progress_bar
Feature/block progress bar
2019-12-16 09:12:37 -06:00
Pranav Srinivas Kumar
21391f3ca0 Added sample for smooth block progress bar 2019-12-16 09:11:25 -06:00
Pranav Srinivas Kumar
55f9eb7c67 Updated smooth block progress bar update logic 2019-12-16 09:07:57 -06:00
Pranav Srinivas Kumar
a516957135 Draft implementation of smooth block progress bar 2019-12-16 08:48:42 -06:00
Pranav
56489bf37a Merge pull request #6 from myd7349/fix-headers-installation
Improve CMakeLists.txt
2019-12-14 07:00:42 -06:00
myd7349
8f903b9741 Improve CMakeLists.txt
- Fix headers installation;
- Handle Threads dependency automatically;
2019-12-14 18:21:40 +08:00
Pranav
a938eb8f4c Update progress_spinner.hpp 2019-12-12 15:15:01 -06:00
Pranav
9e088af612 Update progress_bar.hpp 2019-12-12 15:14:38 -06:00
Pranav Srinivas KumaR
a41c7c118e Closes #2 2019-12-05 19:55:28 -06:00
Pranav Srinivas KumaR
cde4dbd83c Added termcolor LICENSE 2019-12-05 19:49:32 -06:00
Pranav
6f69ca5246 Update README.md 2019-12-05 15:56:17 -06:00
Pranav
d3ef0b0209 Update README.md 2019-12-05 15:55:55 -06:00
Pranav
3012fa78cf Update README.md 2019-12-05 12:59:23 -06:00
39 changed files with 3980 additions and 490 deletions

16
.appveyor.yml Normal file
View File

@@ -0,0 +1,16 @@
environment:
CONAN_RUN_TESTS: True
matrix:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
PATH: C:\\Python37;C:\\Python37\Scripts;C:\Users\appveyor\AppData\Roaming\Python\Python37\Scripts;%PATH%
# - APPVEYOR_BUILD_WORKER_IMAGE: Ubuntu
# PATH: /home/appveyor/.local/bin:$PATH
# - APPVEYOR_BUILD_WORKER_IMAGE: macOS
install:
- python --version
- python -m pip install conan --upgrade --user
- conan user
build_script:
- conan create .

65
.travis.yml Normal file
View File

@@ -0,0 +1,65 @@
language: generic
dist: bionic
matrix:
include:
- env: CXX=g++-9 CC=gcc-9
addons:
apt:
packages:
- g++-9
sources:
- sourceline: 'ppa:ubuntu-toolchain-r/test'
- env: CXX=g++-8 CC=gcc-8
addons:
apt:
packages:
- g++-8
- env: CXX=g++-7 CC=gcc-7
addons:
apt:
packages:
- g++-7
- env: CXX=g++-6 CC=gcc-6
addons:
apt:
packages:
- g++-6
- env: CXX=g++-5 CC=gcc-5
addons:
apt:
packages:
- g++-5
- env: CXX=clang++-9 CC=clang-9
addons:
apt:
packages:
- clang-9
- libc++-9-dev
- libc++abi-9-dev
sources:
- sourceline: 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main'
key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
- env: CXX=clang++-8 CC=clang-8
addons:
apt:
packages:
- clang-8
- libc++-8-dev
- libc++abi-8-dev
- env: CXX=clang++-7 CC=clang-7
addons:
apt:
packages:
- clang-7
- libc++-7-dev
- libc++abi-7-dev
script:
- if [[ "$CXX" == clang* ]]; then export CXXFLAGS="-stdlib=libc++"; fi
- mkdir build && cd build
- cmake -DDEMO=ON -DSAMPLES=ON ..
- make

View File

@@ -1,20 +1,98 @@
cmake_minimum_required(VERSION 3.8)
project(indica VERSION 1.0.0 LANGUAGES CXX)
option(INDICA_BUILD_TESTS OFF)
if(DEFINED PROJECT_NAME)
set(INDICATORS_SUBPROJECT ON)
endif()
project(indicators VERSION 1.7.0 LANGUAGES CXX
HOMEPAGE_URL "https://github.com/p-ranav/indicators"
DESCRIPTION "Activity Indicators for Modern C++")
if(EXISTS "${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
include("${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
conan_basic_setup()
endif()
option(INDICATORS_BUILD_TESTS "Build indicators tests + enable CTest")
option(INDICATORS_SAMPLES "Build indicators samples")
option(INDICATORS_DEMO "Build indicators demo" OFF)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
add_library(indica INTERFACE)
add_library(indica::indica ALIAS indica)
find_package(Threads REQUIRED)
target_compile_features(indica INTERFACE cxx_std_11)
target_include_directories(indica INTERFACE
add_library(indicators INTERFACE)
add_library(indicators::indicators ALIAS indicators)
target_compile_features(indicators INTERFACE cxx_std_11)
target_include_directories(indicators INTERFACE
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)
target_link_libraries(indicators INTERFACE Threads::Threads)
install(TARGETS indica EXPORT indicaConfig)
install(EXPORT indicaConfig
NAMESPACE indica::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/indica)
install(FILES ${CMAKE_CURRENT_LIST_DIR}/include/indica.hpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/indica)
if(INDICATORS_DEMO)
add_subdirectory(demo)
endif()
if(INDICATORS_SAMPLES)
add_subdirectory(samples)
endif()
if(NOT INDICATORS_SUBPROJECT)
configure_package_config_file(indicatorsConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/indicatorsConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/indicators)
write_basic_package_version_file(indicatorsConfigVersion.cmake
COMPATIBILITY AnyNewerVersion)
configure_file(indicators.pc.in indicators.pc @ONLY)
install(TARGETS indicators EXPORT indicatorsTargets)
install(EXPORT indicatorsTargets
FILE indicatorsTargets.cmake
NAMESPACE indicators::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/indicators)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/indicatorsConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/indicatorsConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/indicators)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/indicators.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/indicators
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
USE_SOURCE_PERMISSIONS
PATTERN "*.hpp")
install(FILES LICENSE LICENSE.termcolor
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/licenses/indicators)
if(EXISTS "${PROJECT_SOURCE_DIR}/.gitignore")
# Simple glob to regex conversion (.gitignore => CPACK_SOURCE_IGNORE_FILES)
file(READ ".gitignore" DOT_GITIGNORE)
string(REPLACE ";" "RANDOMSEQUENCE" DOT_GITIGNORE "${DOT_GITIGNORE}")
string(REPLACE "\n" ";" DOT_GITIGNORE "${DOT_GITIGNORE}")
string(REPLACE "RANDOMSEQUENCE" "\\;" DOT_GITIGNORE "${DOT_GITIGNORE}")
foreach(IGNORE_LINE ${DOT_GITIGNORE})
if(NOT IGNORE_LINE OR IGNORE_LINE MATCHES "^#")
continue()
endif()
string(REPLACE "\\" "\\\\" IGNORE_LINE "${IGNORE_LINE}")
string(REPLACE "." "\\\\." IGNORE_LINE "${IGNORE_LINE}")
string(REPLACE "*" ".*" IGNORE_LINE "${IGNORE_LINE}")
string(REPLACE "+" "\\\\+" IGNORE_LINE "${IGNORE_LINE}")
list(APPEND CPACK_SOURCE_IGNORE_FILES "${IGNORE_LINE}")
endforeach()
endif()
# extra ignored files
list(APPEND CPACK_SOURCE_IGNORE_FILES
.editorconfig
.git
.gitignore
.travis.yml
.appveyor.yml
)
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${PROJECT_VERSION}")
set(CPACK_GENERATOR "TGZ;TXZ")
set(CPACK_SOURCE_GENERATOR "TGZ;TXZ")
include(CPack)
endif()

31
LICENSE.termcolor Normal file
View File

@@ -0,0 +1,31 @@
Copyright (c) 2013, Ihor Kalnytskyi.
All rights reserved.
Redistribution and use in source and binary forms of the software as well
as documentation, with or without modification, are permitted provided
that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* The names of the contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

587
README.md
View File

@@ -3,10 +3,16 @@
</p>
<p align="center">
<a href="https://travis-ci.com/p-ranav/indicators">
<img src="https://travis-ci.com/p-ranav/indicators.svg?branch=master" alt="ci status"/>
</a>
<a href="https://www.codacy.com/manual/p-ranav/indicators?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=p-ranav/indicators&amp;utm_campaign=Badge_Grade">
<img src="https://api.codacy.com/project/badge/Grade/93401e73f250407cb32445afec4e3e99" alt="codacy"/>
</a>
<a href="https://github.com/p-ranav/indicators/blob/master/LICENSE">
<img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="license"/>
</a>
<img src="https://img.shields.io/badge/version-1.0-blue.svg?cacheSeconds=2592000" alt="version"/>
<img src="https://img.shields.io/badge/version-1.7-blue.svg?cacheSeconds=2592000" alt="version"/>
</p>
<p align="center">
@@ -15,56 +21,39 @@
# Highlights
* Thread-safe `ProgressBar` and `ProgressSpinner` classes
* Thread-safe progress bars and spinners
* Header-only library. Grab a copy of `include/indicators`
* Source for the above GIF can be found [here](demo/demo.cpp)
* MIT License
```bash
git clone https://github.com/p-ranav/indicators
cd indicators
mkdir build && cd build
cmake -DINDICATORS_SAMPLES=ON -DINDICATORS_DEMO=ON ..
make
```
# Table of Contents
* [Progress Bar](#progress-bar)
* [Block Progress Bar](#block-progress-bar)
* [Multi Progress](#multiprogress)
* [Dynamic Progress](#dynamicprogress)
* [Progress Spinner](#progress-spinner)
* [Contributing](#contributing)
* [License](#license)
# Progress bar
To introduce a progress bar in your application, include `indicators/progress_bar.hpp` and create a `ProgressBar` object.
```cpp
#include <indicators/progress_bar.hpp>
int main() {
indicators::ProgressBar bar;
return 0;
}
```
Here's the general structure of a progress bar:
To introduce a progress bar in your application, include `indicators/progress_bar.hpp` and create a `ProgressBar` object. Here's the general structure of a progress bar:
```
<prefix_text> <bar_start> <fill> <lead> <remaining> <bar_end> <progress_percentage>? <postfix_text>
^^^^^^^^^^^^^^^^^^ Bar Width ^^^^^^^^^^^^^^^^^^ ^^^^^ Show/Hide ^^^^^
{prefix} {start} {fill} {lead} {remaining} {end} {percentage} [{elapsed}<{remaining}] {postfix}
^^^^^^^^^^^^^ Bar Width ^^^^^^^^^^^^^^^
```
Each of these elements (and more) can be configured using the ProgressBar API. Here's an example configuration:
```cpp
#include <indicators/progress_bar.hpp>
int main() {
indicators::ProgressBar bar;
// Configure the bar
bar.set_bar_width(50);
bar.start_bar_with("[");
bar.fill_bar_progress_with("=");
bar.lead_bar_progress_with(">");
bar.fill_bar_remainder_with(" ");
bar.end_bar_with("]");
bar.set_postfix_text("Getting started");
bar.set_foreground_color(indicators::Color::GREEN);
// Update bar state
return 0;
}
```
Now that the bar is configured, let's update the state of the bar. The amount of progress in ProgressBar is maintained as a float in range `[0, 100]`. When progress reaches 100, the progression is complete.
The amount of progress in ProgressBar is maintained as a float in range `[0, 100]`. When progress reaches 100, the progression is complete.
From application-level code, there are two ways in which you can update this progress:
@@ -72,23 +61,27 @@ From application-level code, there are two ways in which you can update this pro
You can update the progress bar using `bar.tick()` which increments progress by exactly `1%`.
<p align="center">
<img src="img/progress_bar_tick.gif"/>
</p>
```cpp
#include <indicators/progress_bar.hpp>
#include <thread>
#include <chrono>
int main() {
indicators::ProgressBar bar;
// Configure the bar
bar.set_bar_width(50);
bar.start_bar_with("[");
bar.fill_bar_progress_with("=");
bar.lead_bar_progress_with(">");
bar.fill_bar_remainder_with(" ");
bar.end_bar_with("]");
bar.set_postfix_text("Getting started");
bar.set_foreground_color(indicators::Color::GREEN);
using namespace indicators;
ProgressBar bar{
option::BarWidth{50},
option::Start{"["},
option::Fill{"="},
option::Lead{">"},
option::Remainder{" "},
option::End{"]"},
option::PostfixText{"Extracting Archive"},
option::ForegroundColor{Color::green}
};
// Update bar state
while (true) {
@@ -108,170 +101,444 @@ The above code will print a progress bar that goes from 0 to 100% at the rate of
If you'd rather control progress of the bar in discrete steps, consider using `bar.set_progress(value)`. Example:
<p align="center">
<img src="img/progress_bar_set_progress.gif"/>
</p>
```cpp
#include <chrono>
#include <indicators/progress_bar.hpp>
#include <thread>
int main() {
// Hide cursor
std::cout << "\e[?25l";
using namespace indicators;
ProgressBar bar{
option::BarWidth{50},
option::Start{"["},
option::Fill{""},
option::Lead{""},
option::Remainder{"-"},
option::End{" ]"},
option::PostfixText{"Loading dependency 1/4"},
option::ForegroundColor{Color::cyan}
};
// Update bar state
bar.set_progress(10); // 10% done
// do some work
std::this_thread::sleep_for(std::chrono::milliseconds(800));
bar.set_option(option::PostfixText{"Loading dependency 2/4"});
bar.set_progress(30); // 30% done
// do some more work
std::this_thread::sleep_for(std::chrono::milliseconds(700));
bar.set_option(option::PostfixText{"Loading dependency 3/4"});
bar.set_progress(65); // 65% done
// do final bit of work
std::this_thread::sleep_for(std::chrono::milliseconds(900));
bar.set_option(option::PostfixText{"Loaded dependencies!"});
bar.set_progress(100); // all done
bar.mark_as_completed();
// Show cursor
std::cout << "\e[?25h";
return 0;
}
```
## Showing Time Elapsed/Remaining
All progress bars and spinners in `indicators` support showing time elapsed and time remaining. Inspired by python's [tqdm](https://github.com/tqdm/tqdm) module, the format of this meter is `[{elapsed}<{remaining}]`:
<p align="center">
<img src="img/time_meter.gif"/>
</p>
```cpp
#include <chrono>
#include <indicators/progress_bar.hpp>
#include <thread>
int main() {
using namespace indicators;
indicators::ProgressBar bar{
option::BarWidth{50},
option::Start{" ["},
option::Fill{""},
option::Lead{""},
option::Remainder{"-"},
option::End{"]"},
option::PrefixText{"Training Gaze Network 👀"},
option::ForegroundColor{Color::yellow},
option::ShowElapsedTime{true},
option::ShowRemainingTime{true}
};
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
// Show cursor
std::cout << "\e[?25h";
return 0;
}
```
# Block Progress Bar
Are you in need of a smooth block progress bar using [unicode block elements](https://en.wikipedia.org/wiki/Block_Elements)? Use `BlockProgressBar` instead of `ProgressBar`. Thanks to [this blog post](https://mike42.me/blog/2018-06-make-better-cli-progress-bars-with-unicode-block-characters) for making `BlockProgressBar` an easy addition to the library.
<p align="center">
<img src="img/block_progress_bar.gif"/>
</p>
```cpp
#include <indicators/block_progress_bar.hpp>
#include <thread>
#include <chrono>
int main() {
indicators::ProgressBar bar;
// Configure the bar
bar.set_bar_width(50);
bar.start_bar_with("[");
bar.fill_bar_progress_with("=");
bar.lead_bar_progress_with(">");
bar.fill_bar_remainder_with(" ");
bar.end_bar_with("]");
bar.set_postfix_text("Getting started");
bar.set_foreground_color(indicators::Color::GREEN);
// Hide cursor
std::cout << "\e[?25l";
using namespace indicators;
BlockProgressBar bar{
option::BarWidth{80},
option::Start{"["},
option::End{"]"},
option::ForegroundColor{Color::white}
};
// Update bar state
size_t i = 0;
while (i < 101) {
bar.set_progress(i);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
i += 10;
auto progress = 0.0f;
while (true) {
bar.set_progress(progress);
progress += 0.25f;
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
// Show cursor
std::cout << "\e[?25h";
return 0;
}
```
The above code will print a progress bar that goes from 0 to 100% at the rate of 10% every 100 ms.
# MultiProgress
## Multi-threaded Example
`indicators` supports management of multiple progress bars with the `MultiProgress` class template.
`template <typename Indicator, size_t count> class MultiProgress` is a class template that holds references to multiple progress bars and provides a safe interface to update the state of each bar. `MultiProgress` works with both `ProgressBar` and `BlockProgressBar` classes.
Below is an example `MultiProgress` object that manages three `ProgressBar` objects.
<p align="center">
<img src="img/multi_progress.gif"/>
</p>
```cpp
#include <indicators/multi_progress.hpp>
#include <indicators/progress_bar.hpp>
#include <vector>
int main() {
using namespace indicators;
// Configure first progress bar
ProgressBar bar1{
option::BarWidth{50},
option::Start{"["},
option::Fill{""},
option::Lead{""},
option::Remainder{" "},
option::End{" ]"},
option::ForegroundColor{Color::yellow},
option::ShowElapsedTime{true},
option::ShowRemainingTime{true},
option::PrefixText{"Progress Bar #1 "}
};
indicators::ProgressBar bar;
bar.set_bar_width(50);
bar.start_bar_with("[");
bar.fill_bar_progress_with("");
bar.lead_bar_progress_with("");
bar.fill_bar_remainder_with("-");
bar.end_bar_with("]");
bar.set_foreground_color(indicators::Color::YELLOW);
// Configure second progress bar
// As configured, the bar will look like this:
//
// [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-------------] 70%
//
//
std::atomic<size_t> index{0};
std::vector<std::string> status_text =
{
"Rocket.exe is not responding",
"Finding a replacement engineer",
"Buying more snacks",
"Assimilating the modding community",
"Crossing fingers",
"Porting KSP to a Nokia 3310"
ProgressBar bar2{
option::BarWidth{50},
option::Start{"["},
option::Fill{"="},
option::Lead{">"},
option::Remainder{" "},
option::End{" ]"},
option::ForegroundColor{Color::cyan},
option::ShowElapsedTime{true},
option::ShowRemainingTime{true},
option::PrefixText{"Progress Bar #2 "}
};
// Let's say you want to append some status text to the right of the progress bar
// You can use bar.set_postfix_text(...) to append text to the right
//
// [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-------------] 70% Finding a replacement engineer
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
//
// Configure third progress bar
indicators::ProgressBar bar3{
option::BarWidth{50},
option::Start{"["},
option::Fill{"#"},
option::Lead{"#"},
option::Remainder{" "},
option::End{" ]"},
option::ForegroundColor{Color::red},
option::ShowElapsedTime{true},
option::ShowRemainingTime{true},
option::PrefixText{"Progress Bar #3 "}
};
auto job = [&bar, &index, &status_text]() {
// Construct MultiProgress object
indicators::MultiProgress<indicators::ProgressBar, 3> bars(bar1, bar2, bar3);
std::cout << "Multiple Progress Bars:\n";
auto job1 = [&bars]() {
while (true) {
if (bar.is_completed()) {
bars.tick<0>();
if (bars.is_completed<0>())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
};
auto job2 = [&bars]() {
while (true) {
bars.tick<1>();
if (bars.is_completed<1>())
break;
}
bar.set_postfix_text(status_text[index % status_text.size()]);
bar.tick();
index += 1;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
};
std::thread first_job(job);
std::thread second_job(job);
std::thread third_job(job);
std::thread last_job(job);
auto job3 = [&bars]() {
while (true) {
bars.tick<2>();
if (bars.is_completed<2>())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(60));
}
};
std::thread first_job(job1);
std::thread second_job(job2);
std::thread third_job(job3);
first_job.join();
second_job.join();
third_job.join();
last_job.join();
return 0;
}
```
For more examples, checkout the examples in the `samples/` directory.
# DynamicProgress
`DynamicProgress` is a container class, similar to `MultiProgress`, for managing multiple progress bars. As the name suggests, with `DynamicProgress`, you can dynamically add new progress bars. Simply call `bars.push_back`.
Below is an example `DynamicProgress` object that manages six `ProgressBar` objects. Three of these bars are added dynamically.
<p align="center">
<img src="img/dynamic_progress_bar.gif"/>
</p>
```cpp
#include <indicators/dynamic_progress.hpp>
#include <indicators/progress_bar.hpp>
using namespace indicators;
int main() {
ProgressBar bar1{option::BarWidth{50}, option::ForegroundColor{Color::red},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"5c90d4a2d1a8: Downloading "}};
ProgressBar bar2{option::BarWidth{50}, option::ForegroundColor{Color::yellow},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"22337bfd13a9: Downloading "}};
ProgressBar bar3{option::BarWidth{50}, option::ForegroundColor{Color::green},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"10f26c680a34: Downloading "}};
ProgressBar bar4{option::BarWidth{50}, option::ForegroundColor{Color::white},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"6364e0d7a283: Downloading "}};
ProgressBar bar5{option::BarWidth{50}, option::ForegroundColor{Color::blue},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"ff1356ba118b: Downloading "}};
ProgressBar bar6{option::BarWidth{50}, option::ForegroundColor{Color::cyan},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"5a17453338b4: Downloading "}};
std::cout << termcolor::bold << termcolor::white << "Pulling image foo:bar/baz\n";
// Construct with 3 progress bars. We'll add 3 more at a later point
DynamicProgress<ProgressBar> bars(bar1, bar2, bar3);
// Do not hide bars when completed
bars.set_option(option::HideBarWhenComplete{false});
std::thread fourth_job, fifth_job, sixth_job;
auto job4 = [&bars](size_t i) {
while (true) {
bars[i].tick();
if (bars[i].is_completed()) {
bars[i].set_option(option::PrefixText{"6364e0d7a283: Pull complete "});
bars[i].mark_as_completed();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
};
auto job5 = [&bars](size_t i) {
while (true) {
bars[i].tick();
if (bars[i].is_completed()) {
bars[i].set_option(option::PrefixText{"ff1356ba118b: Pull complete "});
bars[i].mark_as_completed();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
};
auto job6 = [&bars](size_t i) {
while (true) {
bars[i].tick();
if (bars[i].is_completed()) {
bars[i].set_option(option::PrefixText{"5a17453338b4: Pull complete "});
bars[i].mark_as_completed();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(40));
}
};
auto job1 = [&bars, &bar6, &sixth_job, &job6]() {
while (true) {
bars[0].tick();
if (bars[0].is_completed()) {
bars[0].set_option(option::PrefixText{"5c90d4a2d1a8: Pull complete "});
// bar1 is completed, adding bar6
auto i = bars.push_back(bar6);
sixth_job = std::thread(job6, i);
sixth_job.join();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(140));
}
};
auto job2 = [&bars, &bar5, &fifth_job, &job5]() {
while (true) {
bars[1].tick();
if (bars[1].is_completed()) {
bars[1].set_option(option::PrefixText{"22337bfd13a9: Pull complete "});
// bar2 is completed, adding bar5
auto i = bars.push_back(bar5);
fifth_job = std::thread(job5, i);
fifth_job.join();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(25));
}
};
auto job3 = [&bars, &bar4, &fourth_job, &job4]() {
while (true) {
bars[2].tick();
if (bars[2].is_completed()) {
bars[2].set_option(option::PrefixText{"10f26c680a34: Pull complete "});
// bar3 is completed, adding bar4
auto i = bars.push_back(bar4);
fourth_job = std::thread(job4, i);
fourth_job.join();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
};
std::thread first_job(job1);
std::thread second_job(job2);
std::thread third_job(job3);
third_job.join();
second_job.join();
first_job.join();
std::cout << termcolor::bold << termcolor::green << "✔ Downloaded image foo/bar:baz" << std::endl;
std::cout << termcolor::reset;
return 0;
}
```
In the above code, notice the option `bars.set_option(option::HideBarWhenComplete{true});`. Yes, you can hide progress bars as and when they complete by setting this option to `true`. If you do so, the above example will look like this:
<p align="center">
<img src="img/dynamic_progress_bar_hide_completed.gif"/>
</p>
# Progress Spinner
To introduce a progress spinner in your application, include `indicators/progress_spinner.hpp` and create a `ProgressSpinner` object.
```cpp
#include <indicators/progress_spinner.hpp>
int main() {
indicators::ProgressSpinner spinner;
return 0;
}
```
Here's the general structure of a progress spinner:
To introduce a progress spinner in your application, include `indicators/progress_spinner.hpp` and create a `ProgressSpinner` object. Here's the general structure of a progress spinner:
```
<prefix_text> <spinner> <progress_percentage>? <postfix_text>
```
Each of these elements (and more) can be configured using the ProgressSpinner API. Here's an example configuration:
```cpp
#include <indicators/progress_spinner.hpp>
int main() {
indicators::ProgressSpinner spinner;
// Configure the spinner
spinner.set_prefix_text(" ");
spinner.set_postfix_text("Checking credentials");
spinner.set_foreground_color(indicators::Color::YELLOW);
spinner.set_spinner_states({"", "", "", "", "", "", "", ""});
// Update spinner state
return 0;
}
{prefix} {spinner} {percentage} [{elapsed}<{remaining}] {postfix}
```
ProgressSpinner has a vector of strings: `spinner_states`. At each update, the spinner will pick the next string from this sequence to print to the console. The spinner state can be updated similarly to ProgressBars: Using either `tick()` or `set_progress(value)`.
<p align="center">
<img src="img/progress_spinner.gif"/>
</p>
```cpp
#include <indicators/progress_spinner.hpp>
int main() {
indicators::ProgressSpinner spinner;
// Configure the spinner
spinner.set_postfix_text("Checking credentials");
spinner.set_foreground_color(indicators::Color::YELLOW);
spinner.set_spinner_states({"", "", "", "", "", "", "", ""});
using namespace indicators;
indicators::ProgressSpinner spinner{
option::PostfixText{"Checking credentials"},
option::ForegroundColor{Color::yellow},
option::SpinnerStates{std::vector<std::string>{"", "", "", "", "", "", "", ""}}
};
// Update spinner state
auto job = [&spinner]() {
while (true) {
if (spinner.is_completed()) {
spinner.set_foreground_color(indicators::Color::GREEN);
spinner.set_prefix_text("");
spinner.hide_spinner();
spinner.hide_percentage();
spinner.set_postfix_text("Authenticated!");
spinner.mark_as_completed();
spinner.set_option(option::ForegroundColor{Color::green});
spinner.set_option(option::PrefixText{""});
spinner.set_option(option::ShowSpinner{false});
spinner.set_option(option::ShowPercentage{false});
spinner.set_option(option::PostfixText{"Authenticated!"});
spinner.mark_as_completed();
break;
} else
spinner.tick();

View File

@@ -1,2 +1,2 @@
#!/usr/bin/env bash
find ./include ./demo/ -type f \( -iname \*.cpp -o -iname \*.hpp \) | xargs clang-format -style="{ColumnLimit : 100}" -i
find ./include ./demo/ ./samples/ ./single_include -type f \( -iname \*.cpp -o -iname \*.hpp \) | xargs clang-format -style="{ColumnLimit : 100}" -i

78
conanfile.py Normal file
View File

@@ -0,0 +1,78 @@
from conans import CMake, ConanFile, tools
from conans.errors import ConanException
from contextlib import contextmanager
import os
class IndicatorsConan(ConanFile):
name = "indicators"
description = "Activity Indicators for Modern C++"
topics = ("indicators", "progress", "activity", "indicator", "header-only", "c++11")
homepage = "https://github.com/p-ranav/indicators"
url = "https://github.com/p-ranav/indicators"
license = ("MIT", "BSD-3-Clause")
exports_sources = "demo/**", "include/**", "samples/**", "CMakeLists.txt", "LICENSE*", \
"indicators.pc.in", "indicatorsConfig.cmake.in", "README*", "img/**"
exports = "LICENSE"
no_copy_source = True
settings = "os", "arch", "compiler", "build_type"
generators = "cmake"
def set_version(self):
import re
m = re.search(r"project\(.*VERSION ([0-9a-zA-Z.-]+)[ )]",
open(os.path.join(self.recipe_folder, "CMakeLists.txt")).read())
if not m:
raise ConanException("Could not extract version from CMakeLists.txt")
self.version = m.group(1)
_cmake = None
def _configure_cmake(self):
if self._cmake:
return self._cmake
generator = None
if self.settings.compiler == "Visual Studio":
generator = "NMake Makefiles"
self._cmake = CMake(self, generator=generator)
if tools.get_env("CONAN_RUN_TESTS", default=False):
self._cmake.definitions["INDICATORS_DEMO"] = True
self._cmake.definitions["INDICATORS_SAMPLES"] = True
self._cmake.configure()
return self._cmake
@property
def _test_programs(self):
programs = []
import re
for subdir in ("demo", "samples", ):
for match in re.finditer(r"add_executable\((\S+)",
open(os.path.join(self.source_folder, subdir, "CMakeLists.txt")).read()):
programs.append(os.path.join(self.build_folder, "bin", match.group(1)))
return programs
@contextmanager
def _build_context(self):
with tools.vcvars(self.settings) if self.settings.compiler == "Visual Studio" else tools.no_op():
yield
def build(self):
with self._build_context():
cmake = self._configure_cmake()
cmake.build()
cmake.build(target="package_source")
# if tools.get_env("CONAN_RUN_TESTS", default=False):
# for program in self._test_programs:
# self.output.info("Running program '{}'".format(program))
# self.run(program, run_environment=True)
def package(self):
with self._build_context():
cmake = self._configure_cmake()
cmake.install()
def package_id(self):
self.info.header_only()
def package_info(self):
self.cpp_info.includedirs.append(os.path.join("include", "indicators"))

2
demo/CMakeLists.txt Normal file
View File

@@ -0,0 +1,2 @@
add_executable(demo demo.cpp)
target_link_libraries(demo PRIVATE indicators::indicators)

View File

@@ -6,19 +6,18 @@ int main() {
// Hide cursor
std::cout << "\e[?25l";
using namespace indicators;
{
//
// PROGRESS BAR 1
//
indicators::ProgressBar p;
p.set_bar_width(50);
p.start_bar_with("[");
p.fill_bar_progress_with("");
p.lead_bar_progress_with("");
p.fill_bar_remainder_with(" ");
p.end_bar_with(" ]");
p.set_foreground_color(indicators::Color::YELLOW);
indicators::ProgressBar p{option::BarWidth{50},
option::Start{"["},
option::Fill{""},
option::Lead{""},
option::Remainder{" "},
option::End{" ]"},
option::ForegroundColor{indicators::Color::yellow}};
std::atomic<size_t> index{0};
std::vector<std::string> status_text = {"Rocket.exe is not responding",
@@ -35,7 +34,7 @@ int main() {
while (true) {
if (p.is_completed())
break;
p.set_postfix_text(status_text[index % status_text.size()]);
p.set_option(option::PostfixText{status_text[index % status_text.size()]});
p.set_progress(index * 10);
index += 1;
std::this_thread::sleep_for(std::chrono::milliseconds(600));
@@ -50,20 +49,21 @@ int main() {
// PROGRESS BAR 2
//
indicators::ProgressBar p;
p.set_bar_width(40);
p.set_prefix_text("Reading package list... ");
p.start_bar_with("");
p.fill_bar_progress_with("");
p.lead_bar_progress_with("");
p.fill_bar_remainder_with("");
p.end_bar_with("");
p.set_foreground_color(indicators::Color::WHITE);
p.hide_percentage();
p.set_option(option::BarWidth{40});
p.set_option(option::PrefixText{"Reading package list... "});
p.set_option(option::Start{""});
p.set_option(option::Fill{""});
p.set_option(option::Lead{""});
p.set_option(option::Remainder{""});
p.set_option(option::End{""});
p.set_option(option::ForegroundColor{indicators::Color::white});
p.set_option(option::ShowPercentage{false});
auto job = [&p]() {
while (true) {
p.set_prefix_text("Reading package list... " + std::to_string(p.current()) + "% ");
p.set_option(
option::PrefixText{"Reading package list... " + std::to_string(p.current()) + "% "});
if (p.current() + 2 >= 100)
p.set_prefix_text("Reading package list... Done");
p.set_option(option::PrefixText{"Reading package list... Done"});
p.tick();
if (p.is_completed()) {
break;
@@ -80,25 +80,25 @@ int main() {
// PROGRESS BAR 3
//
indicators::ProgressBar p;
p.set_bar_width(50);
p.start_bar_with("[");
p.fill_bar_progress_with("=");
p.lead_bar_progress_with(">");
p.fill_bar_remainder_with(" ");
p.end_bar_with("]");
p.set_postfix_text("Getting started");
p.set_foreground_color(indicators::Color::GREEN);
p.set_option(option::BarWidth{50});
p.set_option(option::Start{"["});
p.set_option(option::Fill{"="});
p.set_option(option::Lead{">"});
p.set_option(option::Remainder{" "});
p.set_option(option::End{"]"});
p.set_option(option::PostfixText{"Getting started"});
p.set_option(option::ForegroundColor{indicators::Color::green});
auto job = [&p]() {
while (true) {
auto ticks = p.current();
if (ticks > 20 && ticks < 50)
p.set_postfix_text("Delaying the inevitable");
p.set_option(option::PostfixText{"Delaying the inevitable"});
else if (ticks > 50 && ticks < 80)
p.set_postfix_text("Crying quietly");
p.set_option(option::PostfixText{"Crying quietly"});
else if (ticks > 80 && ticks < 98)
p.set_postfix_text("Almost there");
p.set_option(option::PostfixText{"Almost there"});
else if (ticks >= 98)
p.set_postfix_text("Done");
p.set_option(option::PostfixText{"Done"});
p.tick();
if (p.is_completed())
break;
@@ -115,32 +115,31 @@ int main() {
//
std::vector<std::string> lead_spinner{"", "", "", "", "", "", "", "", "", ""};
indicators::ProgressBar p4;
p4.set_bar_width(40);
p4.start_bar_with("");
p4.fill_bar_progress_with("");
p4.lead_bar_progress_with("");
p4.fill_bar_remainder_with(" ");
p4.end_bar_with("");
p4.set_foreground_color(indicators::Color::CYAN);
p4.set_postfix_text("Restoring system state");
p4.hide_percentage();
p4.set_option(option::BarWidth{40});
p4.set_option(option::Start{""});
p4.set_option(option::Fill{""});
p4.set_option(option::Lead{""});
p4.set_option(option::Remainder{" "});
p4.set_option(option::End{""});
p4.set_option(option::ForegroundColor{indicators::Color::cyan});
p4.set_option(option::PostfixText{"Restoring system state"});
p4.set_option(option::ShowPercentage{false});
std::atomic<size_t> index4{0};
auto job4 = [&p4, &index4, &lead_spinner]() {
while (true) {
p4.set_prefix_text("{ " + std::to_string(p4.current()) + "% } ");
p4.lead_bar_progress_with(lead_spinner[index4 % lead_spinner.size()]);
p4.set_option(option::PrefixText{"{ " + std::to_string(p4.current()) + "% } "});
p4.set_option(option::Lead{lead_spinner[index4 % lead_spinner.size()]});
index4 += 1;
if (p4.current() + 2 >= 100) {
std::cout << std::endl;
p4.set_foreground_color(indicators::Color::RED);
p4.set_prefix_text("{ ERROR }");
p4.start_bar_with("");
p4.fill_bar_progress_with("");
p4.lead_bar_progress_with("");
p4.fill_bar_remainder_with("");
p4.end_bar_with("");
p4.show_percentage();
p4.set_postfix_text("Failed to restore system");
p4.set_option(option::ForegroundColor{indicators::Color::red});
p4.set_option(option::PrefixText{"{ ERROR }"});
p4.set_option(option::Start{});
p4.set_option(option::Fill{});
p4.set_option(option::Lead{});
p4.set_option(option::Remainder{});
p4.set_option(option::ShowPercentage{true});
p4.set_option(option::PostfixText{"Failed to restore system"});
p4.mark_as_completed();
break;
}
@@ -157,23 +156,22 @@ int main() {
//
// GOING BACKWARDS
//
indicators::ProgressBar p;
p.set_bar_width(50);
p.start_bar_with("[");
p.fill_bar_progress_with("");
p.lead_bar_progress_with("");
p.fill_bar_remainder_with("-");
p.end_bar_with("]");
p.set_progress(100);
p.set_foreground_color(indicators::Color::WHITE);
p.set_postfix_text("Reverting system restore");
indicators::ProgressBar p{option::BarWidth{50},
option::Start{"["},
option::Fill{""},
option::Lead{""},
option::Remainder{"-"},
option::End{"]"},
option::ForegroundColor{indicators::Color::white},
option::PostfixText{"Reverting system restore"}};
p.set_progress(100); // TODO backwards as an option?
std::atomic<size_t> progress{100};
auto job = [&p, &progress]() {
while (true) {
progress -= 1;
p.set_progress(progress);
if (progress == 0) {
p.set_postfix_text("Revert complete!");
p.set_option(option::PostfixText{"Revert complete!"});
p.mark_as_completed();
break;
}
@@ -189,19 +187,19 @@ int main() {
//
// PROGRESS BAR 5
//
indicators::ProgressSpinner p;
p.set_prefix_text("");
p.set_postfix_text("Checking credentials");
p.set_foreground_color(indicators::Color::YELLOW);
p.set_spinner_states({"", "", "", "", "", "", "", ""});
indicators::ProgressSpinner p{
option::PrefixText{""}, option::PostfixText{"Checking credentials"},
option::ForegroundColor{indicators::Color::yellow},
option::SpinnerStates{std::vector<std::string>{"", "", "", "", "", "", "", ""}}};
auto job = [&p]() {
while (true) {
if (p.is_completed()) {
p.set_foreground_color(indicators::Color::GREEN);
p.set_prefix_text("");
p.hide_spinner();
p.hide_percentage();
p.set_postfix_text("Authenticated!");
p.set_option(option::ForegroundColor{indicators::Color::green});
p.set_option(option::PrefixText{""});
p.set_option(option::ShowSpinner{false});
p.set_option(option::ShowPercentage{false});
p.set_option(option::PostfixText{"Authenticated!"});
p.mark_as_completed();
break;
} else
@@ -218,70 +216,67 @@ int main() {
//
// PROGRESS BAR 6
//
indicators::ProgressSpinner p;
p.set_prefix_text(" - ");
p.set_postfix_text("Searching for the Moon");
p.set_foreground_color(indicators::Color::WHITE);
p.set_spinner_states({"", "", "", ""});
p.hide_percentage();
indicators::ProgressSpinner p{
option::PrefixText{" - "}, option::PostfixText{"Searching for the Moon"},
option::ForegroundColor{indicators::Color::white}, option::ShowPercentage{false},
option::SpinnerStates{std::vector<std::string>{"", "", "", ""}}};
auto job = [&p]() {
while (true) {
auto current = p.current();
if (current == 24) {
p.set_prefix_text(" - ✔");
p.hide_spinner();
p.set_option(option::PrefixText{" - ✔"});
p.set_option(option::ShowSpinner{false});
} else if (current == 25) {
std::cout << std::endl;
p.show_spinner();
p.set_prefix_text(" - ");
p.set_postfix_text("Contacting Kerbal headquarters");
p.set_option(option::ShowSpinner{true});
p.set_option(option::PrefixText{" - "});
p.set_option(option::PostfixText{"Contacting Kerbal headquarters"});
} else if (current == 49) {
p.set_prefix_text(" - ✔");
p.hide_spinner();
p.set_option(option::PrefixText{" - ✔"});
p.set_option(option::ShowSpinner{false});
} else if (current == 50) {
std::cout << std::endl;
p.show_spinner();
p.set_prefix_text(" - ");
p.set_postfix_text("Designing spaceship");
p.set_option(option::ShowSpinner{true});
p.set_option(option::PrefixText{" - "});
p.set_option(option::PostfixText{"Designing spaceship"});
} else if (current == 74) {
p.set_prefix_text(" - ✔");
p.hide_spinner();
p.set_option(option::PrefixText{" - ✔"});
p.set_option(option::ShowSpinner{false});
} else if (current == 75) {
std::cout << std::endl;
p.show_spinner();
p.set_prefix_text(" - ");
p.set_postfix_text("Launching rocket");
p.set_option(option::ShowSpinner{true});
p.set_option(option::PrefixText{" - "});
p.set_option(option::PostfixText{"Launching rocket"});
} else if (current == 95) {
p.set_prefix_text(" - ✔");
p.hide_spinner();
p.set_option(option::PrefixText{" - ✔"});
p.set_option(option::ShowSpinner{false});
} else if (current == 99) {
std::cout << std::endl;
//
// NESTED PROGRESS BAR
//
indicators::ProgressBar p2;
p2.set_bar_width(30);
p2.set_prefix_text(" - ");
p2.start_bar_with("🌎");
p2.fill_bar_progress_with("·");
p2.lead_bar_progress_with("🚀");
p2.fill_bar_remainder_with(" ");
p2.end_bar_with("🌑");
p2.set_postfix_text("Achieved low-Earth orbit");
p2.set_foreground_color(indicators::Color::WHITE);
indicators::ProgressBar p2{option::BarWidth{30},
option::PrefixText{" - "},
option::Start{"🌎"},
option::Fill{"·"},
option::Lead{"🚀"},
option::Remainder{" "},
option::End{"🌑"},
option::PostfixText{"Achieved low-Earth orbit"},
option::ForegroundColor{indicators::Color::white}};
std::vector<std::string> ship_trail{"", "", "", "", "", "", "", ""};
std::atomic<int> ship_trail_index{0};
auto job2 = [&p2, &ship_trail_index, &ship_trail]() {
while (true) {
auto ticks = p2.current();
if (ticks > 20 && ticks < 50)
p2.set_postfix_text("Switching to trans-lunar trajectory");
p2.set_option(option::PostfixText{"Switching to trans-lunar trajectory"});
else if (ticks > 50 && ticks < 80)
p2.set_postfix_text("Transferred to Lunar lander");
p2.set_option(option::PostfixText{"Transferred to Lunar lander"});
else if (ticks > 80 && ticks < 98)
p2.set_postfix_text("Almost there");
p2.set_option(option::PostfixText{"Almost there"});
else if (ticks >= 98)
p2.set_postfix_text("Landed on the Moon");
p2.set_option(option::PostfixText{"Landed on the Moon"});
p2.tick();
ship_trail_index += 1;
if (p2.is_completed())
@@ -291,7 +286,7 @@ int main() {
};
std::thread thread2(job2);
thread2.join();
p.set_postfix_text("Mission successful!");
p.set_option(indicators::option::PostfixText{"Mission successful!"});
p.mark_as_completed();
break;
}

BIN
img/block_progress_bar.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 MiB

BIN
img/multi_progress.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
img/progress_bar_tick.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

BIN
img/progress_spinner.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
img/time_meter.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 KiB

View File

@@ -0,0 +1,240 @@
/*
Activity Indicators for Modern C++
https://github.com/p-ranav/indicators
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2019 Pranav Srinivas Kumar <pranav.srinivas.kumar@gmail.com>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <indicators/color.hpp>
#include <indicators/details/stream_helper.hpp>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <indicators/setting.hpp>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <tuple>
namespace indicators {
class BlockProgressBar {
using Settings = std::tuple<option::ForegroundColor, option::BarWidth, option::Start, option::End,
option::PrefixText, option::PostfixText, option::ShowPercentage,
option::ShowElapsedTime, option::ShowRemainingTime, option::Completed,
option::SavedStartTime, option::MaxPostfixTextLen>;
public:
template <typename... Args,
typename std::enable_if<details::are_settings_from_tuple<
Settings, typename std::decay<Args>::type...>::value,
void *>::type = nullptr>
explicit BlockProgressBar(Args &&... args)
: settings_(details::get<details::ProgressBarOption::foreground_color>(
option::ForegroundColor{Color::white}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::bar_width>(option::BarWidth{100},
std::forward<Args>(args)...),
details::get<details::ProgressBarOption::start>(option::Start{"["},
std::forward<Args>(args)...),
details::get<details::ProgressBarOption::end>(option::End{"]"},
std::forward<Args>(args)...),
details::get<details::ProgressBarOption::prefix_text>(
option::PrefixText{""}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::postfix_text>(
option::PostfixText{""}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_percentage>(
option::ShowPercentage{true}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_elapsed_time>(
option::ShowElapsedTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_remaining_time>(
option::ShowRemainingTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::completed>(option::Completed{false},
std::forward<Args>(args)...),
details::get<details::ProgressBarOption::saved_start_time>(
option::SavedStartTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::max_postfix_text_len>(
option::MaxPostfixTextLen{0}, std::forward<Args>(args)...)) {}
template <typename T, details::ProgressBarOption id>
void set_option(details::Setting<T, id> &&setting) {
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(
std::declval<Settings>()))>::type>::value,
"Setting has wrong type!");
std::lock_guard<std::mutex> lock(mutex_);
get_value<id>() = std::move(setting).value;
}
template <typename T, details::ProgressBarOption id>
void set_option(const details::Setting<T, id> &setting) {
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(
std::declval<Settings>()))>::type>::value,
"Setting has wrong type!");
std::lock_guard<std::mutex> lock(mutex_);
get_value<id>() = setting.value;
}
void set_option(
const details::Setting<std::string, details::ProgressBarOption::postfix_text> &setting) {
std::lock_guard<std::mutex> lock(mutex_);
get_value<details::ProgressBarOption::postfix_text>() = setting.value;
if (setting.value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()) {
get_value<details::ProgressBarOption::max_postfix_text_len>() = setting.value.length();
}
}
void
set_option(details::Setting<std::string, details::ProgressBarOption::postfix_text> &&setting) {
std::lock_guard<std::mutex> lock(mutex_);
get_value<details::ProgressBarOption::postfix_text>() = std::move(setting).value;
auto &new_value = get_value<details::ProgressBarOption::postfix_text>();
if (new_value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()) {
get_value<details::ProgressBarOption::max_postfix_text_len>() = new_value.length();
}
}
void set_progress(float value) {
{
std::lock_guard<std::mutex> lock{mutex_};
progress_ = value;
}
save_start_time();
print_progress();
}
void tick() {
{
std::lock_guard<std::mutex> lock{mutex_};
progress_ += 1;
}
save_start_time();
print_progress();
}
size_t current() {
std::lock_guard<std::mutex> lock{mutex_};
return std::min(static_cast<size_t>(progress_), size_t(100));
}
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
void mark_as_completed() {
get_value<details::ProgressBarOption::completed>() = true;
print_progress();
}
private:
template <details::ProgressBarOption id>
auto get_value() -> decltype((details::get_value<id>(std::declval<Settings &>()).value)) {
return details::get_value<id>(settings_).value;
}
template <details::ProgressBarOption id>
auto get_value() const
-> decltype((details::get_value<id>(std::declval<const Settings &>()).value)) {
return details::get_value<id>(settings_).value;
}
Settings settings_;
float progress_{0.0};
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_;
std::mutex mutex_;
template <typename Indicator, size_t count> friend class MultiProgress;
template <typename Indicator> friend class DynamicProgress;
std::atomic<bool> multi_progress_mode_{false};
void save_start_time() {
auto &show_elapsed_time = get_value<details::ProgressBarOption::show_elapsed_time>();
auto &saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
auto &show_remaining_time = get_value<details::ProgressBarOption::show_remaining_time>();
if ((show_elapsed_time || show_remaining_time) && !saved_start_time) {
start_time_point_ = std::chrono::high_resolution_clock::now();
saved_start_time = true;
}
}
void print_progress(bool from_multi_progress = false) {
if (multi_progress_mode_ && !from_multi_progress) {
if (progress_ > 100.0) {
get_value<details::ProgressBarOption::completed>() = true;
}
return;
}
std::lock_guard<std::mutex> lock{mutex_};
auto now = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - start_time_point_);
std::cout << termcolor::bold;
details::set_stream_color(std::cout, get_value<details::ProgressBarOption::foreground_color>());
std::cout << get_value<details::ProgressBarOption::prefix_text>();
std::cout << get_value<details::ProgressBarOption::start>();
details::BlockProgressScaleWriter writer{std::cout,
get_value<details::ProgressBarOption::bar_width>()};
writer.write(progress_);
std::cout << get_value<details::ProgressBarOption::end>();
if (get_value<details::ProgressBarOption::show_percentage>()) {
std::cout << " " << std::min(static_cast<size_t>(progress_), size_t(100)) << "%";
}
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
std::cout << " [";
details::write_duration(std::cout, elapsed);
}
if (get_value<details::ProgressBarOption::show_remaining_time>()) {
if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "<";
else
std::cout << " [";
auto eta = std::chrono::nanoseconds(
progress_ > 0 ? static_cast<long long>(elapsed.count() * 100 / progress_) : 0);
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
details::write_duration(std::cout, remaining);
std::cout << "]";
} else {
if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "]";
}
if (get_value<details::ProgressBarOption::max_postfix_text_len>() == 0)
get_value<details::ProgressBarOption::max_postfix_text_len>() = 10;
std::cout << " " << get_value<details::ProgressBarOption::postfix_text>()
<< std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ')
<< "\r";
std::cout.flush();
if (progress_ > 100.0) {
get_value<details::ProgressBarOption::completed>() = true;
}
if (get_value<details::ProgressBarOption::completed>() &&
!from_multi_progress) // Don't std::endl if calling from MultiProgress
std::cout << termcolor::reset << std::endl;
}
};
} // namespace indicators

View File

@@ -28,5 +28,5 @@ SOFTWARE.
#include <indicators/termcolor.hpp>
namespace indicators {
enum class Color { GREY, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE };
enum class Color { grey, red, green, yellow, blue, magenta, cyan, white };
}

View File

@@ -0,0 +1,127 @@
#pragma once
#include <indicators/color.hpp>
#include <indicators/termcolor.hpp>
#include <algorithm>
#include <chrono>
#include <iomanip>
#include <ostream>
#include <string>
#include <vector>
#include <cassert>
#include <cmath>
namespace indicators {
namespace details {
inline void set_stream_color(std::ostream &os, Color color) {
switch (color) {
case Color::grey:
os << termcolor::grey;
break;
case Color::red:
os << termcolor::red;
break;
case Color::green:
os << termcolor::green;
break;
case Color::yellow:
os << termcolor::yellow;
break;
case Color::blue:
os << termcolor::blue;
break;
case Color::magenta:
os << termcolor::magenta;
break;
case Color::cyan:
os << termcolor::cyan;
break;
case Color::white:
os << termcolor::white;
break;
default:
assert(false);
}
}
inline std::ostream &write_duration(std::ostream &os, std::chrono::nanoseconds ns) {
using namespace std;
using namespace std::chrono;
using days = duration<int, ratio<86400>>;
char fill = os.fill();
os.fill('0');
auto d = duration_cast<days>(ns);
ns -= d;
auto h = duration_cast<hours>(ns);
ns -= h;
auto m = duration_cast<minutes>(ns);
ns -= m;
auto s = duration_cast<seconds>(ns);
if (d.count() > 0)
os << setw(2) << d.count() << "d:";
if (h.count() > 0)
os << setw(2) << h.count() << "h:";
os << setw(2) << m.count() << "m:" << setw(2) << s.count() << 's';
os.fill(fill);
return os;
}
class BlockProgressScaleWriter {
public:
BlockProgressScaleWriter(std::ostream &os, size_t bar_width) : os(os), bar_width(bar_width) {}
std::ostream &write(float progress) {
std::string fill_text{""};
std::vector<std::string> lead_characters{" ", "", "", "", "", "", "", ""};
auto value = std::min(1.0f, std::max(0.0f, progress / 100.0f));
auto whole_width = std::floor(value * bar_width);
auto remainder_width = fmod((value * bar_width), 1.0f);
auto part_width = std::floor(remainder_width * lead_characters.size());
std::string lead_text = lead_characters[size_t(part_width)];
if ((bar_width - whole_width - 1) < 0)
lead_text = "";
for (size_t i = 0; i < whole_width; ++i)
os << fill_text;
os << lead_text;
for (size_t i = 0; i < (bar_width - whole_width - 1); ++i)
os << " ";
return os;
}
private:
std::ostream &os;
size_t bar_width = 0;
};
class ProgressScaleWriter {
public:
ProgressScaleWriter(std::ostream &os, size_t bar_width, const std::string &fill,
const std::string &lead, const std::string &remainder)
: os(os), bar_width(bar_width), fill(fill), lead(lead), remainder(remainder) {}
std::ostream &write(float progress) {
auto pos = static_cast<size_t>(progress * static_cast<float>(bar_width) / 100.0);
for (size_t i = 0; i < bar_width; ++i) {
if (i < pos)
os << fill;
else if (i == pos)
os << lead;
else
os << remainder;
}
return os;
}
private:
std::ostream &os;
size_t bar_width = 0;
std::string fill;
std::string lead;
std::string remainder;
};
} // namespace details
} // namespace indicators

View File

@@ -0,0 +1,138 @@
/*
Activity Indicators for Modern C++
https://github.com/p-ranav/indicators
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2019 Pranav Srinivas Kumar <pranav.srinivas.kumar@gmail.com>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <atomic>
#include <functional>
#include <indicators/color.hpp>
#include <indicators/setting.hpp>
#include <iostream>
#include <mutex>
#include <vector>
namespace indicators {
template <typename Indicator> class DynamicProgress {
using Settings = std::tuple<option::HideBarWhenComplete>;
public:
template <typename... Indicators> explicit DynamicProgress(Indicators &... bars) {
bars_ = {bars...};
for (auto &bar : bars_) {
bar.get().multi_progress_mode_ = true;
++total_count_;
++incomplete_count_;
}
}
Indicator &operator[](size_t index) {
print_progress();
std::lock_guard<std::mutex> lock{mutex_};
return bars_[index].get();
}
size_t push_back(Indicator &bar) {
std::lock_guard<std::mutex> lock{mutex_};
bar.multi_progress_mode_ = true;
bars_.push_back(bar);
return bars_.size() - 1;
}
template <typename T, details::ProgressBarOption id>
void set_option(details::Setting<T, id> &&setting) {
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(
std::declval<Settings>()))>::type>::value,
"Setting has wrong type!");
std::lock_guard<std::mutex> lock(mutex_);
get_value<id>() = std::move(setting).value;
}
template <typename T, details::ProgressBarOption id>
void set_option(const details::Setting<T, id> &setting) {
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(
std::declval<Settings>()))>::type>::value,
"Setting has wrong type!");
std::lock_guard<std::mutex> lock(mutex_);
get_value<id>() = setting.value;
}
private:
Settings settings_;
std::atomic<bool> started_{false};
std::mutex mutex_;
std::vector<std::reference_wrapper<Indicator>> bars_;
std::atomic<size_t> total_count_{0};
std::atomic<size_t> incomplete_count_{0};
template <details::ProgressBarOption id>
auto get_value() -> decltype((details::get_value<id>(std::declval<Settings &>()).value)) {
return details::get_value<id>(settings_).value;
}
template <details::ProgressBarOption id>
auto get_value() const
-> decltype((details::get_value<id>(std::declval<const Settings &>()).value)) {
return details::get_value<id>(settings_).value;
}
void print_progress() {
std::lock_guard<std::mutex> lock{mutex_};
auto &hide_bar_when_complete = get_value<details::ProgressBarOption::hide_bar_when_complete>();
if (hide_bar_when_complete) {
// Hide completed bars
if (started_) {
for (size_t i = 0; i < incomplete_count_; ++i)
std::cout << "\033[A\r\033[K" << std::flush;
}
incomplete_count_ = 0;
for (auto &bar : bars_) {
if (!bar.get().is_completed()) {
bar.get().print_progress(true);
std::cout << "\n";
++incomplete_count_;
}
}
if (!started_)
started_ = true;
} else {
// Don't hide any bars
if (started_) {
for (size_t i = 0; i < total_count_; ++i)
std::cout << "\x1b[A";
}
for (auto &bar: bars_) {
bar.get().print_progress(true);
std::cout << "\n";
}
if (!started_)
started_ = true;
}
total_count_ = bars_.size();
std::cout << termcolor::reset;
}
};
} // namespace indicators

View File

@@ -0,0 +1,94 @@
/*
Activity Indicators for Modern C++
https://github.com/p-ranav/indicators
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2019 Pranav Srinivas Kumar <pranav.srinivas.kumar@gmail.com>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <atomic>
#include <functional>
#include <indicators/color.hpp>
#include <iostream>
#include <mutex>
#include <vector>
namespace indicators {
template <typename Indicator, size_t count> class MultiProgress {
public:
template <typename... Indicators,
typename = typename std::enable_if<(sizeof...(Indicators) == count)>::type>
explicit MultiProgress(Indicators &... bars) {
bars_ = {bars...};
for (auto &bar : bars_) {
bar.get().multi_progress_mode_ = true;
}
}
template <size_t index>
typename std::enable_if<(index >= 0 && index < count), void>::type set_progress(float value) {
if (!bars_[index].get().is_completed())
bars_[index].get().set_progress(value);
print_progress();
}
template <size_t index>
typename std::enable_if<(index >= 0 && index < count), void>::type tick() {
if (!bars_[index].get().is_completed())
bars_[index].get().tick();
print_progress();
}
template <size_t index>
typename std::enable_if<(index >= 0 && index < count), bool>::type is_completed() const {
return bars_[index].get().is_completed();
}
private:
std::atomic<bool> started_{false};
std::mutex mutex_;
std::vector<std::reference_wrapper<Indicator>> bars_;
bool _all_completed() {
bool result{true};
for (size_t i = 0; i < count; ++i)
result &= bars_[i].get().is_completed();
return result;
}
void print_progress() {
std::lock_guard<std::mutex> lock{mutex_};
if (started_)
for (size_t i = 0; i < count; ++i)
std::cout << "\x1b[A";
for (auto &bar : bars_) {
bar.get().print_progress(true);
std::cout << "\n";
}
std::cout << termcolor::reset;
if (!started_)
started_ = true;
}
};
} // namespace indicators

View File

@@ -25,164 +25,232 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <indicators/details/stream_helper.hpp>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cmath>
#include <indicators/color.hpp>
#include <indicators/setting.hpp>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <tuple>
#include <type_traits>
namespace indicators {
class ProgressBar {
using Settings =
std::tuple<option::BarWidth, option::PrefixText, option::PostfixText, option::Start,
option::End, option::Fill, option::Lead, option::Remainder,
option::MaxPostfixTextLen, option::Completed, option::ShowPercentage,
option::ShowElapsedTime, option::ShowRemainingTime, option::SavedStartTime,
option::ForegroundColor>;
public:
void set_foreground_color(Color color) {
std::unique_lock<std::mutex> lock{_mutex};
_foreground_color = color;
template <typename... Args,
typename std::enable_if<details::are_settings_from_tuple<
Settings, typename std::decay<Args>::type...>::value,
void *>::type = nullptr>
explicit ProgressBar(Args &&... args)
: settings_(details::get<details::ProgressBarOption::bar_width>(option::BarWidth{100},
std::forward<Args>(args)...),
details::get<details::ProgressBarOption::prefix_text>(
option::PrefixText{}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::postfix_text>(
option::PostfixText{}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::start>(option::Start{"["},
std::forward<Args>(args)...),
details::get<details::ProgressBarOption::end>(option::End{"]"},
std::forward<Args>(args)...),
details::get<details::ProgressBarOption::fill>(option::Fill{"="},
std::forward<Args>(args)...),
details::get<details::ProgressBarOption::lead>(option::Lead{">"},
std::forward<Args>(args)...),
details::get<details::ProgressBarOption::remainder>(option::Remainder{" "},
std::forward<Args>(args)...),
details::get<details::ProgressBarOption::max_postfix_text_len>(
option::MaxPostfixTextLen{0}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::completed>(option::Completed{false},
std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_percentage>(
option::ShowPercentage{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_elapsed_time>(
option::ShowElapsedTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_remaining_time>(
option::ShowRemainingTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::saved_start_time>(
option::SavedStartTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::foreground_color>(
option::ForegroundColor{Color::white}, std::forward<Args>(args)...)) {}
template <typename T, details::ProgressBarOption id>
void set_option(details::Setting<T, id> &&setting) {
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(
std::declval<Settings>()))>::type>::value,
"Setting has wrong type!");
std::lock_guard<std::mutex> lock(mutex_);
get_value<id>() = std::move(setting).value;
}
void set_bar_width(size_t bar_width) {
std::unique_lock<std::mutex> lock{_mutex};
_bar_width = bar_width;
template <typename T, details::ProgressBarOption id>
void set_option(const details::Setting<T, id> &setting) {
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(
std::declval<Settings>()))>::type>::value,
"Setting has wrong type!");
std::lock_guard<std::mutex> lock(mutex_);
get_value<id>() = setting.value;
}
void start_bar_with(const std::string &start) {
std::unique_lock<std::mutex> lock{_mutex};
_start = start;
}
void fill_bar_progress_with(const std::string &fill) {
std::unique_lock<std::mutex> lock{_mutex};
_fill = fill;
}
void lead_bar_progress_with(const std::string &lead) {
std::unique_lock<std::mutex> lock{_mutex};
_lead = lead;
}
void fill_bar_remainder_with(const std::string &remainder) {
std::unique_lock<std::mutex> lock{_mutex};
_remainder = remainder;
}
void end_bar_with(const std::string &end) {
std::unique_lock<std::mutex> lock{_mutex};
_end = end;
}
void set_prefix_text(const std::string &text) {
std::unique_lock<std::mutex> lock{_mutex};
_prefix_text = text;
}
void set_postfix_text(const std::string &text) {
std::unique_lock<std::mutex> lock{_mutex};
_postfix_text = text;
if (_postfix_text.length() > _max_postfix_text_length)
_max_postfix_text_length = _postfix_text.length();
}
void show_percentage() { _show_percentage = true; }
void hide_percentage() { _show_percentage = false; }
void set_progress(float value) {
{
std::unique_lock<std::mutex> lock{_mutex};
_progress = value;
void set_option(
const details::Setting<std::string, details::ProgressBarOption::postfix_text> &setting) {
std::lock_guard<std::mutex> lock(mutex_);
get_value<details::ProgressBarOption::postfix_text>() = setting.value;
if (setting.value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()) {
get_value<details::ProgressBarOption::max_postfix_text_len>() = setting.value.length();
}
_print_progress();
}
void
set_option(details::Setting<std::string, details::ProgressBarOption::postfix_text> &&setting) {
std::lock_guard<std::mutex> lock(mutex_);
get_value<details::ProgressBarOption::postfix_text>() = std::move(setting).value;
auto &new_value = get_value<details::ProgressBarOption::postfix_text>();
if (new_value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()) {
get_value<details::ProgressBarOption::max_postfix_text_len>() = new_value.length();
}
}
void set_progress(float new_progress) {
{
std::lock_guard<std::mutex> lck(mutex_);
progress_ = new_progress;
}
save_start_time();
print_progress();
}
void tick() {
{
std::unique_lock<std::mutex> lock{_mutex};
_progress += 1;
std::lock_guard<std::mutex> lock{mutex_};
progress_ += 1;
}
_print_progress();
save_start_time();
print_progress();
}
size_t current() {
std::unique_lock<std::mutex> lock{_mutex};
return std::min(static_cast<size_t>(_progress), size_t(100));
std::lock_guard<std::mutex> lock{mutex_};
return std::min(static_cast<size_t>(progress_), size_t(100));
}
bool is_completed() const { return _completed; }
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
void mark_as_completed() {
_completed = true;
_print_progress();
get_value<details::ProgressBarOption::completed>() = true;
print_progress();
}
private:
float _progress{0.0};
size_t _bar_width{100};
std::string _prefix_text{""};
std::string _start{"["};
std::string _fill{"="};
std::string _lead{">"};
std::string _remainder{" "};
std::string _end{"]"};
std::string _postfix_text{""};
std::atomic<size_t> _max_postfix_text_length{0};
std::atomic<bool> _completed{false};
std::atomic<bool> _show_percentage{true};
std::mutex _mutex;
Color _foreground_color;
template <details::ProgressBarOption id>
auto get_value() -> decltype((details::get_value<id>(std::declval<Settings &>()).value)) {
return details::get_value<id>(settings_).value;
}
template <details::ProgressBarOption id>
auto get_value() const
-> decltype((details::get_value<id>(std::declval<const Settings &>()).value)) {
return details::get_value<id>(settings_).value;
}
float progress_{0};
Settings settings_;
std::chrono::nanoseconds elapsed_;
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_;
std::mutex mutex_;
template <typename Indicator, size_t count> friend class MultiProgress;
template <typename Indicator> friend class DynamicProgress;
std::atomic<bool> multi_progress_mode_{false};
void save_start_time() {
auto &show_elapsed_time = get_value<details::ProgressBarOption::show_elapsed_time>();
auto &saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
auto &show_remaining_time = get_value<details::ProgressBarOption::show_remaining_time>();
if ((show_elapsed_time || show_remaining_time) && !saved_start_time) {
start_time_point_ = std::chrono::high_resolution_clock::now();
saved_start_time = true;
}
}
void print_progress(bool from_multi_progress = false) {
std::lock_guard<std::mutex> lock{mutex_};
if (multi_progress_mode_ && !from_multi_progress) {
if (progress_ > 100.0) {
get_value<details::ProgressBarOption::completed>() = true;
}
return;
}
auto now = std::chrono::high_resolution_clock::now();
if (!get_value<details::ProgressBarOption::completed>())
elapsed_ = std::chrono::duration_cast<std::chrono::nanoseconds>(now - start_time_point_);
void _print_progress() {
std::unique_lock<std::mutex> lock{_mutex};
std::cout << termcolor::bold;
switch (_foreground_color) {
case Color::GREY:
std::cout << termcolor::grey;
break;
case Color::RED:
std::cout << termcolor::red;
break;
case Color::GREEN:
std::cout << termcolor::green;
break;
case Color::YELLOW:
std::cout << termcolor::yellow;
break;
case Color::BLUE:
std::cout << termcolor::blue;
break;
case Color::MAGENTA:
std::cout << termcolor::magenta;
break;
case Color::CYAN:
std::cout << termcolor::cyan;
break;
case Color::WHITE:
std::cout << termcolor::white;
break;
details::set_stream_color(std::cout, get_value<details::ProgressBarOption::foreground_color>());
std::cout << get_value<details::ProgressBarOption::prefix_text>();
std::cout << get_value<details::ProgressBarOption::start>();
details::ProgressScaleWriter writer{std::cout,
get_value<details::ProgressBarOption::bar_width>(),
get_value<details::ProgressBarOption::fill>(),
get_value<details::ProgressBarOption::lead>(),
get_value<details::ProgressBarOption::remainder>()};
writer.write(progress_);
std::cout << get_value<details::ProgressBarOption::end>();
if (get_value<details::ProgressBarOption::show_percentage>()) {
std::cout << " " << std::min(static_cast<size_t>(progress_), size_t(100)) << "%";
}
std::cout << _prefix_text;
std::cout << _start;
auto pos = static_cast<size_t>(_progress * static_cast<float>(_bar_width) / 100.0);
for (size_t i = 0; i < _bar_width; ++i) {
if (i < pos)
std::cout << _fill;
else if (i == pos)
std::cout << _lead;
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
std::cout << " [";
details::write_duration(std::cout, elapsed_);
}
if (get_value<details::ProgressBarOption::show_remaining_time>()) {
if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "<";
else
std::cout << _remainder;
std::cout << " [";
auto eta = std::chrono::nanoseconds(
progress_ > 0 ? static_cast<long long>(elapsed_.count() * 100 / progress_) : 0);
auto remaining = eta > elapsed_ ? (eta - elapsed_) : (elapsed_ - eta);
details::write_duration(std::cout, remaining);
std::cout << "]";
} else {
if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "]";
}
std::cout << _end;
if (_show_percentage) {
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
}
if (_max_postfix_text_length == 0)
_max_postfix_text_length = 10;
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
if (get_value<details::ProgressBarOption::max_postfix_text_len>() == 0)
get_value<details::ProgressBarOption::max_postfix_text_len>() = 10;
std::cout << " " << get_value<details::ProgressBarOption::postfix_text>()
<< std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ')
<< "\r";
std::cout.flush();
if (_progress > 100.0) {
_completed = true;
if (progress_ > 100.0) {
get_value<details::ProgressBarOption::completed>() = true;
}
if (_completed)
if (get_value<details::ProgressBarOption::completed>() &&
!from_multi_progress) // Don't std::endl if calling from MultiProgress
std::cout << termcolor::reset << std::endl;
}
};

View File

@@ -25,133 +25,204 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <indicators/details/stream_helper.hpp>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cmath>
#include <indicators/color.hpp>
#include <indicators/setting.hpp>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <tuple>
#include <vector>
namespace indicators {
class ProgressSpinner {
using Settings =
std::tuple<option::ForegroundColor, option::PrefixText, option::PostfixText,
option::ShowPercentage, option::ShowElapsedTime, option::ShowRemainingTime,
option::ShowSpinner, option::SavedStartTime, option::Completed,
option::MaxPostfixTextLen, option::SpinnerStates>;
public:
void set_foreground_color(Color color) {
std::unique_lock<std::mutex> lock{_mutex};
_foreground_color = color;
template <typename... Args,
typename std::enable_if<details::are_settings_from_tuple<
Settings, typename std::decay<Args>::type...>::value,
void *>::type = nullptr>
explicit ProgressSpinner(Args &&... args)
: settings_(details::get<details::ProgressBarOption::foreground_color>(
option::ForegroundColor{Color::white}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::prefix_text>(
option::PrefixText{}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::postfix_text>(
option::PostfixText{}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_percentage>(
option::ShowPercentage{true}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_elapsed_time>(
option::ShowElapsedTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::show_remaining_time>(
option::ShowRemainingTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::spinner_show>(
option::ShowSpinner{true}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::saved_start_time>(
option::SavedStartTime{false}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::completed>(option::Completed{false},
std::forward<Args>(args)...),
details::get<details::ProgressBarOption::max_postfix_text_len>(
option::MaxPostfixTextLen{0}, std::forward<Args>(args)...),
details::get<details::ProgressBarOption::spinner_states>(
option::SpinnerStates{std::vector<std::string>{"", "", "", "", "", "",
"", "", "", ""}},
std::forward<Args>(args)...)) {}
template <typename T, details::ProgressBarOption id>
void set_option(details::Setting<T, id> &&setting) {
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(
std::declval<Settings>()))>::type>::value,
"Setting has wrong type!");
std::lock_guard<std::mutex> lock(mutex_);
get_value<id>() = std::move(setting).value;
}
void set_prefix_text(const std::string &text) {
std::unique_lock<std::mutex> lock{_mutex};
_prefix_text = text;
template <typename T, details::ProgressBarOption id>
void set_option(const details::Setting<T, id> &setting) {
static_assert(!std::is_same<T, typename std::decay<decltype(details::get_value<id>(
std::declval<Settings>()))>::type>::value,
"Setting has wrong type!");
std::lock_guard<std::mutex> lock(mutex_);
get_value<id>() = setting.value;
}
void set_postfix_text(const std::string &text) {
std::unique_lock<std::mutex> lock{_mutex};
_postfix_text = text;
if (_postfix_text.length() > _max_postfix_text_length)
_max_postfix_text_length = _postfix_text.length();
void set_option(
const details::Setting<std::string, details::ProgressBarOption::postfix_text> &setting) {
std::lock_guard<std::mutex> lock(mutex_);
get_value<details::ProgressBarOption::postfix_text>() = setting.value;
if (setting.value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()) {
get_value<details::ProgressBarOption::max_postfix_text_len>() = setting.value.length();
}
}
void show_percentage() { _show_percentage = true; }
void hide_percentage() { _show_percentage = false; }
void show_spinner() { _show_spinner = true; }
void hide_spinner() { _show_spinner = false; }
void
set_option(details::Setting<std::string, details::ProgressBarOption::postfix_text> &&setting) {
std::lock_guard<std::mutex> lock(mutex_);
get_value<details::ProgressBarOption::postfix_text>() = std::move(setting).value;
auto &new_value = get_value<details::ProgressBarOption::postfix_text>();
if (new_value.length() > get_value<details::ProgressBarOption::max_postfix_text_len>()) {
get_value<details::ProgressBarOption::max_postfix_text_len>() = new_value.length();
}
}
void set_progress(float value) {
{
std::unique_lock<std::mutex> lock{_mutex};
_progress = value;
std::lock_guard<std::mutex> lock{mutex_};
progress_ = value;
}
_print_progress();
save_start_time();
print_progress();
}
void tick() {
{
std::unique_lock<std::mutex> lock{_mutex};
_progress += 1;
std::lock_guard<std::mutex> lock{mutex_};
progress_ += 1;
}
_print_progress();
save_start_time();
print_progress();
}
size_t current() {
std::unique_lock<std::mutex> lock{_mutex};
return std::min(static_cast<size_t>(_progress), size_t(100));
std::lock_guard<std::mutex> lock{mutex_};
return std::min(static_cast<size_t>(progress_), size_t(100));
}
bool is_completed() const { return _completed; }
bool is_completed() const { return get_value<details::ProgressBarOption::completed>(); }
void mark_as_completed() {
_completed = true;
_print_progress();
}
void set_spinner_states(const std::vector<std::string> &states) {
std::unique_lock<std::mutex> lock{_mutex};
_states = states;
get_value<details::ProgressBarOption::completed>() = true;
print_progress();
}
private:
float _progress{0.0};
std::string _prefix_text{""};
size_t _index{0};
std::vector<std::string> _states{"", "", "", "", "", "", "", "", "", ""};
std::string _postfix_text{""};
std::atomic<size_t> _max_postfix_text_length{0};
std::atomic<bool> _completed{false};
std::atomic<bool> _show_percentage{true};
std::atomic<bool> _show_spinner{true};
std::mutex _mutex;
Color _foreground_color;
Settings settings_;
float progress_{0.0};
size_t index_{0};
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_point_;
std::mutex mutex_;
template <details::ProgressBarOption id>
auto get_value() -> decltype((details::get_value<id>(std::declval<Settings &>()).value)) {
return details::get_value<id>(settings_).value;
}
template <details::ProgressBarOption id>
auto get_value() const
-> decltype((details::get_value<id>(std::declval<const Settings &>()).value)) {
return details::get_value<id>(settings_).value;
}
void save_start_time() {
auto &show_elapsed_time = get_value<details::ProgressBarOption::show_elapsed_time>();
auto &show_remaining_time = get_value<details::ProgressBarOption::show_remaining_time>();
auto &saved_start_time = get_value<details::ProgressBarOption::saved_start_time>();
if ((show_elapsed_time || show_remaining_time) && !saved_start_time) {
start_time_point_ = std::chrono::high_resolution_clock::now();
saved_start_time = true;
}
}
void print_progress() {
std::lock_guard<std::mutex> lock{mutex_};
auto now = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(now - start_time_point_);
void _print_progress() {
std::unique_lock<std::mutex> lock{_mutex};
std::cout << termcolor::bold;
switch (_foreground_color) {
case Color::GREY:
std::cout << termcolor::grey;
break;
case Color::RED:
std::cout << termcolor::red;
break;
case Color::GREEN:
std::cout << termcolor::green;
break;
case Color::YELLOW:
std::cout << termcolor::yellow;
break;
case Color::BLUE:
std::cout << termcolor::blue;
break;
case Color::MAGENTA:
std::cout << termcolor::magenta;
break;
case Color::CYAN:
std::cout << termcolor::cyan;
break;
case Color::WHITE:
std::cout << termcolor::white;
break;
details::set_stream_color(std::cout, get_value<details::ProgressBarOption::foreground_color>());
std::cout << get_value<details::ProgressBarOption::prefix_text>();
if (get_value<details::ProgressBarOption::spinner_show>())
std::cout << get_value<details::ProgressBarOption::spinner_states>()
[index_ % get_value<details::ProgressBarOption::spinner_states>().size()];
if (get_value<details::ProgressBarOption::show_percentage>()) {
std::cout << " " << std::min(static_cast<size_t>(progress_), size_t(100)) << "%";
}
std::cout << _prefix_text;
if (_show_spinner)
std::cout << _states[_index % _states.size()];
if (_show_percentage) {
std::cout << " " << std::min(static_cast<size_t>(_progress), size_t(100)) << "%";
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
std::cout << " [";
details::write_duration(std::cout, elapsed);
}
if (_max_postfix_text_length == 0)
_max_postfix_text_length = 10;
std::cout << " " << _postfix_text << std::string(_max_postfix_text_length, ' ') << "\r";
if (get_value<details::ProgressBarOption::show_remaining_time>()) {
if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "<";
else
std::cout << " [";
auto eta = std::chrono::nanoseconds(
progress_ > 0 ? static_cast<long long>(elapsed.count() * 100 / progress_) : 0);
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
details::write_duration(std::cout, remaining);
std::cout << "]";
} else {
if (get_value<details::ProgressBarOption::show_elapsed_time>())
std::cout << "]";
}
if (get_value<details::ProgressBarOption::max_postfix_text_len>() == 0)
get_value<details::ProgressBarOption::max_postfix_text_len>() = 10;
std::cout << " " << get_value<details::ProgressBarOption::postfix_text>()
<< std::string(get_value<details::ProgressBarOption::max_postfix_text_len>(), ' ')
<< "\r";
std::cout.flush();
_index += 1;
if (_progress > 100.0) {
_completed = true;
index_ += 1;
if (progress_ > 100.0) {
get_value<details::ProgressBarOption::completed>() = true;
}
if (_completed)
if (get_value<details::ProgressBarOption::completed>())
std::cout << termcolor::reset << std::endl;
}
};

View File

@@ -0,0 +1,203 @@
/*
Activity Indicators for Modern C++
https://github.com/p-ranav/indicators
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2019 Dawid Pilarski <dawid.pilarski@panicsoftware.com>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <cstddef>
#include <indicators/color.hpp>
#include <string>
#include <type_traits>
#include <utility>
namespace indicators {
namespace details {
template <bool condition> struct if_else;
template <> struct if_else<true> { using type = std::true_type; };
template <> struct if_else<false> { using type = std::false_type; };
template <bool condition, typename True, typename False> struct if_else_type;
template <typename True, typename False> struct if_else_type<true, True, False> {
using type = True;
};
template <typename True, typename False> struct if_else_type<false, True, False> {
using type = False;
};
template <typename... Ops> struct conjuction;
template <> struct conjuction<> : std::true_type {};
template <typename Op, typename... TailOps>
struct conjuction<Op, TailOps...>
: if_else_type<!Op::value, std::false_type, conjuction<TailOps...>>::type {};
template <typename... Ops> struct disjunction;
template <> struct disjunction<> : std::false_type {};
template <typename Op, typename... TailOps>
struct disjunction<Op, TailOps...>
: if_else_type<Op::value, std::true_type, disjunction<TailOps...>>::type {};
enum class ProgressBarOption {
bar_width = 0,
prefix_text,
postfix_text,
start,
end,
fill,
lead,
remainder,
max_postfix_text_len,
completed,
show_percentage,
show_elapsed_time,
show_remaining_time,
saved_start_time,
foreground_color,
spinner_show,
spinner_states,
hide_bar_when_complete
};
template <typename T, ProgressBarOption Id> struct Setting {
template <typename... Args,
typename = typename std::enable_if<std::is_constructible<T, Args...>::value>::type>
explicit Setting(Args &&... args) : value(std::forward<Args>(args)...) {}
Setting(const Setting &) = default;
Setting(Setting &&) = default;
static constexpr auto id = Id;
using type = T;
T value{};
};
template <typename T> struct is_setting : std::false_type {};
template <ProgressBarOption Id, typename T> struct is_setting<Setting<T, Id>> : std::true_type {};
template <typename... Args>
struct are_settings : if_else<conjuction<is_setting<Args>...>::value>::type {};
template <> struct are_settings<> : std::true_type {};
template <typename Setting, typename Tuple> struct is_setting_from_tuple;
template <typename Setting> struct is_setting_from_tuple<Setting, std::tuple<>> : std::true_type {};
template <typename Setting, typename... TupleTypes>
struct is_setting_from_tuple<Setting, std::tuple<TupleTypes...>>
: if_else<disjunction<std::is_same<Setting, TupleTypes>...>::value>::type {};
template <typename Tuple, typename... Settings>
struct are_settings_from_tuple
: if_else<conjuction<is_setting_from_tuple<Settings, Tuple>...>::value>::type {};
template <ProgressBarOption Id> struct always_true { static constexpr auto value = true; };
template <ProgressBarOption Id, typename Default> Default &&get_impl(Default &&def) {
return std::forward<Default>(def);
}
template <ProgressBarOption Id, typename Default, typename T, typename... Args>
auto get_impl(Default &&def, T &&first, Args &&... tail) ->
typename std::enable_if<(std::decay<T>::type::id == Id),
decltype(std::forward<T>(first))>::type {
return std::forward<T>(first);
}
template <ProgressBarOption Id, typename Default, typename T, typename... Args>
auto get_impl(Default &&def, T &&first, Args &&... tail) ->
typename std::enable_if<(std::decay<T>::type::id != Id),
decltype(get_impl<Id>(std::forward<Default>(def),
std::forward<Args>(tail)...))>::type {
return get_impl<Id>(std::forward<Default>(def), std::forward<Args>(tail)...);
}
template <ProgressBarOption Id, typename Default, typename... Args,
typename = typename std::enable_if<are_settings<Args...>::value, void>::type>
auto get(Default &&def, Args &&... args)
-> decltype(details::get_impl<Id>(std::forward<Default>(def), std::forward<Args>(args)...)) {
return details::get_impl<Id>(std::forward<Default>(def), std::forward<Args>(args)...);
}
template <ProgressBarOption Id> using StringSetting = Setting<std::string, Id>;
template <ProgressBarOption Id> using IntegerSetting = Setting<std::size_t, Id>;
template <ProgressBarOption Id> using BooleanSetting = Setting<bool, Id>;
template <ProgressBarOption Id, typename Tuple, std::size_t counter = 0> struct option_idx;
template <ProgressBarOption Id, typename T, typename... Settings, std::size_t counter>
struct option_idx<Id, std::tuple<T, Settings...>, counter>
: if_else_type<(Id == T::id), std::integral_constant<std::size_t, counter>,
option_idx<Id, std::tuple<Settings...>, counter + 1>>::type {};
template <ProgressBarOption Id, std::size_t counter> struct option_idx<Id, std::tuple<>, counter> {
static_assert(always_true<(ProgressBarOption)Id>::value, "No such option was found");
};
template <ProgressBarOption Id, typename Settings>
auto get_value(Settings &&settings)
-> decltype((std::get<option_idx<Id, typename std::decay<Settings>::type>::value>(
std::declval<Settings &&>()))) {
return std::get<option_idx<Id, typename std::decay<Settings>::type>::value>(
std::forward<Settings>(settings));
}
} // namespace details
namespace option {
using BarWidth = details::IntegerSetting<details::ProgressBarOption::bar_width>;
using PrefixText = details::StringSetting<details::ProgressBarOption::prefix_text>;
using PostfixText = details::StringSetting<details::ProgressBarOption::postfix_text>;
using Start = details::StringSetting<details::ProgressBarOption::start>;
using End = details::StringSetting<details::ProgressBarOption::end>;
using Fill = details::StringSetting<details::ProgressBarOption::fill>;
using Lead = details::StringSetting<details::ProgressBarOption::lead>;
using Remainder = details::StringSetting<details::ProgressBarOption::remainder>;
using MaxPostfixTextLen = details::IntegerSetting<details::ProgressBarOption::max_postfix_text_len>;
using Completed = details::BooleanSetting<details::ProgressBarOption::completed>;
using ShowPercentage = details::BooleanSetting<details::ProgressBarOption::show_percentage>;
using ShowElapsedTime = details::BooleanSetting<details::ProgressBarOption::show_elapsed_time>;
using ShowRemainingTime = details::BooleanSetting<details::ProgressBarOption::show_remaining_time>;
using SavedStartTime = details::BooleanSetting<details::ProgressBarOption::saved_start_time>;
using ForegroundColor = details::Setting<Color, details::ProgressBarOption::foreground_color>;
using ShowSpinner = details::BooleanSetting<details::ProgressBarOption::spinner_show>;
using SpinnerStates =
details::Setting<std::vector<std::string>, details::ProgressBarOption::spinner_states>;
using HideBarWhenComplete =
details::BooleanSetting<details::ProgressBarOption::hide_bar_when_complete>;
} // namespace option
} // namespace indicators

View File

@@ -32,6 +32,9 @@
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
#include <unistd.h>
#elif defined(TERMCOLOR_OS_WINDOWS)
#if !defined(NOMINMAX)
#define NOMINMAX
#endif
#include <io.h>
#include <windows.h>
#endif

7
indicators.pc.in Normal file
View File

@@ -0,0 +1,7 @@
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
Name: @PROJECT_NAME@
Description: @PROJECT_DESCRIPTION@
URL: @PROJECT_HOMEPAGE_URL@
Version: @PROJECT_VERSION@
Cflags: -I${includedir}

View File

@@ -0,0 +1,9 @@
@PACKAGE_INIT@
include(CMakeFindDependencyMacro)
find_dependency(Threads REQUIRED)
if (NOT TARGET indicators::indicators)
include(${CMAKE_CURRENT_LIST_DIR}/indicatorsTargets.cmake)
endif ()

27
samples/CMakeLists.txt Normal file
View File

@@ -0,0 +1,27 @@
add_executable(block_progress_bar block_progress_bar.cpp)
target_link_libraries(block_progress_bar PRIVATE indicators::indicators)
add_executable(multi_threaded_bar multi_threaded_bar.cpp)
target_link_libraries(multi_threaded_bar PRIVATE indicators::indicators)
add_executable(progress_bar_set_progress progress_bar_set_progress.cpp)
target_link_libraries(progress_bar_set_progress PRIVATE indicators::indicators)
add_executable(progress_bar_tick progress_bar_tick.cpp)
target_link_libraries(progress_bar_tick PRIVATE indicators::indicators)
add_executable(progress_spinner progress_spinner.cpp)
target_link_libraries(progress_spinner PRIVATE indicators::indicators)
add_executable(time_meter time_meter.cpp)
target_link_libraries(time_meter PRIVATE indicators::indicators)
add_executable(multi_progress_bar multi_progress_bar.cpp)
target_link_libraries(multi_progress_bar PRIVATE indicators::indicators)
add_executable(multi_block_progress_bar multi_block_progress_bar.cpp)
target_link_libraries(multi_block_progress_bar PRIVATE indicators::indicators)
add_executable(dynamic_progress dynamic_progress.cpp)
target_link_libraries(dynamic_progress PRIVATE indicators::indicators)

View File

@@ -0,0 +1,26 @@
#include <chrono>
#include <indicators/block_progress_bar.hpp>
#include <thread>
int main() {
// Hide cursor
std::cout << "\e[?25l";
indicators::BlockProgressBar bar{indicators::option::BarWidth{80}};
// Update bar state
auto progress = 0.0f;
while (true) {
bar.set_progress(progress);
progress += 0.25f;
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
// Show cursor
std::cout << "\e[?25h";
return 0;
}

View File

@@ -0,0 +1,131 @@
#include <indicators/dynamic_progress.hpp>
#include <indicators/progress_bar.hpp>
using namespace indicators;
int main() {
ProgressBar bar1{option::BarWidth{50}, option::ForegroundColor{Color::red},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"5c90d4a2d1a8: Downloading "}};
ProgressBar bar2{option::BarWidth{50}, option::ForegroundColor{Color::yellow},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"22337bfd13a9: Downloading "}};
ProgressBar bar3{option::BarWidth{50}, option::ForegroundColor{Color::green},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"10f26c680a34: Downloading "}};
ProgressBar bar4{option::BarWidth{50}, option::ForegroundColor{Color::white},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"6364e0d7a283: Downloading "}};
ProgressBar bar5{option::BarWidth{50}, option::ForegroundColor{Color::blue},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"ff1356ba118b: Downloading "}};
ProgressBar bar6{option::BarWidth{50}, option::ForegroundColor{Color::cyan},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"5a17453338b4: Downloading "}};
std::cout << termcolor::bold << termcolor::white << "Pulling image foo:bar/baz\n";
DynamicProgress<ProgressBar> bars(bar1, bar2, bar3);
bars.set_option(option::HideBarWhenComplete{false});
std::thread fourth_job, fifth_job, sixth_job;
auto job4 = [&bars](size_t i) {
while (true) {
bars[i].tick();
if (bars[i].is_completed()) {
bars[i].set_option(option::PrefixText{"6364e0d7a283: Pull complete "});
bars[i].mark_as_completed();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
};
auto job5 = [&bars](size_t i) {
while (true) {
bars[i].tick();
if (bars[i].is_completed()) {
bars[i].set_option(option::PrefixText{"ff1356ba118b: Pull complete "});
bars[i].mark_as_completed();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
};
auto job6 = [&bars](size_t i) {
while (true) {
bars[i].tick();
if (bars[i].is_completed()) {
bars[i].set_option(option::PrefixText{"5a17453338b4: Pull complete "});
bars[i].mark_as_completed();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(40));
}
};
auto job1 = [&bars, &bar6, &sixth_job, &job6]() {
while (true) {
bars[0].tick();
if (bars[0].is_completed()) {
bars[0].set_option(option::PrefixText{"5c90d4a2d1a8: Pull complete "});
// bar1 is completed, adding bar6
auto i = bars.push_back(bar6);
sixth_job = std::thread(job6, i);
sixth_job.join();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(140));
}
};
auto job2 = [&bars, &bar5, &fifth_job, &job5]() {
while (true) {
bars[1].tick();
if (bars[1].is_completed()) {
bars[1].set_option(option::PrefixText{"22337bfd13a9: Pull complete "});
// bar2 is completed, adding bar5
auto i = bars.push_back(bar5);
fifth_job = std::thread(job5, i);
fifth_job.join();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(25));
}
};
auto job3 = [&bars, &bar4, &fourth_job, &job4]() {
while (true) {
bars[2].tick();
if (bars[2].is_completed()) {
bars[2].set_option(option::PrefixText{"10f26c680a34: Pull complete "});
// bar3 is completed, adding bar4
auto i = bars.push_back(bar4);
fourth_job = std::thread(job4, i);
fourth_job.join();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
};
std::thread first_job(job1);
std::thread second_job(job2);
std::thread third_job(job3);
third_job.join();
second_job.join();
first_job.join();
std::cout << termcolor::bold << termcolor::green << "✔ Downloaded image foo/bar:baz" << std::endl;
std::cout << termcolor::reset;
return 0;
}

View File

@@ -0,0 +1,58 @@
#include <indicators/block_progress_bar.hpp>
#include <indicators/multi_progress.hpp>
int main() {
using namespace indicators;
BlockProgressBar bar1{option::BarWidth{50}, option::ForegroundColor{Color::yellow},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"Progress Bar #1 "}};
BlockProgressBar bar2{option::BarWidth{50}, option::ForegroundColor{Color::cyan},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"Progress Bar #2 "}};
BlockProgressBar bar3{option::BarWidth{50}, option::ForegroundColor{Color::red},
option::ShowElapsedTime{true}, option::ShowRemainingTime{true},
option::PrefixText{"Progress Bar #3 "}};
indicators::MultiProgress<indicators::BlockProgressBar, 3> bars(bar1, bar2, bar3);
std::cout << "Multiple Progress Bars:\n";
auto job1 = [&bars]() {
while (true) {
bars.tick<0>();
if (bars.is_completed<0>())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
};
auto job2 = [&bars]() {
while (true) {
bars.tick<1>();
if (bars.is_completed<1>())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
};
auto job3 = [&bars]() {
while (true) {
bars.tick<2>();
if (bars.is_completed<2>())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(60));
}
};
std::thread first_job(job1);
std::thread second_job(job2);
std::thread third_job(job3);
first_job.join();
second_job.join();
third_job.join();
return 0;
}

View File

@@ -0,0 +1,79 @@
#include <indicators/multi_progress.hpp>
#include <indicators/progress_bar.hpp>
int main() {
indicators::ProgressBar bar1{indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{""},
indicators::option::Lead{""},
indicators::option::Remainder{" "},
indicators::option::End{" ]"},
indicators::option::ForegroundColor{indicators::Color::yellow},
indicators::option::ShowElapsedTime{true},
indicators::option::ShowRemainingTime{true},
indicators::option::PrefixText{"Progress Bar #1 "}};
indicators::ProgressBar bar2{indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{"="},
indicators::option::Lead{">"},
indicators::option::Remainder{" "},
indicators::option::End{" ]"},
indicators::option::ForegroundColor{indicators::Color::cyan},
indicators::option::ShowElapsedTime{true},
indicators::option::ShowRemainingTime{true},
indicators::option::PrefixText{"Progress Bar #2 "}};
indicators::ProgressBar bar3{indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{"#"},
indicators::option::Lead{"#"},
indicators::option::Remainder{" "},
indicators::option::End{" ]"},
indicators::option::ForegroundColor{indicators::Color::red},
indicators::option::ShowElapsedTime{true},
indicators::option::ShowRemainingTime{true},
indicators::option::PrefixText{"Progress Bar #3 "}};
indicators::MultiProgress<indicators::ProgressBar, 3> bars(bar1, bar2, bar3);
std::cout << "Multiple Progress Bars:\n";
auto job1 = [&bars]() {
while (true) {
bars.tick<0>();
if (bars.is_completed<0>())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
};
auto job2 = [&bars]() {
while (true) {
bars.tick<1>();
if (bars.is_completed<1>())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
};
auto job3 = [&bars]() {
while (true) {
bars.tick<2>();
if (bars.is_completed<2>())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(60));
}
};
std::thread first_job(job1);
std::thread second_job(job2);
std::thread third_job(job3);
first_job.join();
second_job.join();
third_job.join();
return 0;
}

View File

@@ -0,0 +1,61 @@
#include <indicators/progress_bar.hpp>
#include <vector>
int main() {
indicators::ProgressBar bar{
indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{""},
indicators::option::Lead{""},
indicators::option::Remainder{"-"},
indicators::option::End{" ]"},
indicators::option::ForegroundColor{indicators::Color::yellow},
};
// As configured, the bar will look like this:
//
// [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-------------] 70%
//
//
std::atomic<size_t> index{0};
std::vector<std::string> status_text = {"Rocket.exe is not responding",
"Finding a replacement engineer",
"Buying more snacks",
"Assimilating the modding community",
"Crossing fingers",
"Porting KSP to a Nokia 3310"};
// Let's say you want to append some status text to the right of the progress bar
// You can use bar.set_postfix_text(...) to append text to the right
//
// [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-------------] 70% Finding a replacement engineer
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
//
auto job = [&bar, &index, &status_text]() {
while (true) {
if (bar.is_completed()) {
break;
}
bar.set_option(indicators::option::PostfixText{status_text[index % status_text.size()]});
bar.tick();
index += 1;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
};
std::thread first_job(job);
std::thread second_job(job);
std::thread third_job(job);
std::thread last_job(job);
first_job.join();
second_job.join();
third_job.join();
last_job.join();
return 0;
}

View File

@@ -0,0 +1,51 @@
#include <chrono>
#include <indicators/progress_bar.hpp>
#include <thread>
int main() {
// Hide cursor
std::cout << "\e[?25l";
indicators::ProgressBar bar{
indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{""},
indicators::option::Lead{""},
indicators::option::Remainder{"-"},
indicators::option::End{" ]"},
indicators::option::PostfixText{"Loading dependency 1/4"},
indicators::option::ForegroundColor{indicators::Color::cyan},
};
// Update bar state
bar.set_progress(10); // 10% done
// do some work
std::this_thread::sleep_for(std::chrono::milliseconds(800));
bar.set_option(indicators::option::PostfixText{"Loading dependency 2/4"});
bar.set_progress(30); // 30% done
// do some more work
std::this_thread::sleep_for(std::chrono::milliseconds(700));
bar.set_option(indicators::option::PostfixText{"Loading dependency 3/4"});
bar.set_progress(65); // 65% done
// do final bit of work
std::this_thread::sleep_for(std::chrono::milliseconds(900));
bar.set_option(indicators::option::PostfixText{"Loaded dependencies!"});
bar.set_progress(100); // all done
bar.mark_as_completed();
// Show cursor
std::cout << "\e[?25h";
return 0;
}

View File

@@ -0,0 +1,26 @@
#include <chrono>
#include <indicators/progress_bar.hpp>
#include <thread>
int main() {
indicators::ProgressBar bar{
indicators::option::BarWidth{50},
indicators::option::Start{"["},
indicators::option::Fill{"="},
indicators::option::Lead{">"},
indicators::option::Remainder{" "},
indicators::option::End{" ]"},
indicators::option::PostfixText{"Getting started"},
indicators::option::ForegroundColor{indicators::Color::green},
};
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
return 0;
}

View File

@@ -0,0 +1,38 @@
#include <indicators/progress_spinner.hpp>
int main() {
// Hide cursor
std::cout << "\e[?25l";
indicators::ProgressSpinner spinner{
indicators::option::PostfixText{"Checking credentials"},
indicators::option::ForegroundColor{indicators::Color::yellow},
indicators::option::SpinnerStates{
std::vector<std::string>{"", "", "", "", "", "", "", ""}},
};
// Update spinner state
auto job = [&spinner]() {
while (true) {
if (spinner.is_completed()) {
spinner.set_option(indicators::option::ForegroundColor{indicators::Color::green});
spinner.set_option(indicators::option::PrefixText{""});
spinner.set_option(indicators::option::ShowSpinner{false});
spinner.set_option(indicators::option::ShowPercentage{false});
spinner.set_option(indicators::option::PostfixText{"Authenticated!"});
spinner.mark_as_completed();
break;
} else
spinner.tick();
std::this_thread::sleep_for(std::chrono::milliseconds(40));
}
};
std::thread thread(job);
thread.join();
// Show cursor
std::cout << "\e[?25h";
return 0;
}

31
samples/time_meter.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include <chrono>
#include <indicators/progress_bar.hpp>
#include <thread>
int main() {
indicators::ProgressBar bar{
indicators::option::BarWidth{50},
indicators::option::Start{" ["},
indicators::option::Fill{""},
indicators::option::Lead{""},
indicators::option::Remainder{"-"},
indicators::option::End{"]"},
indicators::option::PrefixText{"Training Gaze Network 👀"},
indicators::option::ForegroundColor{indicators::Color::yellow},
indicators::option::ShowElapsedTime{true},
indicators::option::ShowRemainingTime{true},
};
// Update bar state
while (true) {
bar.tick();
if (bar.is_completed())
break;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
// Show cursor
std::cout << "\e[?25h";
return 0;
}

File diff suppressed because it is too large Load Diff