Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add heuristics for C and C++ #526

Merged
merged 6 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions gengo/languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ C:
category: programming
color: "#8888CC"
nerd-font-glyph: "\U0000e61e"
heuristics:
- '(?m)^#include\s+<(stdio\.h|stdlib\.h|string\.h|math\.h)>'
- '(?m)^int\s+main\s*\(\s*(void|int\s+argc\s*,\s*char\s*\*\s*argv\[\s*\])\s*SW\)'
matchers:
extensions:
- c
Expand All @@ -155,6 +158,11 @@ C:
category: programming
color: "#88CC88"
nerd-font-glyph: "\U0000e61d"
heuristics:
- '(?m)^#include\s+<(iostream|vector|string|algorithm)>'
- '(?m)^(namespace\s+\w+\s*\{|using\s+namespace\s+\w+;)'
- '(?m)^(class|template|typename)\b'
- '(?m)\b(std::|nullptr|constexpr)\b'
matchers:
extensions:
- c++
Expand Down
90 changes: 90 additions & 0 deletions samples-test/samples/C++/progress-bar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "progress-bar.h"
#include <iostream>
#include <sstream>

ProgressBar::ProgressBar(char symbol, int length, int total)
: symbol(symbol)
, length(length)
, progress(0)
, total(total)
, startSymbol('[')
, endSymbol(']')
, format("{bar} {percent} {count}")
, showPercent(false)
, showCount(false) {}

ProgressBar& ProgressBar::update(int progress) {
this->progress = progress;
return *this;
}

ProgressBar& ProgressBar::showPercentage(bool show) {
showPercent = show;
return *this;
}

ProgressBar& ProgressBar::showCounter(bool show) {
showCount = show;
return *this;
}

ProgressBar& ProgressBar::setStartEndSymbols(char start, char end) {
startSymbol = start;
endSymbol = end;
return *this;
}

ProgressBar& ProgressBar::setCustomFormat(const std::string& format) {
this->format = format;
return *this;
}

ProgressBar& ProgressBar::tick() {
progress++;
return *this;
}

void ProgressBar::print() const {
std::ostringstream bar;
std::string percent_str;
std::string count_str;
std::string result;

// Generate bar component
bar << startSymbol;
int scaled_progress = static_cast<int>(static_cast<float>(progress) * length / total);

for (int i = 0; i < length; i++) {
bar << (i < scaled_progress ? symbol : ' ');
}
bar << endSymbol;

// Generate percent component
int percent = (progress * 100) / total;
if (showPercent) {
percent_str = std::to_string(percent) + "%";
}

// Generate count component
if (showCount) {
count_str = std::to_string(progress) + "/" + std::to_string(total);
}

// Process format string
size_t pos = 0;
std::string formatStr = format;

// Replace placeholders with actual values
while ((pos = formatStr.find("{bar}")) != std::string::npos) {
formatStr.replace(pos, 5, bar.str());
}
while ((pos = formatStr.find("{percent}")) != std::string::npos) {
formatStr.replace(pos, 9, percent_str);
}
while ((pos = formatStr.find("{count}")) != std::string::npos) {
formatStr.replace(pos, 7, count_str);
}

std::cout << formatStr << (percent < 100 ? "\r" : "\n");
std::cout.flush();
}
30 changes: 30 additions & 0 deletions samples-test/samples/C++/progress-bar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef PROGRESS_BAR_H
#define PROGRESS_BAR_H

#include <string>

class ProgressBar {
private:
char symbol;
char startSymbol;
char endSymbol;
int length;
int progress;
int total;
std::string format;
bool showPercent;
bool showCount;

public:
ProgressBar(char symbol, int length, int total);

ProgressBar& update(int progress);
ProgressBar& showPercentage(bool show);
ProgressBar& showCounter(bool show);
ProgressBar& setStartEndSymbols(char start, char end);
ProgressBar& setCustomFormat(const std::string& format);
ProgressBar& tick();
void print() const;
};

#endif
Loading