Skip to content

Commit

Permalink
Errno (#327)
Browse files Browse the repository at this point in the history
* Add errno exception

First PR needed to have (in later PR) a better error check and to allow to restore the terminal in good state even if the user doesn't wrap the terminal stufs inside try catch.
  • Loading branch information
flagarde committed Nov 6, 2023
1 parent 2413c3c commit a77db3f
Show file tree
Hide file tree
Showing 23 changed files with 546 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
Checks: 'abseil-*,altera-*,android-*,boost-*,bugprone-*,cert-*,clang-analyzer-*,concurrency-*,cppcoreguidelines-*,darwin-*,fuchsia-*,google-*,hicpp-*,linuxkernel-*,llvm-*,misc-*,modernize-*,mpi-*,openmp-*,performance-*,portability-*,readability-*,zircon-*,-modernize-use-trailing-return-type,-fuchsia-overloaded-operator,-fuchsia-default-arguments-calls,-altera-unroll-loops'
Checks: 'abseil-*,altera-*,android-*,boost-*,bugprone-*,cert-*,clang-analyzer-*,concurrency-*,cppcoreguidelines-*,darwin-*,fuchsia-*,google-*,hicpp-*,linuxkernel-*,llvm-*,misc-*,modernize-*,mpi-*,openmp-*,performance-*,portability-*,readability-*,zircon-*,-modernize-use-trailing-return-type,-fuchsia-overloaded-operator,-fuchsia-default-arguments-calls,-altera-unroll-loops,-cppcoreguidelines-avoid-non-const-global-variables,-modernize-pass-by-value'
WarningsAsErrors: false
...
76 changes: 33 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,40 @@
[![pre-commit](https://github.com/jupyter-xeus/cpp-terminal/actions/workflows/pre-commit-check.yml/badge.svg)](https://github.com/jupyter-xeus/cpp-terminal/actions/workflows/pre-commit-check.yml)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/jupyter-xeus/cpp-terminal/master.svg)](https://results.pre-commit.ci/latest/github/jupyter-xeus/cpp-terminal/maset)

`CPP-Terminal` is a small and dependency-free C++ library for writing platform independent terminal-based applications. It follows the "Zero-overhead principle" and limits externally included files to the C++ STL. Being crossplatform we are currently supporting Windows, Linux and MacOS and are providing an unified API across all platforms. Our main features are consisting of Colors, Keyboard input, terminal resize handling, as well as other common terminal functionality. It's also possible to open a managed terminal from a windows GUI application.
`CPP-Terminal` is a small and dependency-free C++ library for writing platform independent terminal-based applications. It follows the "Zero-overhead principle" and limits externally included files to the C++ STL. Being cross-platform we are currently supporting Windows, Linux and MacOS and are providing an unified API across all platforms. Our main features are consisting of Colors, Keyboard input, terminal resize handling, as well as other common terminal functionality. It's also possible to open a managed terminal from a windows GUI application.

## Hello World example

To write a simple Hello World program, all you need to do is:

```cpp
#include "cpp-terminal/io.hpp"

#include "cpp-terminal/terminal.hpp"
#include <iostream>

int main()
{
std::cout << "\033[31mHello world !\033[0m" << std::endl;
return 0;
std::cout << "Just including terminal.hpp activate \033[31mcolor\033[0m !" << std::endl;
}
```

or

```cpp
#include "cpp-terminal/io.hpp"
#include "cpp-terminal/terminal.hpp"
#include "cpp-terminal/color.hpp"

#include <iostream>

int main()
{
std::cout << Term::color_fg(Term::Color::Name::Red)<<"Hello world !"<<color_fg(Term::Color::Name::Default)<< std::endl;
return 0;
}
```

On windows you can simply create or attach a console through a GUI application by doing:

```cpp
#include "cpp-terminal/io.hpp"
#include "cpp-terminal/terminal.hpp"
#include "cpp-terminal/color.hpp"

#include <iostream>
#include <windows.h>

Expand All @@ -60,22 +55,17 @@ int __stdcall WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int show)
Until 2021, CPP-Terminal used to be a single header library. Now, CPP-Terminal consists out of multiple small and usage oriented headers:
- `cpp-terminal/base.hpp`: everything for basic Terminal control
- `cpp-terminal/input.hpp`: functions for gathering input
- `cpp-terminal/prompt.hpp`: some variations of different prompts
- `cpp-terminal/window.hpp`: a fully managed terminal window for terminal user interfaces (TUI)
- `cpp-terminal/version.hpp`: macros with cpp-terminal's version number
The library uses private header for platform dependent code:
- `cpp-terminal/private/conversion.hpp`: Various conversions
- `cpp-terminal/private/platform.hpp`: platform dependent code
CPP-Terminal tries to be a small and simple replacement for ncurses. This approach keeps the code small and maintainable, but also easy to extend it's functionality. We limit ourselves to a subset of features that work on all supported platforms without needing to worry about style differences or other changes. Any application written with `CPP-Terminal` will work everywhere out of the box natively, without emulation or extra work. The small codebase makes CPP-Terminal easy to debug and extend, as well as understanding what happens behind the scenes in the library's core.
## Examples
We have created serval examples to show possible use cases of CPP-Terminal and to get you started more quickly. Every example works natively on all platforms in the exact same way:
- [colors.cpp](examples/colors.cpp): basic color, style and unicode demo
- [kilo.cpp](examples/kilo.cpp): the [kilo](https://github.com/snaptoken/kilo-src) text editor
ported to C++ and `CPP-Terminal` instead of using Linux specific API
Expand All @@ -102,7 +92,7 @@ We have created serval examples to show possible use cases of CPP-Terminal and t
Adding CPP-Terminal to your own project is really easy. We have collected various ways with easy
how-to's [in our documentation](https://github.com/jupyter-xeus/cpp-terminal/wiki/Adding-CPP-Terminal-to-your-ptoject).
## Documentation [![docs](https://github.com/jupyter-xeus/cpp-terminal/actions/workflows/docs.yml/badge.svg)](https://github.com/jupyter-xeus/cpp-terminal/actions/workflows/docs.yml) ##
## Documentation [![docs](https://github.com/jupyter-xeus/cpp-terminal/actions/workflows/docs.yml/badge.svg)](https://github.com/jupyter-xeus/cpp-terminal/actions/workflows/docs.yml)
<h1 align="center">
<a href="https://jupyter-xeus.github.io/cpp-terminal/">
Expand Down Expand Up @@ -133,7 +123,7 @@ of [the MIT License](https://github.com/jupyter-xeus/cpp-terminal/blob/master/LI

## Projects using cpp-terminal

* [Lime](https://github.com/TobiasWallner/Lime)
- [Lime](https://github.com/TobiasWallner/Lime)

## Similar Projects

Expand All @@ -143,59 +133,59 @@ Libraries to handle color output.

C++:

* [rang](https://github.com/agauniyal/rang)
- [rang](https://github.com/agauniyal/rang)

### Drawing

JavaScript:

* [node-drawille](https://github.com/madbence/node-drawille)
- [node-drawille](https://github.com/madbence/node-drawille)

### Prompt

Libraries to handle a prompt in terminals.

C and C++:

* [readline](https://tiswww.case.edu/php/chet/readline/rltop.html)
* [libedit](http://thrysoee.dk/editline/)
* [linenoise](https://github.com/antirez/linenoise)
* [replxx](https://github.com/AmokHuginnsson/replxx)
- [readline](https://tiswww.case.edu/php/chet/readline/rltop.html)
- [libedit](http://thrysoee.dk/editline/)
- [linenoise](https://github.com/antirez/linenoise)
- [replxx](https://github.com/AmokHuginnsson/replxx)

Python:

* [python-prompt-toolkit](https://github.com/prompt-toolkit/python-prompt-toolkit)
- [python-prompt-toolkit](https://github.com/prompt-toolkit/python-prompt-toolkit)

### General TUI libraries

C and C++:

* [curses](https://en.wikipedia.org/wiki/Curses_%28programming_library%29) and [ncurses](https://www.gnu.org/software/ncurses/ncurses.html)
* [Newt](https://en.wikipedia.org/wiki/Newt_(programming_library))
* [termbox](https://github.com/nsf/termbox)
* [FTXUI](https://github.com/ArthurSonzogni/FTXUI)
* [ImTui](https://github.com/ggerganov/imtui)
- [curses](https://en.wikipedia.org/wiki/Curses_%28programming_library%29) and [ncurses](https://www.gnu.org/software/ncurses/ncurses.html)
- [Newt](https://en.wikipedia.org/wiki/Newt_(programming_library))
- [termbox](https://github.com/nsf/termbox)
- [FTXUI](https://github.com/ArthurSonzogni/FTXUI)
- [ImTui](https://github.com/ggerganov/imtui)

Python:

* [urwid](http://urwid.org/)
* [python-prompt-toolkit](https://github.com/prompt-toolkit/python-prompt-toolkit)
* [npyscreen](http://www.npcole.com/npyscreen/)
* [curtsies](https://github.com/bpython/curtsies)
- [urwid](http://urwid.org/)
- [python-prompt-toolkit](https://github.com/prompt-toolkit/python-prompt-toolkit)
- [npyscreen](http://www.npcole.com/npyscreen/)
- [curtsies](https://github.com/bpython/curtsies)

Go:

* [gocui](https://github.com/jroimartin/gocui)
* [clui](https://github.com/VladimirMarkelov/clui)
* [tview](https://github.com/rivo/tview)
* [termbox-go](https://github.com/nsf/termbox-go)
* [termui](https://github.com/gizak/termui)
* [tcell](https://github.com/gdamore/tcell)
- [gocui](https://github.com/jroimartin/gocui)
- [clui](https://github.com/VladimirMarkelov/clui)
- [tview](https://github.com/rivo/tview)
- [termbox-go](https://github.com/nsf/termbox-go)
- [termui](https://github.com/gizak/termui)
- [tcell](https://github.com/gdamore/tcell)

Rust:

* [tui-rs](https://github.com/fdehau/tui-rs)
- [tui-rs](https://github.com/fdehau/tui-rs)

JavaScript:

* [blessed](https://github.com/chjj/blessed) and [blessed-contrib](https://github.com/yaronn/blessed-contrib)
- [blessed](https://github.com/chjj/blessed) and [blessed-contrib](https://github.com/yaronn/blessed-contrib)
32 changes: 24 additions & 8 deletions cpp-terminal/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <cstdint>
#include <exception>
#include <stdexcept>
#include <string>

namespace Term
Expand All @@ -20,15 +19,32 @@ namespace Term
class Exception : public std::exception
{
public:
Exception(const std::string& what) : m_what(what) {}
Exception(const std::int64_t& code, const std::string& what) : m_what(what), m_code(code) {}
virtual const char* what() const noexcept override { return m_what.c_str(); }
std::int64_t code() const noexcept { return m_code; }
virtual ~Exception() = default;
explicit Exception(const std::string& message) noexcept;
Exception(const std::int64_t& code, const std::string& message) noexcept;
Exception(const Exception&) = default;
Exception(Exception&&) = default;
Exception& operator=(Exception&&) = default;
Exception& operator=(const Exception&) = default;

const char* what() const noexcept override;
std::int64_t code() const noexcept;
std::string message() const noexcept;
std::string context() const noexcept;
~Exception() noexcept override = default;

protected:
std::string m_what;
std::int64_t m_code{0};
explicit Exception(const std::int64_t& code) noexcept;
virtual void build_what() const noexcept;
void setMessage(const std::string& message) noexcept;
void setContext(const std::string& context) noexcept;
void setWhat(const std::string& what) noexcept;
static const constexpr std::size_t m_maxSize{256};

private:
std::int64_t m_code{0};
std::string m_message;
std::string m_context;
mutable std::string m_what;
};

} // namespace Term
2 changes: 1 addition & 1 deletion cpp-terminal/platforms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads)
add_library(cpp-terminal-platforms STATIC exception.cpp unicode.cpp conversion.cpp args.cpp terminal.cpp tty.cpp terminfo.cpp input.cpp screen.cpp cursor.cpp file.cpp env.cpp blocking_queue.cpp sigwinch.cpp)
add_library(cpp-terminal-platforms STATIC return_code.cpp file_initializer.cpp exception.cpp unicode.cpp conversion.cpp args.cpp terminal.cpp tty.cpp terminfo.cpp input.cpp screen.cpp cursor.cpp file.cpp env.cpp blocking_queue.cpp sigwinch.cpp)
target_link_libraries(cpp-terminal-platforms PRIVATE Warnings::Warnings PUBLIC Threads::Threads)
target_compile_options(cpp-terminal-platforms PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/utf-8 /wd4668 /wd4514>)
target_include_directories(cpp-terminal-platforms PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}> $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> $<INSTALL_INTERFACE:include>)
Expand Down
14 changes: 7 additions & 7 deletions cpp-terminal/platforms/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ namespace Term
namespace Private
{

/**
* @brief Value of an environment variables.
*
* @param env environment variable.
* @return std::pair<bool, std::string> with bool:true if the environment variable is set and std::string to the value of environment variable.
* @warning Internal use only.
*/
///
/// @brief Value of an environment variables.
///
/// @param env environment variable.
/// @return std::pair<bool, std::string> with bool:true if the environment variable is set and std::string to the value of environment variable.
/// @warning Internal use only.
///
std::pair<bool, std::string> getenv(const std::string& env);

} // namespace Private
Expand Down
Loading

0 comments on commit a77db3f

Please sign in to comment.