Skip to content

Commit

Permalink
[prompt] restore alt value
Browse files Browse the repository at this point in the history
  • Loading branch information
tstack committed Feb 20, 2025
1 parent 7a6751f commit afa6b79
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 224 deletions.
4 changes: 3 additions & 1 deletion src/base/attr_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ attr_line_t::from_table_cell_content(const string_fragment& content,
static constexpr auto LF_SYMBOL = "\u240a"sv;
static constexpr auto CR_SYMBOL = "\u240d"sv;
static constexpr auto REP_SYMBOL = "\ufffd"sv;
static const std::string ELLIPSIS = "\u22ef";
static constexpr auto ELLIPSIS = "\u22ef"_frag;

auto has_ansi = false;
size_t char_width = 0;
Expand Down Expand Up @@ -191,6 +191,7 @@ attr_line_t&
attr_line_t::with_ansi_string(const std::string& str)
{
this->al_string = str;
this->al_attrs.clear();
scrub_ansi_string(this->al_string, &this->al_attrs);

return *this;
Expand All @@ -200,6 +201,7 @@ attr_line_t&
attr_line_t::with_ansi_string(const string_fragment& str)
{
this->al_string = str.to_string();
this->al_attrs.clear();
scrub_ansi_string(this->al_string, &this->al_attrs);

return *this;
Expand Down
48 changes: 48 additions & 0 deletions src/base/attr_line.hh
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,19 @@ public:
return *this;
}

attr_line_t& append(const std::pair<string_fragment, role_t> value)
{
size_t start_len = this->al_string.length();

this->al_string.append(value.first.data(), value.first.length());

line_range lr{(int) start_len, (int) this->al_string.length()};

this->al_attrs.emplace_back(lr, VC_ROLE.value(value.second));

return *this;
}

template<typename S>
attr_line_t& append_quoted(const std::pair<S, string_attr_pair>& value)
{
Expand Down Expand Up @@ -469,6 +482,24 @@ public:
return *this;
}

attr_line_t& insert(size_t index, const std::string& str)
{
this->al_string.insert(index, str.data(), str.length());

shift_string_attrs(this->al_attrs, index, str.length());

return *this;
}

attr_line_t& insert(size_t index, string_fragment str)
{
this->al_string.insert(index, str.data(), str.length());

shift_string_attrs(this->al_attrs, index, str.length());

return *this;
}

template<typename S>
attr_line_t& insert(size_t index,
const std::pair<S, string_attr_pair>& value)
Expand All @@ -487,6 +518,23 @@ public:
return *this;
}

attr_line_t& insert(size_t index,
const std::pair<string_fragment, role_t>& value)
{
size_t start_len = this->al_string.length();

this->insert(index, value.first);

line_range lr{
(int) index,
(int) (index + (this->al_string.length() - start_len)),
};

this->al_attrs.emplace_back(lr, VC_ROLE.value(value.second));

return *this;
}

template<typename... Args>
attr_line_t& add_header(Args... args)
{
Expand Down
20 changes: 10 additions & 10 deletions src/base/intern_string.hh
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ struct string_fragment {
{
}

bool is_valid() const
constexpr bool is_valid() const
{
return this->sf_begin != -1 && this->sf_begin <= this->sf_end;
}

int length() const { return this->sf_end - this->sf_begin; }
constexpr int length() const { return this->sf_end - this->sf_begin; }

Result<ssize_t, const char*> utf8_length() const;

Expand All @@ -164,7 +164,7 @@ struct string_fragment {

size_t column_width() const;

const char* data() const { return &this->sf_string[this->sf_begin]; }
constexpr const char* data() const { return &this->sf_string[this->sf_begin]; }

const unsigned char* udata() const
{
Expand All @@ -176,13 +176,13 @@ struct string_fragment {
return (char*) &this->sf_string[this->sf_begin + offset];
}

char front() const { return this->sf_string[this->sf_begin]; }
constexpr char front() const { return this->sf_string[this->sf_begin]; }

uint32_t front_codepoint() const;

char back() const { return this->sf_string[this->sf_end - 1]; }
constexpr char back() const { return this->sf_string[this->sf_end - 1]; }

void pop_back()
constexpr void pop_back()
{
if (!this->empty()) {
this->sf_end -= 1;
Expand All @@ -200,7 +200,7 @@ struct string_fragment {

string_fragment sub_cell_range(int cell_start, int cell_end) const;

const char& operator[](size_t index) const
constexpr const char& operator[](size_t index) const
{
return this->sf_string[sf_begin + index];
}
Expand Down Expand Up @@ -291,7 +291,7 @@ struct string_fragment {
return *suffix == '\0';
}

string_fragment substr(int begin) const
constexpr string_fragment substr(int begin) const
{
return string_fragment{
this->sf_string, this->sf_begin + begin, this->sf_end};
Expand All @@ -309,13 +309,13 @@ struct string_fragment {
this->sf_string, this->sf_begin + begin, this->sf_begin + end};
}

bool contains(const string_fragment& sf) const
constexpr bool contains(const string_fragment& sf) const
{
return this->sf_string == sf.sf_string && this->sf_begin <= sf.sf_begin
&& sf.sf_end <= this->sf_end;
}

size_t count(char ch) const
constexpr size_t count(char ch) const
{
size_t retval = 0;

Expand Down
Loading

0 comments on commit afa6b79

Please sign in to comment.