move dirs

This commit is contained in:
2023-09-26 22:05:13 +03:00
parent 5414fa8538
commit 55d774e7d5
190 changed files with 0 additions and 0 deletions

24
code/Crystal/Colorize.cr Normal file
View File

@ -0,0 +1,24 @@
require "colorize"
"foo".colorize(:green)
100.colorize(:red)
[1, 2, 3].colorize(:blue)
# Available colors are:
# :default
# :black
# :red
# :green
# :yellow
# :blue
# :magenta
# :cyan
# :light_gray
# :dark_gray
# :light_red
# :light_green
# :light_yellow
# :light_blue
# :light_magenta
# :light_cyan
# :white

13
code/Crystal/README.md Normal file
View File

@ -0,0 +1,13 @@
# Crystal
## std
- [Значение из Regex::MatchData](value_from_match.cr) - Получить значение из `Regex::MatchData("Happy")`
- [IO::TimeoutError](prompt_timeout.cr) - Установка тайм-аута на пользовательский ввод
- [ENV["..."]](env_variable.cr) - Переменные среды
- [Colorize](Colorize.cr) - Цветной вывод в консоль
- [JSON](json.cr) - Пример работы с JSON
- [Fiber](schedule.cr) - Пример распаралеливания кода и расписания
- [http/client](http_client.cr) - Пример HTTP клиента
## Stuff
- [`irc_bot.cr`](irc_bot.cr) - Реализация клиента (бота) для IRC

View File

@ -0,0 +1,28 @@
require "socket"
client = UNIXSocket.new("/tmp/myapp.sock")
Signal::INT.trap {
client.puts "quit"
exit
}
print "name: "
name = gets.to_s
spawn {
loop {
msg = client.gets
if msg
puts msg
else
puts "server closed"
exit
end
}
}
loop {
msg = gets.to_s
client.puts "#{name}: #{msg}"
}

View File

@ -0,0 +1 @@
https://github.com/crystal-lang/crystal/issues/4277#issuecomment-294009173

View File

@ -0,0 +1,38 @@
require "socket"
server = UNIXServer.new("/tmp/myapp.sock")
CLIENTS = [] of UNIXSocket
Signal::INT.trap {
puts "closing server..."
server.close
exit
}
def on_message(client)
loop {
msg = client.gets
if msg == "quit"
puts "deleting client..."
CLIENTS.delete client
pp CLIENTS.size
break
else
yield msg
end
}
end
loop {
puts "waiting for client on /tmp/myapp.sock"
new_client = server.accept
spawn {
on_message(new_client) { |msg|
CLIENTS.each { |client|
client.puts msg unless client == new_client
}
}
}
CLIENTS << new_client
pp CLIENTS.size
}

View File

@ -0,0 +1,24 @@
require "socket"
# connect to socket
sock = UNIXSocket.new("/tmp/myapp.sock")
loop do
print "Enter command [help, start, run]: "
# get string from terminal input
string = gets.not_nil!.to_s
if string == "q"
break
end
# send string to server
sock.puts "#{string}\n"
# get response from server & print
response = sock.gets
puts response
end
sock.close

View File

@ -0,0 +1,46 @@
require "socket"
require "file_utils"
SOCKET = "/tmp/myapp.sock"
begin
FileUtils.rm(SOCKET)
rescue e : File::NotFoundError
end
server = UNIXServer.new(SOCKET)
CLIENTS = [] of UNIXSocket
Signal::INT.trap do
puts "Closing server..."
server.close(delete = true)
exit
end
def handle_client(client)
loop do
message = client.gets
p! client
if message == "start"
puts "::request START command::"
client.puts "Hello :)"
elsif message == "help"
puts "::request HELP command::"
client.puts "This example ECHO unix server!"
elsif message == "run"
puts "::request RUN command::"
client.puts "Ready, Set, GO!"
else
break
end
end
end
puts "Listen..."
while client = server.accept
spawn handle_client(client)
end

View File

@ -0,0 +1,9 @@
LibC.signal Signal::INT.value, ->(s : Int32) {
puts "CTRL-C handler here!"
exit
}
Signal::INT.trap do
puts "CTRL-C handler here!"
exit
end

View File

@ -0,0 +1,3 @@
shell = ENV["SHELL"]
puts "SHELL: #{shell}\nLANG: #{ENV["COLORTERM"]}"

View File

@ -0,0 +1,11 @@
require "json"
require "http/client"
minecraft_url = "https://launchermeta.mojang.com/mc/game/version_manifest_v2.json"
response = HTTP::Client.get minecraft_url
version_manifest = JSON.parse(response.body)
version = version_manifest["latest"]["release"]
puts version

24
code/Crystal/irc_bot.cr Normal file
View File

@ -0,0 +1,24 @@
require "socket"
puts "Connecting..."
irc = TCPSocket.new("iiiypuk.me", 6667)
irc << "USER crystal localhost localhost :This is a bot!\n"
irc << "NICK crystal\r\n"
irc << "JOIN #admin\r\n"
irc << "PRIVMSG #admin :!time\r\n"
while true
response = irc.gets
puts response
if response.to_s.includes?("PING")
irc << "PONG #{response.to_s.split[1]}\r\n"
end
if response.to_s.includes?("!time")
irc << "PRIVMSG #admin :#{Time.local.to_unix}\r\n"
end
end
irc.close

5
code/Crystal/json.cr Normal file
View File

@ -0,0 +1,5 @@
require "json"
# Парсинг строки
value = JSON.parse("[1, 2, 3]")
p! value[0]

View File

@ -0,0 +1,2 @@
STDIN.read_timeout = 1
STDIN.gets # raises IO::TimeoutError (after 1 second)

18
code/Crystal/schedule.cr Normal file
View File

@ -0,0 +1,18 @@
def every(period : Time::Span, &block : -> T) forall T
spawn do
loop do
block.call
sleep period
end
end
end
every(2.seconds) {
puts "-@-@-"
}
every(4.seconds) {
puts "(-.-)Zzz..."
}
sleep

View File

@ -0,0 +1,8 @@
test_string_1 = "rw------- (0o600)"
# test_string_2 = "rwx------ (0o700)"
if /\d{3}/.match(test_string_1).try &.[0] != "600"
puts "Password file permissions is not RW for you.".colorize(:red)
exit(0)
end