-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_ameba.sh
170 lines (134 loc) · 4.64 KB
/
text_ameba.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
#################################
# test-ameba
#################################
select_option() {
ESC=$( printf "\033")
cursor_blink_on() { printf "$ESC[?25h"; }
cursor_blink_off() { printf "$ESC[?25l"; }
cursor_to() { printf "$ESC[$1;${2:-1}H"; }
print_option() { printf " $1 "; }
print_selected() { printf "> $ESC[7m $1 $ESC[27m"; }
get_cursor_row() { IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; }
key_input() { read -s -n3 key 2>/dev/null >&2
if [[ $key = $ESC[A ]]; then echo up; fi
if [[ $key = $ESC[B ]]; then echo down; fi
if [[ $key = "" ]]; then echo enter; fi; }
local options=("$@")
local selected=0
local current_row
current_row=$(get_cursor_row)
cursor_blink_off
while true; do
for i in "${!options[@]}"; do
cursor_to $(($current_row + $i))
if [ $i -eq $selected ]; then
print_selected "${options[$i]}"
else
print_option "${options[$i]}"
fi
done
case $(key_input) in
up) ((selected--));
if [ $selected -lt 0 ]; then selected=$((${#options[@]} - 1)); fi;;
down) ((selected++));
if [ $selected -ge ${#options[@]} ]; then selected=0; fi;;
enter) break;;
esac
done
cursor_to $current_row
printf "\n"
cursor_blink_on
return $selected
}
process_text_input() {
printf '\033[H\033[2J'
echo "==========================="
echo "= t e x t - a m e b a ="
echo "==========================="
echo -e "\nテキストを入力してください (終了の場合は'end'と入力しEnterキーを押してください。)\n"
local IFS=$'\n'
local output=""
local first=true
while IFS= read -r line; do
if [ "$line" = "end" ]; then
break
fi
line=$(echo "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "$line" ]; then
if [ "$first" = true ]; then
output="'$line'"
first=false
else
output="$output,\n'$line'"
fi
fi
done
printf '\033[H\033[2J'
echo -e "\n( ´・ω・)⊃ スッ \n\n$output"
echo -e "$output" | copy_to_clipboard
}
copy_to_clipboard() {
if command -v xclip >/dev/null 2>&1; then
xclip -selection clipboard
echo -e "\nクリップボードにコピーしました。\n"
elif command -v pbcopy >/dev/null 2>&1; then
pbcopy
echo -e "\nクリップボードにコピーしました。\n"
else
echo -e "\nクリップボードにコピーできませんでした。xclip または pbcopy がインストールされていません。\n"
return 1
fi
}
process_file_input() {
printf '\033[H\033[2J'
echo "==========================="
echo "= t e x t - a m e b a ="
echo "==========================="
echo -e "\nインプットを行うファイルのパスを入力してください (相対パスでも大丈夫です)\n"
read -p "> " input_file
input_file="${input_file/#\~/$HOME}"
input_file="$(realpath "$input_file")"
if [ -f "$input_file" ]; then
output_dir="$HOME/text_ameba"
if [ ! -d "$output_dir" ]; then
mkdir -p "$output_dir"
fi
timestamp=$(date +"%Y%m%d_%H%M%S")
output_file="$output_dir/$timestamp"
> "$output_file"
total_lines=$(awk 'BEGIN { count = 0 } NF > 0 {count++} END {print count}' "$input_file")
awk -v total_lines="$total_lines" '
BEGIN {
count = 0
}
NF > 0 {
printf "\x27%s\x27", $0
if (count < total_lines-1 ) {
printf ",\n"
count++
}
}
END {
printf "\n"
}
' "$input_file" > "$output_file"
echo "処理が完了しました。結果は $output_file に保存されています。"
else
echo "エラー: ファイル '$input_file' が存在しません。"
fi
}
text_ameba() {
printf '\033[H\033[2J'
echo "==========================="
echo "= t e x t - a m e b a ="
echo "==========================="
options=("テキスト入力" "ファイル読み込み" "終了")
select_option "${options[@]}"
choice=$?
case $choice in
0) process_text_input ;;
1) process_file_input ;;
2) echo "";;
esac
}