-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathumar.sh
executable file
·3194 lines (2421 loc) · 103 KB
/
umar.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
version="v3.4.0"
pid=$$
distro=""
de=""
color_green='\033[0;32m'
color_cyan='\033[0;36m'
color_blue='\033[0;34m'
color_red='\033[0;31m'
color_yellow='\033[1;33m'
color_reset='\033[0m'
bold_start='\033[1m'
bold_end='\033[0m'
umar() {
if is_file_exist "/etc/os-release"; then
distro=$(awk -F= '/^ID=/ { print $2 }' "/etc/os-release" | tr -d '"')
fi
case "$distro" in
arch*) distro="arch" ;;
debian*) distro="debian" ;;
ubuntu*) distro="ubuntu" ;;
xubuntu*) distro="xubuntu" ;;
fedora*) distro="fedora" ;;
centos*) distro="centos" ;;
manjaro*) distro="manjaro" ;;
*) distro="unknown" ;;
esac
if [ -n "$XDG_CURRENT_DESKTOP" ]; then
de="$XDG_CURRENT_DESKTOP"
elif is_file_exist "/etc/X11/xinit/xinitrc.d/50-gnome-session.sh"; then
de="gnome"
elif is_file_exist "/etc/X11/xinit/xinitrc.d/50-kde.sh"; then
de="kde"
elif is_file_exist "/etc/X11/xinit/xinitrc.d/50-xfce.sh"; then
de="xfce"
elif is_file_exist "/etc/X11/xinit/xinitrc.d/50-lxde.sh"; then
de="lxde"
elif is_file_exist "/etc/X11/xinit/xinitrc.d/50-i3.sh"; then
de="i3wm"
elif is_file_exist "/usr/share/xsessions/gnome.desktop"; then
de="gnome"
elif is_file_exist "/usr/share/xsessions/kde.plasma.desktop"; then
de="kdeplasma"
elif is_file_exist "/usr/share/xsessions/xfce.desktop"; then
de="xfce"
elif is_file_exist "/usr/share/xsessions/lxde.desktop"; then
de="lxde"
elif is_file_exist "/usr/share/xsessions/i3.desktop"; then
de="i3wm"
fi
create_dir "$HOME/.umar"
check_requirements "sh"
if is_no_argument "$@"; then
printout "
I am Umar (${color_yellow}$version${color_reset}), your Linux assistant. I can help \
you with the common tasks listed below. I will continue to be updated indefinitely, as \
my creator may need to add new features, update my logic, fix issues, or make other changes. \
I can also use AI, but you need to manually set up the configuration first.
"
printout "\
${color_green}${bold_start}u ${bold_end}${color_reset} :Make me smarter by updating me to the latest version
${color_green}${bold_start}v ${bold_end}${color_reset} :Show my current version
${color_green}${bold_start}r ${bold_end}${color_reset} :Reveal/open my source code
${color_green}${bold_start}p ${bold_end}${color_reset} :Print my source code
${color_green}${bold_start}w ${bold_end}${color_reset} :Open my command window. You may create a keyboard shortcut for this command to open my command window directly
${color_green}${bold_start}run ${bold_end}${color_reset}OPTION | COMMANDS... :Run custom command(s)
${color_cyan}${bold_start} -l ${bold_end}${color_reset} :get list custom command(s)
${color_cyan}${bold_start} -s ${bold_end}${color_reset} :set new custom command
${color_cyan}${bold_start} -r ${bold_end}${color_reset}COMMANDS... :remove custom command(s)
${color_cyan}${bold_start} -cd ${bold_end}${color_reset} :change custom command description
${color_cyan}${bold_start} -cc ${bold_end}${color_reset} :change custom command
${color_green}${bold_start}ai ${bold_end}${color_reset}[OPTION] :Use AI function(s). Supported AI: Google, ChatGPT
${color_cyan}${bold_start} -p ${bold_end}${color_reset}PROMPT_TEXT :prompt to AI
${color_cyan}${bold_start} -c ${bold_end}${color_reset} :start AI chat session
${color_cyan}${bold_start} -i ${bold_end}${color_reset} :show AI information
${color_cyan}${bold_start} -s ${bold_end}${color_reset} :set AI config
${color_cyan}${bold_start} -ct ${bold_end}${color_reset} :change AI type
${color_cyan}${bold_start} -cm ${bold_end}${color_reset} :change AI model
${color_cyan}${bold_start} -ca ${bold_end}${color_reset} :change AI api key
${color_green}${bold_start}open ${bold_end}${color_reset}PACKAGES... :Open package(s)
${color_green}${bold_start}kill ${bold_end}${color_reset}NAMES... :Kill package(s) process
${color_green}${bold_start}ins ${bold_end}${color_reset}PACKAGES... :Install package(s)
${color_green}${bold_start}rm ${bold_end}${color_reset}PACKAGES... :Remove package(s)
${color_green}${bold_start}upg ${bold_end}${color_reset}[PACKAGES...] :Upgrade package(s)
${color_green}${bold_start}srch ${bold_end}${color_reset}TEXT... :Search for the given keyword(s) using a terminal browser
${color_green}${bold_start}au ${bold_end}${color_reset}[OPTION] :Use audio function(s)
${color_cyan}${bold_start} -c ${bold_end}${color_reset} :show audio card(s)
${color_cyan}${bold_start} -p ${bold_end}${color_reset}AUDIO_FILEPATHS... :play audio(s)
${color_green}${bold_start}img ${bold_end}${color_reset}OPTION :Use image function(s)
${color_cyan}${bold_start} -s ${bold_end}${color_reset}IMAGE_FILEPATHS... :show image(s)
${color_green}${bold_start}vid ${bold_end}${color_reset}OPTION :Use video function(s)
${color_cyan}${bold_start} -p ${bold_end}${color_reset}VIDEO_FILEPATH :play video
${color_green}${bold_start}bth ${bold_end}${color_reset} :Open bluetooth manager
${color_green}${bold_start}batt ${bold_end}${color_reset}OPTION :Use battery function(s)
${color_cyan}${bold_start} -c ${bold_end}${color_reset} :show battery capacity
${color_green}${bold_start}repl ${bold_end}${color_reset}OPTION :Use replace function(s)
${color_cyan}${bold_start} -if ${bold_end}${color_reset}OLD NEW FILEPATHS... :replace in file(s)
${color_cyan}${bold_start} -id ${bold_end}${color_reset}OLD NEW DIR_PATHS... :replace in directories
${color_green}${bold_start}pdf ${bold_end}${color_reset}OPTION :Use PDF function(s)
${color_cyan}${bold_start} -o ${bold_end}${color_reset}PDF_FILEPATHS... :open PDF(s)
${color_green}${bold_start}dev ${bold_end}${color_reset} :Show available device(s)
${color_green}${bold_start}reso ${bold_end}${color_reset}[DEVICE] [RESOLUTION] :Set screen resolution
${color_green}${bold_start}bri ${bold_end}${color_reset}[DEVICE] [BRIGHTNESS] :Set screen brightness
${color_green}${bold_start}tcpd ${bold_end}${color_reset} :Configure touchpad device
${color_green}${bold_start}wifi ${bold_end}${color_reset}[SSID] :Scan or connect to a Wi-Fi using nmcli
${color_green}${bold_start}thttp ${bold_end}${color_reset}OPTIONS... :Test and benchmark HTTP URL
${color_cyan}${bold_start} -c ${bold_end}${color_reset}NUM :concurrent
${color_cyan}${bold_start} -r ${bold_end}${color_reset}NUM :retry
${color_cyan}${bold_start} -t ${bold_end}${color_reset}SECONDS :time duration
${color_cyan}${bold_start} -header ${bold_end}${color_reset}TEXT :header
${color_cyan}${bold_start} -userAgent ${bold_end}${color_reset}TEXT :user agent
${color_cyan}${bold_start} -contentType ${bold_end}${color_reset}TEXT :content type
${color_cyan}${bold_start} -u ${bold_end}${color_reset}URL :url
${color_green}${bold_start}ss ${bold_end}${color_reset}[OPTION] :Take a screenshot
${color_cyan}${bold_start} -a ${bold_end}${color_reset} :area
${color_cyan}${bold_start} -f ${bold_end}${color_reset} :fullscreen
${color_green}${bold_start}stp ${bold_end}${color_reset}OPTION :Use setup function(s)
${color_cyan}${bold_start} -fa ${bold_end}${color_reset} :setup fresh Arch Linux installation
${color_cyan}${bold_start} -fai3 ${bold_end}${color_reset} :setup i3wm on a fresh Arch Linux installation
${color_cyan}${bold_start} -d ${bold_end}${color_reset} :install developer tools: DataGrip, GoLand, PyCharm, IDEA, WebStorm, \
RustRover, Go, NVM, Rust, PyEnv, Postman, Docker, Git, Vim, Meld, Sublime Text
${color_cyan}${bold_start} -ag ${bold_end}${color_reset} :install gaming tools on Arch Linux: Steam, Graphical Drivers
${color_green}${bold_start}intmb ${bold_end}${color_reset}OPTION :Use Intel MacBook function(s)
${color_cyan}${bold_start} -a ${bold_end}${color_reset} :configure audio
${color_cyan}${bold_start} -c ${bold_end}${color_reset} :configure camera
${color_cyan}${bold_start} -b ${bold_end}${color_reset} :configure bluetooth
${color_cyan}${bold_start} -f ${bold_end}${color_reset}[SPEED] :set fan speed
" | while IFS=: read -r name description; do
printout_no_enter "$name"
printout "$description" | fold -s -w $(($(tput cols) - 30)) | sed '2,$s/^/ /'
done
printout "$(printout "\
All functions have been tested on ${color_yellow}**Arch Linux**${color_reset}. They might work on other distros, \
but I'm not sure. ${color_red}**DON'T EXECUTE ANYTHING IF YOU'RE NOT SURE, IT MAY BREAK YOUR SYSTEM**${color_reset}
" | markdown_parse)\n"
return 0
fi
_command="command_$1"
shift
$_command "$@" || printout "Invalid command"
}
# ---------------------------------------------------------------------------------------------------------------------
#
# `command`
# Provides available Umar's command(s)
#
# ---------------------------------------------------------------------------------------------------------------------
command_u() {
check_requirements "curl" "sudo"
_url="https://raw.githubusercontent.com/dalikewara/umar/master/umar.sh"
_tmp_filepath="/tmp/umar.sh"
_installation_filepath="/usr/local/bin/umar"
curl -L "$_url" -o "$_tmp_filepath"
sudo mv "$_tmp_filepath" "$_installation_filepath"
sudo chmod +x "$_installation_filepath"
if [ -x "$_installation_filepath" ]; then
printout_exit "Ok"
fi
printout_exit "Aborted!"
}
command_v() {
printout_exit "$version"
}
command_r() {
check_requirements "vim"
execute vim -R "/usr/local/bin/umar"
}
command_p() {
cat "/usr/local/bin/umar"
}
command_w() {
check_requirements "yad"
_command=$(yad --entry --sticky --no-buttons --width=400 --title "" --text "DON'T EXECUTE ANYTHING IF YOU'RE NOT SURE, IT MAY BREAK YOUR SYSTEM" --text-align "fill" --undecorated)
if is_empty "$_command"; then
return 0
fi
if ! is_start_with "$_command" "umar"; then
_command="umar $_command"
fi
if is_start_with "$_command" "umar help"; then
open_terminal_and_execute_wait "umar"
elif is_start_with "$_command" "umar img" || is_start_with "$_command" "umar vid"; then
execute_eval "$_command"
elif is_start_with "$_command" "umar open" || is_start_with "$_command" "umar ss"; then
$_command
elif is_start_with "$_command" "umar srch" || is_start_with "$_command" "umar au" || is_start_with "$_command" "umar bth"; then
open_terminal_and_execute "$_command"
else
open_terminal_and_execute_wait "$_command"
fi
}
command_run() {
if is_no_argument "$@"; then
printout_exit "An option or command(s) is required!"
fi
_cfg_dir="$HOME/.umar/run"
_cfg_filepath="$HOME/.umar/run-list.cfg"
create_dir "$_cfg_dir"
create_file "$_cfg_filepath"
if is_equal "$1" "-l"; then
_cfg="$(read_file_content "$_cfg_filepath")"
if is_empty "$_cfg"; then
printout_exit "No custom commands have been set"
fi
printout "$_cfg" | while IFS=: read -r _name _description _command; do
if is_empty "$_name" && is_empty "$_description" && is_empty "$_command"; then
continue
fi
printf "${color_green}${bold_start}%-20s ${bold_end}${color_reset}%-30s ${color_cyan}%b${color_reset}\n\n" "$_name" "$_description" "$(head -n 5 -v $_command)"
done
return 0
fi
if is_equal "$1" "-s"; then
printout_no_enter "Enter a name... "
_name=$(read_input)
if is_empty "$_name"; then
printout_exit "The name can't be empty!"
fi
if is_contain "$_name" ":"; then
printout_exit "The name can't contain a colon!"
fi
if is_equal "$_name" "-l"; then
printout_exit "The name can't be: -l"
fi
if is_equal "$_name" "-s"; then
printout_exit "The name can't be: -s"
fi
if is_equal "$_name" "-r"; then
printout_exit "The name can't be: -r"
fi
if is_equal "$_name" "-cd"; then
printout_exit "The name can't be: -cd"
fi
if is_equal "$_name" "-cc"; then
printout_exit "The name can't be: -cc"
fi
_name_exist=$(read_file_content "$_cfg_filepath" | while IFS=: read -r _name_config _ _; do
if is_equal "$_name" "$_name_config"; then
printout_exit "exist"
fi
done)
if is_equal "$_name_exist" "exist"; then
printout_exit "The name already exists!"
fi
printout_no_enter "Enter a description... "
_description=$(read_input)
if is_contain "$_description" ":"; then
printout_exit "The description can't contain a colon!"
fi
_command="$_cfg_dir/$_name.sh"
create_file "$_command"
write_to_file "\
#!/bin/sh
" "$_command"
chmod +x "$_command"
_cfg=$(append_content_to_file "$_name:$_description:$_command" "$_cfg_filepath")
write_to_file "$_cfg" "$_cfg_filepath"
vim + "$_command"
printout "Ok"
return 0
fi
if is_equal "$1" "-r"; then
shift
if is_no_argument "$@"; then
printout_exit "You didn't provide any custom command(s) to remove!"
fi
for _arg in "$@"; do
read_file_content "$_cfg_filepath" | while IFS=: read -r _name_config _ _; do
if is_equal "$_arg" "$_name_config" && ! is_empty "$_name_config"; then
delete_line_from_file_by_keyword "$_name_config:" "$_cfg_filepath"
rm "$_cfg_dir/$_name_config.sh"
break
fi
done
done
printout "Ok"
return 0
fi
if is_equal "$1" "-cd"; then
printout_no_enter "Enter a name... "
_name=$(read_input)
if is_empty "$_name"; then
printout_exit "The name can't be empty!"
fi
if is_contain "$_name" ":"; then
printout_exit "The name can't contain a colon!"
fi
_name_exist=$(read_file_content "$_cfg_filepath" | while IFS=: read -r _name_config _ _; do
if is_equal "$_name" "$_name_config" && ! is_empty "$_name_config"; then
printout_exit "exist"
fi
done)
if ! is_equal "$_name_exist" "exist"; then
printout_exit "${color_green}$_name${color_reset} command not found!"
fi
printout_no_enter "Enter a description... "
_description=$(read_input)
if is_contain "$_description" ":"; then
printout_exit "The description can't contain a colon!"
fi
read_file_content "$_cfg_filepath" | while IFS=: read -r _name_config _ _command_config; do
if is_equal "$_name" "$_name_config" && ! is_empty "$_name_config"; then
change_file_content_line_by_keyword "$_name_config:" "$_name_config:$_description:$_command_config" "$_cfg_filepath"
break
fi
done
return 0
fi
if is_equal "$1" "-cc"; then
printout_no_enter "Enter a name... "
_name=$(read_input)
if is_empty "$_name"; then
printout_exit "The name can't be empty!"
fi
if is_contain "$_name" ":"; then
printout_exit "The name can't contain a colon!"
fi
_name_exist=$(read_file_content "$_cfg_filepath" | while IFS=: read -r _name_config _ _; do
if is_equal "$_name" "$_name_config" && ! is_empty "$_name_config"; then
printout_exit "exist"
fi
done)
if ! is_equal "$_name_exist" "exist"; then
printout_exit "${color_green}$_name${color_reset} command not found!"
fi
vim "$_cfg_dir/$_name.sh"
return 0
fi
write_to_tmp_value_file "not exist"
for _arg in "$@"; do
read_file_content "$_cfg_filepath" | while IFS=: read -r _name _description _command; do
if is_empty "$_name" && is_empty "$_description" && is_empty "$_command"; then
continue
fi
if is_equal "$_arg" "$_name"; then
if is_equal "$_name" "-l"; then
printout_exit "The name can't be: -l"
fi
if is_equal "$_name" "-s"; then
printout_exit "The name can't be: -s"
fi
if is_equal "$_name" "-r"; then
printout_exit "The name can't be: -r"
fi
if is_equal "$_name" "-cd"; then
printout_exit "The name can't be: -cd"
fi
if is_equal "$_name" "-cc"; then
printout_exit "The name can't be: -cc"
fi
write_to_tmp_value_file "exist"
printout "\n${color_green}${bold_start}$_name${bold_end}${color_reset} >_ ${color_cyan}$(head -n 5 -v $_command)${color_reset}\n"
execute_eval "$_command"
printout_blank_line
break
fi
done
if ! is_equal "$(get_tmp_value)" "exist"; then
printout "${color_green}${bold_start}$_arg${bold_end}${color_reset} command not found!"
fi
done
}
command_ai() {
check_requirements "jq"
_cfg_filepath="$HOME/.umar/ai.cfg"
_apikey_filepath="$HOME/.umar/ai.apikey"
_type=""
_model=""
_apikey=""
create_file "$_cfg_filepath"
if ! is_file_exist "$_apikey_filepath"; then
create_file "$_apikey_filepath"
write_to_file "$(printout " \n ")" "$_apikey_filepath"
fi
if ! is_equal "$1" "-s"; then
if is_file_exist "$_cfg_filepath"; then
_type=$(read_file_content_line "1" "$_cfg_filepath")
_model=$(read_file_content_line "2" "$_cfg_filepath")
fi
if is_empty "$_type"; then
printout_exit "You didn't provide any AI type to process!\nYou can use this command to set up a new one: ${color_cyan}umar ai -s${color_reset}"
fi
if is_empty "$_model"; then
printout_exit "You didn't provide any AI model to process!\nYou can use this command to set up a new one: ${color_cyan}umar ai -s${color_reset}"
fi
if is_file_exist "$_apikey_filepath"; then
if is_equal "$_type" "google"; then
_apikey=$(read_file_content_line "1" "$_apikey_filepath")
elif is_equal "$_type" "chatgpt"; then
_apikey=$(read_file_content_line "2" "$_apikey_filepath")
fi
fi
fi
if is_equal "$1" "-p" || is_equal "$1" "-c"; then
if is_equal "$_type" "google" && is_empty "$_apikey"; then
printout_exit "You need an API key to continue the process!\nYou can use this command to set a new one: ${color_cyan}umar ai -ca${color_reset}"
elif is_equal "$_type" "chatgpt" && is_empty "$_apikey"; then
printout_exit "You need an API key to continue the process!\nYou can use this command to set a new one: ${color_cyan}umar ai -ca${color_reset}"
fi
fi
if is_equal "$1" "-p"; then
shift
printout "${color_yellow}$_type ($_model)${color_reset}\n"
_prompt=$(printout "$*" | escape_json_string)
_response=""
if is_equal "$_type" "google"; then
_response=$(http_request_google_ai "$_model" "$_apikey" "$(printout "{\"role\": \"user\", \"parts\":[{\"text\": \"$_prompt\"}]}," | remove_trailing_comma)")
elif is_equal "$_type" "chatgpt"; then
_response=$(http_request_chatgpt_ai "$_model" "$_apikey" "$(printout "{\"role\": \"user\", \"content\": \"$_prompt\"}," | remove_trailing_comma)")
fi
printout_typing "$(printout "$_response" | markdown_parse)"
printout_blank_line
return 0
fi
if is_equal "$1" "-c"; then
printout "${color_yellow}$_type ($_model)${color_reset}\n"
_prompt=""
while true; do
printout_no_enter "${color_yellow}>_${color_reset} "
_chat_prompt="$(read_input)"
if is_equal "$_chat_prompt" "exit" || is_equal "$_chat_prompt" "abort"; then
printout "Aborted!"
break
fi
if is_empty "$_chat_prompt"; then
continue
fi
_chat_prompt=$(printout "$_chat_prompt" | escape_json_string)
if is_equal "$_type" "google"; then
_chat_prompt="{\"role\": \"user\", \"parts\":[{\"text\": \"$(printout "$_chat_prompt" | escape_json_string)\"}]},"
elif is_equal "$_type" "chatgpt"; then
_chat_prompt="{\"role\": \"user\", \"content\": \"$(printout "$_chat_prompt" | escape_json_string)\"},"
fi
if is_empty "$_prompt"; then
_prompt="$_chat_prompt"
else
_prompt="$_prompt $_chat_prompt"
fi
_response=""
printout_blank_line
_prompt=$(printf "%s" "$_prompt" | tr '
' ' ')
if is_equal "$_type" "google"; then
_response=$(http_request_google_ai "$_model" "$_apikey" "$(printout "$_prompt" | remove_trailing_comma)")
elif is_equal "$_type" "chatgpt"; then
_response=$(http_request_chatgpt_ai "$_model" "$_apikey" "$(printout "$_prompt" | remove_trailing_comma)")
fi
if is_equal "$_type" "google"; then
_prompt="$_prompt {\"role\": \"model\", \"parts\":[{\"text\": \"$(printout "$_response" | escape_json_string)\"}]},"
elif is_equal "$_type" "chatgpt"; then
_prompt="$_prompt {\"role\": \"model\", \"content\": \"$(printout "$_response" | escape_json_string)\"},"
fi
printout_typing "$(printout "$_response" | markdown_parse)"
printout_blank_line
done
return 0
fi
if is_equal "$1" "" || is_equal "$1" "-i"; then
if ! is_empty "$_apikey"; then
_apikey="* * * * *"
fi
printout "Type: ${color_yellow}$_type\n${color_reset}Model: ${color_yellow}$_model\n${color_reset}API key: ${color_blue}$_apikey${color_reset}"
return 0
fi
if is_equal "$1" "-s" || is_equal "$1" "-ct"; then
printout_no_enter "\
AI type:
1. Google
2. ChatGPT
Choose the AI type number... "
_type=$(read_input)
if is_empty "$_type"; then
printout_exit "AI type can't be empty!"
fi
if ! is_equal "$_type" "1" && ! is_equal "$_type" "2"; then
printout_exit "Wrong AI type!"
fi
if is_equal "$_type" "1"; then
_type="google"
fi
if is_equal "$_type" "2"; then
_type="chatgpt"
fi
fi
if is_equal "$1" "-s" || is_equal "$1" "-cm" || is_equal "$1" "-ct"; then
if is_empty "$_type"; then
_type=$(read_file_content_line "1" "$_cfg_filepath")
fi
if is_equal "$_type" "google"; then
printout_no_enter "
AI model:
1. gemini-1.0-pro
2. gemini-1.5-pro
3. gemini-1.5-flash
Choose the AI model number... "
elif is_equal "$_type" "chatgpt"; then
printout_no_enter "
AI model:
1. gpt-4o-mini
2. gpt-3.5-turbo
3. gpt-3.5-turbo-0125
4. gpt-3.5-turbo-1106
5. gpt-3.5-turbo-16k
6. gpt-3.5-turbo-instruct
7. gpt-3.5-turbo-instruct-0914
Choose the AI model number... "
else
printout_exit "Wrong AI type!"
fi
_model=$(read_input)
if is_empty "$_model"; then
printout_exit "AI model can't be empty!"
fi
if is_equal "$_type" "google"; then
if ! is_equal "$_model" "1" && ! is_equal "$_model" "2" && ! is_equal "$_model" "3"; then
printout_exit "Wrong AI model!"
fi
if is_equal "$_model" "1"; then
_model="gemini-1.0-pro"
fi
if is_equal "$_model" "2"; then
_model="gemini-1.5-pro"
fi
if is_equal "$_model" "3"; then
_model="gemini-1.5-flash"
fi
elif is_equal "$_type" "chatgpt"; then
if ! is_equal "$_model" "1" && ! is_equal "$_model" "2" && ! is_equal "$_model" "3" && ! is_equal "$_model" "4" && ! is_equal "$_model" "5" && ! is_equal "$_model" "6" && ! is_equal "$_model" "7"; then
printout_exit "Wrong AI model!"
fi
if is_equal "$_model" "1"; then
_model="gpt-4o-mini"
fi
if is_equal "$_model" "2"; then
_model="gpt-3.5-turbo"
fi
if is_equal "$_model" "3"; then
_model="gpt-3.5-turbo-0125"
fi
if is_equal "$_model" "3"; then
_model="gpt-3.5-turbo-1106"
fi
if is_equal "$_model" "3"; then
_model="gpt-3.5-turbo-16k"
fi
if is_equal "$_model" "3"; then
_model="gpt-3.5-turbo-instruct"
fi
if is_equal "$_model" "3"; then
_model="gpt-3.5-turbo-instruct-0914"
fi
fi
fi
if is_equal "$1" "-s" || is_equal "$1" "-ca"; then
if is_empty "$_type"; then
_type=$(read_file_content_line "1" "$_cfg_filepath")
fi
if is_equal "$_type" "google"; then
printout_no_enter "
You'll need an API key to use the AI. You can follow this documentation -> https://ai.google.dev/gemini-api/docs/api-key
Enter the API key... "
elif is_equal "$_type" "chatgpt"; then
printout_no_enter "
You'll need an API key to use the AI. Log in/Sign up to your OpenAI account and create a new API key
Enter the API key... "
else
printout_exit "Wrong AI type!"
fi
_apikey=$(read_input)
if is_equal "$_type" "google" && is_empty "$_apikey"; then
printout_exit "API key can't be empty!"
elif is_equal "$_type" "chatgpt" && is_empty "$_apikey"; then
printout_exit "API key can't be empty!"
fi
fi
if is_equal "$1" "-s"; then
write_to_file "$(printout "$_type\n$_model")" "$_cfg_filepath"
if is_equal "$_type" "google"; then
change_file_content_line "1" "$_apikey" "$_apikey_filepath"
elif is_equal "$_type" "chatgpt"; then
change_file_content_line "2" "$_apikey" "$_apikey_filepath"
fi
printout "AI configuration registered"
return 0
fi
if is_equal "$1" "-ct"; then
change_file_content_line "1" "$_type" "$_cfg_filepath"
change_file_content_line "2" "$_model" "$_cfg_filepath"
printout "Ok"
return 0
fi
if is_equal "$1" "-cm"; then
change_file_content_line "2" "$_model" "$_cfg_filepath"
printout "Ok"
return 0
fi
if is_equal "$1" "-ca"; then
if is_equal "$_type" "google"; then
change_file_content_line "1" "$_apikey" "$_apikey_filepath"
elif is_equal "$_type" "chatgpt"; then
change_file_content_line "2" "$_apikey" "$_apikey_filepath"
fi
printout "Ok"
return 0
fi
printout "Aborted!"
}
command_open() {
if is_no_argument "$@"; then
printout_exit "You didn't provide any package(s) to open!"
fi
_not_exist=""
for _arg in "$@"; do
if is_package_exist "$_arg" || is_user_package_exist "$_arg"; then
execute_async_no_std_out "$_arg"
continue
fi
_not_exist="yes"
printout "${color_red}**$_arg** ${color_reset}package not found!" | markdown_parse
done
if ! is_equal "$_not_exist" "yes"; then
clear_shell
fi
}
command_kill() {
if is_no_argument "$@"; then
printout_exit "You didn't provide any name(s) to kill!"
fi
check_requirements "sudo"
_process_list=""
printout_blank_line
for _arg in "$@"; do
_process_list="$_process_list\n$(ps -ef | grep "$_arg" | grep -v "$pid" | awk '{print $2}')"
ps aux | grep "$_arg" | grep -v "$pid"
done
_process_list=$(printout "${color_yellow}$_process_list${color_reset}")
printout_no_enter "\nAll processes listed above will be terminated. Are you sure? [N/y] "
_confirmation=$(read_input)
if ! is_equal "$_confirmation" "y"; then
printout "\nAborted!"
return 0
fi
printout "$_process_list" | while IFS= read -r _line; do
if ! is_empty "$_line"; then
sudo kill -9 "$_line" > /dev/null 2>&1
fi
done
printout "\nAll processes have been terminated!"
}
command_ins() {
if is_no_argument "$@"; then
printout_exit "You didn't provide any packages(s) to install!"
fi
install_package "$@"
}
command_rm() {
if is_no_argument "$@"; then
printout_exit "You didn't provide any package(s) to remove!"
fi
remove_package "$@"
}
command_upg() {
upgrade_package "$@"
}
command_srch() {
if is_no_argument "$@"; then
printout_exit "You didn't provide any text to search!"
fi
check_requirements "w3m"
execute w3m "https://www.google.com/search?q=$(printout "$*" | sed 's/ /+/g')"
clear_shell
}
command_au() {
if is_equal "$1" "-c"; then
check_requirements "pactl"
pactl list cards
return 0
fi
if is_equal "$1" "-p"; then
check_requirements "mpg123"
shift
if is_no_argument "$@"; then
printout_exit "You didn't provide any audio(s) to play!"
fi
mpg123 -v "$@"
return 0
fi
check_requirements "pulsemixer"
pulsemixer
}
command_img() {
if is_no_argument "$@"; then
printout_exit "An option is required!"
fi
check_requirements "feh"
if is_equal "$1" "-s"; then
shift
if is_no_argument "$@"; then
printout_exit "You didn't provide any image(s) to show!"
fi
feh "$@" &
return 0
fi
printout_exit "Aborted!"
}
command_vid() {
if is_no_argument "$@"; then
printout_exit "An option is required!"
fi
check_requirements "mpv"
if is_equal "$1" "-p"; then
shift
mpv "$@" > /dev/null 2>&1 &
return 0
fi
printout_exit "Aborted!"
}
command_bth() {
check_requirements "bluetoothctl"
bluetoothctl
}
command_batt() {
if is_no_argument "$@"; then
printout_exit "An option is required!"
fi
if is_equal "$1" "-c"; then
if is_file_exist "/sys/class/power_supply/BAT0/capacity"; then
cat "/sys/class/power_supply/BAT0/capacity"
fi
return 0
fi
printout "Aborted!"
}
command_repl() {
if is_no_argument "$@"; then
printout_exit "An option is required!"
fi
_option="$1"
_old=""
_new=""
if is_equal "$_option" "-if" || is_equal "$_option" "-id"; then
shift
if is_empty "$1" || is_empty "$2"; then
printout_exit "Invalid input!"
fi
_old="$1"
_new="$2"
shift
shift
if is_no_argument "$@"; then
if is_equal "$_option" "-if"; then
printout_exit "The targeted filepath is required!"
fi