Freeciv21
Develop your civilization from humble roots to a global empire
luaconsole_common.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2021 Freeciv21 and Freeciv contributors. This file is
3  part of Freeciv21. Freeciv21 is free software: you can
4  ^oo^ redistribute it and/or modify it under the terms of the GNU
5  (..) General Public License as published by the Free Software
6  () () Foundation, either version 3 of the License, or (at your
7  ()__() option) any later version. You should have received
8  a copy of the GNU General Public License along with Freeciv21. If not,
9  see https://www.gnu.org/licenses/.
10  */
11 
12 #include <cstdarg>
13 // utility
14 #include "fcintl.h"
15 
16 // common
17 #include "featured_text.h"
18 
19 // include
20 #include "luaconsole_g.h"
21 
22 // client
23 #include "luaconsole_common.h"
24 
29 void luaconsole_append(const struct ft_color color,
30  const char *featured_text)
31 {
32  char plain_text[MAX_LEN_MSG];
33  struct text_tag_list *tags;
34 
35  // Separate the text and the tags.
36  featured_text_to_plain_text(featured_text, plain_text, sizeof(plain_text),
37  &tags, true);
38 
39  if (ft_color_requested(color)) {
40  // A color is requested.
41  struct text_tag *ptag =
43 
44  if (ptag) {
45  // Prepends to the list, to avoid to overwrite inside colors.
46  text_tag_list_prepend(tags, ptag);
47  } else {
48  qCritical(
49  "Failed to create a color text tag (fg = %s, bg = %s).",
50  (nullptr != color.foreground ? color.foreground : "nullptr"),
51  (nullptr != color.background ? color.background : "nullptr"));
52  }
53  }
54 
55  real_luaconsole_append(plain_text, tags);
56  text_tag_list_destroy(tags);
57 }
58 
63 void luaconsole_vprintf(const struct ft_color color, const char *format,
64  va_list args)
65 {
66  char featured_text[MAX_LEN_MSG];
67 
68  fc_vsnprintf(featured_text, sizeof(featured_text), format, args);
69  luaconsole_append(color, featured_text);
70 }
71 
76 void luaconsole_printf(const struct ft_color color, const char *format, ...)
77 {
78  va_list args;
79 
80  va_start(args, format);
81  luaconsole_vprintf(color, format, args);
82  va_end(args);
83 }
84 
88 void luaconsole_event(const char *plain_text,
89  const struct text_tag_list *tags)
90 {
91  real_luaconsole_append(plain_text, tags);
92 }
93 
98 {
99  luaconsole_append(ftc_any, _("This is the Client Lua Console."));
100 }
#define _(String)
Definition: fcintl.h:50
size_t featured_text_to_plain_text(const char *featured_text, char *plain_text, size_t plain_text_len, struct text_tag_list **tags, bool replace_link_text)
Separate the text from the text features.
struct text_tag * text_tag_new(enum text_tag_type tag_type, ft_offset_t start_offset, ft_offset_t stop_offset,...)
Returns a new text_tag or nullptr on error.
const struct ft_color ftc_any
#define FT_OFFSET_UNSET
Definition: featured_text.h:95
static bool ft_color_requested(const struct ft_color color)
@ TTT_COLOR
void luaconsole_vprintf(const struct ft_color color, const char *format, va_list args)
Add a line of text to the output ("chatline") window.
void luaconsole_event(const char *plain_text, const struct text_tag_list *tags)
Add a line of text to the output ("chatline") window from server event.
void luaconsole_append(const struct ft_color color, const char *featured_text)
Add a line of text to the output ("chatline") window, like puts() would do it in the console.
void luaconsole_printf(const struct ft_color color, const char *format,...)
Add a line of text to the output ("chatline") window.
void luaconsole_welcome_message()
Standard welcome message.
void real_luaconsole_append(const char *astring, const struct text_tag_list *tags)
Appends the string to the chat output window.
Definition: luaconsole.cpp:48
#define MAX_LEN_MSG
Definition: packets.h:37
struct text_tag::@23::@25 color
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition: support.cpp:512