Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit 731e77e

Browse files
Allow printing colored text
1 parent 7822512 commit 731e77e

File tree

7 files changed

+52
-15
lines changed

7 files changed

+52
-15
lines changed

src/commands/eval.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ class EvalCommand : public liteshell::BaseCommand
2626
auto input = context.get("expression");
2727
if (context.present.count("-p"))
2828
{
29-
input = context.client->get_stream()->getline(input, liteshell::InputStream::FORCE_STDOUT | liteshell::InputStream::FORCE_STDIN);
29+
input = context.client->get_stream()->getline(
30+
[&input]()
31+
{ std::cout << input << std::flush; },
32+
liteshell::InputStream::FORCE_STDOUT | liteshell::InputStream::FORCE_STDIN);
3033
}
3134

3235
auto result = context.present.count("-m") ? std::to_string(context.client->get_environment()->eval_ll(input)) : input;

src/commands/for.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ class ForCommand : public liteshell::BaseCommand
3434
bool force_stream = !context.client->get_stream()->exhaust();
3535
while (true)
3636
{
37-
auto input = utils::strip(context.client->get_stream()->getline("for>", force_stream ? liteshell::InputStream::FORCE_STREAM : 0));
37+
auto input = utils::strip(context.client->get_stream()->getline(
38+
[]()
39+
{ std::cout << "for>" << std::flush; },
40+
force_stream ? liteshell::InputStream::FORCE_STREAM : 0));
3841
if (utils::startswith(input, "for "))
3942
{
4043
counter++;

src/commands/if.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ class IfCommand : public liteshell::BaseCommand
3131
bool has_else = false;
3232
while (true)
3333
{
34-
auto input = utils::strip(context.client->get_stream()->getline("if_true>", force_stream ? liteshell::InputStream::FORCE_STREAM : 0));
34+
auto input = utils::strip(context.client->get_stream()->getline(
35+
[]()
36+
{ std::cout << "if_true>" << std::flush; },
37+
force_stream ? liteshell::InputStream::FORCE_STREAM : 0));
3538
if (utils::startswith(input, "if "))
3639
{
3740
counter++;
@@ -63,7 +66,10 @@ class IfCommand : public liteshell::BaseCommand
6366
{
6467
while (true)
6568
{
66-
auto input = utils::strip(context.client->get_stream()->getline("if_false>", force_stream ? liteshell::InputStream::FORCE_STREAM : 0));
69+
auto input = utils::strip(context.client->get_stream()->getline(
70+
[]()
71+
{ std::cout << "if_false>" << std::flush; },
72+
force_stream ? liteshell::InputStream::FORCE_STREAM : 0));
6773
if (utils::startswith(input, "if "))
6874
{
6975
counter++;

src/include/all.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "standard.hpp"
1717
#include "stream.hpp"
1818
#include "strip.hpp"
19+
#include "style.hpp"
1920
#include "subprocess.hpp"
2021
#include "tables.hpp"
2122
#include "units.hpp"

src/include/client.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "fuzzy_search.hpp"
77
#include "maps.hpp"
88
#include "stream.hpp"
9+
#include "style.hpp"
910
#include "subprocess.hpp"
1011
#include "wrapper.hpp"
1112

@@ -392,16 +393,16 @@ namespace liteshell
392393
utils::set_ignore_ctrl_c(true);
393394
while (true)
394395
{
395-
SYSTEMTIME time;
396-
GetLocalTime(&time);
397396
process_command(
398397
_stream->getline(
399-
utils::format(
400-
"\n[%d:%d:%d]liteshell~%s>",
401-
time.wHour,
402-
time.wMinute,
403-
time.wSecond,
404-
utils::get_working_directory().c_str()),
398+
[]()
399+
{
400+
SYSTEMTIME time;
401+
GetLocalTime(&time);
402+
std::cout << utils::format("\n[%d:%d:%d]", time.wHour, time.wMinute, time.wSecond);
403+
utils::style_print("liteshell~", FOREGROUND_BLUE | FOREGROUND_INTENSITY);
404+
std::cout << utils::get_working_directory() << ">";
405+
},
405406
0));
406407
}
407408
}

src/include/stream.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ namespace liteshell
8282
/**
8383
* @brief Read the next command
8484
*
85-
* @param prompt The prompt to display before reading
85+
* @param prompt The function to display the prompt string before reading
8686
* @param flags The flags to use when reading the command
8787
* @return The next command in the input stream
8888
*/
89-
std::string getline(const std::string &prompt, const int flags)
89+
std::string getline(const std::function<void()> &prompt, const int flags)
9090
{
9191
#ifdef DEBUG
9292
std::cout << "Received getline request, flags = " << flags << std::endl;
@@ -111,7 +111,7 @@ namespace liteshell
111111

112112
if ((flags & FORCE_STDOUT) || (echo && peek_echo()))
113113
{
114-
std::cout << prompt << std::flush;
114+
prompt();
115115
}
116116

117117
bool from_stdin = (flags & FORCE_STDIN) || exhaust();

src/include/style.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include "standard.hpp"
4+
5+
namespace utils
6+
{
7+
template <typename T>
8+
void style_print(const T &message, const WORD &attributes)
9+
{
10+
const auto console = GetStdHandle(STD_OUTPUT_HANDLE);
11+
12+
CONSOLE_SCREEN_BUFFER_INFO current;
13+
const auto has_old_attr = GetConsoleScreenBufferInfo(console, &current);
14+
15+
SetConsoleTextAttribute(console, attributes); // Ignore failure
16+
std::cout << message;
17+
18+
if (has_old_attr)
19+
{
20+
SetConsoleTextAttribute(console, current.wAttributes);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)