commit abdc9d4cce617a0252097f5014095f9ceff469b5 Author: Alexander Zhirov Date: Thu May 15 17:52:07 2025 +0300 Инициализация 0.1.0 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7ce9cf7 --- /dev/null +++ b/.gitignore @@ -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/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..f65bf8b --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# Примеры на D + +Реализация простых функций на языке D, включающих в себя различные примеры. Каждый пример вынесен в отдельный модуль, что позволяет без привязок к общему коду изучить код конкретного примера. + +Данные примеры содержат: + +```sh +d-examples +└── shell Запуск команд в shell + ├── pipe Чтение выходных данных на примере ip + └── spinner Эмуляция статуса выполнения процесса +``` diff --git a/dub.json b/dub.json new file mode 100644 index 0000000..c3002e3 --- /dev/null +++ b/dub.json @@ -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" + } +} \ No newline at end of file diff --git a/dub.selections.json b/dub.selections.json new file mode 100644 index 0000000..0c49417 --- /dev/null +++ b/dub.selections.json @@ -0,0 +1,6 @@ +{ + "fileVersion": 1, + "versions": { + "commandr": "1.1.0" + } +} diff --git a/source/app.d b/source/app.d new file mode 100644 index 0000000..7407377 --- /dev/null +++ b/source/app.d @@ -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; +} diff --git a/source/examples/package.d b/source/examples/package.d new file mode 100644 index 0000000..e3169c8 --- /dev/null +++ b/source/examples/package.d @@ -0,0 +1,4 @@ +module examples; + +public import examples.version_; +public import examples.shell; diff --git a/source/examples/shell/package.d b/source/examples/shell/package.d new file mode 100644 index 0000000..3ea1e93 --- /dev/null +++ b/source/examples/shell/package.d @@ -0,0 +1,4 @@ +module examples.shell; + +public import examples.shell.pipe; +public import examples.shell.spinner; diff --git a/source/examples/shell/pipe.d b/source/examples/shell/pipe.d new file mode 100644 index 0000000..54c3f2c --- /dev/null +++ b/source/examples/shell/pipe.d @@ -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); + } +} diff --git a/source/examples/shell/spinner.d b/source/examples/shell/spinner.d new file mode 100644 index 0000000..a231703 --- /dev/null +++ b/source/examples/shell/spinner.d @@ -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"); +} diff --git a/source/examples/version_.d b/source/examples/version_.d new file mode 100644 index 0000000..6f1bec7 --- /dev/null +++ b/source/examples/version_.d @@ -0,0 +1,3 @@ +module examples.version_; + +enum examplesVersion = "0.1.0";