Инициализация 0.1.0
This commit is contained in:
commit
abdc9d4cce
10 changed files with 158 additions and 0 deletions
17
.gitignore
vendored
Normal file
17
.gitignore
vendored
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
.dub
|
||||||
|
docs.json
|
||||||
|
__dummy.html
|
||||||
|
docs/
|
||||||
|
/d-examples
|
||||||
|
d-examples.so
|
||||||
|
d-examples.dylib
|
||||||
|
d-examples.dll
|
||||||
|
d-examples.a
|
||||||
|
d-examples.lib
|
||||||
|
d-examples-test-*
|
||||||
|
*.exe
|
||||||
|
*.pdb
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
*.lst
|
||||||
|
bin/
|
12
README.md
Normal file
12
README.md
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
# Примеры на D
|
||||||
|
|
||||||
|
Реализация простых функций на языке D, включающих в себя различные примеры. Каждый пример вынесен в отдельный модуль, что позволяет без привязок к общему коду изучить код конкретного примера.
|
||||||
|
|
||||||
|
Данные примеры содержат:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
d-examples
|
||||||
|
└── shell Запуск команд в shell
|
||||||
|
├── pipe Чтение выходных данных на примере ip
|
||||||
|
└── spinner Эмуляция статуса выполнения процесса
|
||||||
|
```
|
14
dub.json
Normal file
14
dub.json
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"Alexander Zhirov"
|
||||||
|
],
|
||||||
|
"copyright": "Copyright © 2025, Alexander Zhirov",
|
||||||
|
"description": "D examples",
|
||||||
|
"license": "MIT",
|
||||||
|
"name": "d-examples",
|
||||||
|
"targetPath": "bin",
|
||||||
|
"targetType": "executable",
|
||||||
|
"dependencies": {
|
||||||
|
"commandr": "~>1.1.0"
|
||||||
|
}
|
||||||
|
}
|
6
dub.selections.json
Normal file
6
dub.selections.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"fileVersion": 1,
|
||||||
|
"versions": {
|
||||||
|
"commandr": "1.1.0"
|
||||||
|
}
|
||||||
|
}
|
23
source/app.d
Normal file
23
source/app.d
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import examples;
|
||||||
|
import commandr;
|
||||||
|
import core.stdc.stdlib : EXIT_SUCCESS;
|
||||||
|
|
||||||
|
private string programName = "d-examples";
|
||||||
|
|
||||||
|
int main(string[] args)
|
||||||
|
{
|
||||||
|
auto argumets = new Program(programName, examplesVersion)
|
||||||
|
.add(new Command("shell", "Запуск команд в shell")
|
||||||
|
.add(new Command("pipe", "Чтение выходных данных на примере ip"))
|
||||||
|
.add(new Command("spinner", "Эмуляция статуса выполнения процесса"))
|
||||||
|
)
|
||||||
|
.parse(args);
|
||||||
|
|
||||||
|
argumets
|
||||||
|
.on("shell", (shell) { shell
|
||||||
|
.on("pipe", (pipe) { pipeShell(); })
|
||||||
|
.on("spinner", (loading) { spinnerShell(); });
|
||||||
|
});
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
4
source/examples/package.d
Normal file
4
source/examples/package.d
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
module examples;
|
||||||
|
|
||||||
|
public import examples.version_;
|
||||||
|
public import examples.shell;
|
4
source/examples/shell/package.d
Normal file
4
source/examples/shell/package.d
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
module examples.shell;
|
||||||
|
|
||||||
|
public import examples.shell.pipe;
|
||||||
|
public import examples.shell.spinner;
|
26
source/examples/shell/pipe.d
Normal file
26
source/examples/shell/pipe.d
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
module examples.shell.pipe;
|
||||||
|
|
||||||
|
import std.process : pipeProcess, Redirect, wait;
|
||||||
|
import std.stdio : writeln;
|
||||||
|
|
||||||
|
void pipeShell()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Выполняем команду ping с аргументами
|
||||||
|
auto pipes = pipeProcess(["ping", "8.8.8.8", "-c", "4"], Redirect.stdout);
|
||||||
|
|
||||||
|
// Читаем вывод команды построчно
|
||||||
|
foreach (line; pipes.stdout.byLine)
|
||||||
|
{
|
||||||
|
writeln(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ожидаем завершения процесса
|
||||||
|
wait(pipes.pid);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
writeln("Ошибка: ", e.msg);
|
||||||
|
}
|
||||||
|
}
|
49
source/examples/shell/spinner.d
Normal file
49
source/examples/shell/spinner.d
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
module examples.shell.spinner;
|
||||||
|
|
||||||
|
import std.stdio;
|
||||||
|
import core.thread;
|
||||||
|
import core.time;
|
||||||
|
import core.stdc.signal : signal, SIGINT;
|
||||||
|
import core.stdc.stdio : fprintf, stderr;
|
||||||
|
import core.stdc.stdlib : exit;
|
||||||
|
|
||||||
|
// Обработчик сигнала Ctrl+C
|
||||||
|
extern (C) void handleCtrlC(int sig) nothrow @nogc
|
||||||
|
{
|
||||||
|
// Используем fprintf для вывода в stderr
|
||||||
|
fprintf(stderr, "\033[?25h\rInterrupted! \n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void spinnerShell()
|
||||||
|
{
|
||||||
|
// Устанавливаем обработчик для SIGINT (Ctrl+C)
|
||||||
|
signal(SIGINT, &handleCtrlC);
|
||||||
|
|
||||||
|
// Расширенный набор брайлевских символов для плавной анимации
|
||||||
|
immutable dchar[] spinner = [
|
||||||
|
'⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧'
|
||||||
|
];
|
||||||
|
ulong i = 0;
|
||||||
|
|
||||||
|
// Скрываем курсор
|
||||||
|
write("\033[?25l");
|
||||||
|
|
||||||
|
// Пример длительного процесса
|
||||||
|
foreach (j; 0 .. 40)
|
||||||
|
{
|
||||||
|
// Выводим текущий символ спиннера
|
||||||
|
writef("\rProcessing... %c", spinner[i]);
|
||||||
|
// Сбрасываем буфер вывода
|
||||||
|
stdout.flush();
|
||||||
|
|
||||||
|
// Переключаем символ спиннера
|
||||||
|
i = (i + 1) % spinner.length;
|
||||||
|
|
||||||
|
// Задержка для анимации
|
||||||
|
Thread.sleep(dur!("msecs")(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Восстанавливаем курсор и очищаем строку
|
||||||
|
write("\033[?25h\rDone! \n");
|
||||||
|
}
|
3
source/examples/version_.d
Normal file
3
source/examples/version_.d
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
module examples.version_;
|
||||||
|
|
||||||
|
enum examplesVersion = "0.1.0";
|
Loading…
Add table
Add a link
Reference in a new issue