Skip to content

Commit

Permalink
Add new char counter code
Browse files Browse the repository at this point in the history
  • Loading branch information
endormi committed Apr 22, 2024
1 parent a093cad commit 4b3bae2
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
1 change: 1 addition & 0 deletions GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ for example, `-c` and `-f`, to separate scripts into folders.
4 -c -gtl: gnome terminal profile load
4 -f -bf: bf (backup folder)
4 -f -c: clean (clean system of logs and trash)
4 -f -char: amount of chars in a file
4 -f -d: check for duplicate files
4 -f -del: del (delete file or directory)
4 -f -dir: directory size calculator
Expand Down
1 change: 1 addition & 0 deletions docs/source/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ for example, `-c` and `-f`, to separate scripts into folders.
4 -c -gtl: gnome terminal profile load
4 -f -bf: bf (backup folder)
4 -f -c: clean (clean system of logs and trash)
4 -f -char: amount of chars in a file
4 -f -d: check for duplicate files
4 -f -del: del (delete file or directory)
4 -f -dir: directory size calculator
Expand Down
3 changes: 3 additions & 0 deletions man/tilux.1
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ Backup folder.
.It 4 \-f \-c
Clean system of logs and trash.

.It 4 \-f \-char
Amount of chars in a file.

.It 4 \-f \-d
Check for duplicate files.

Expand Down
66 changes: 66 additions & 0 deletions sys/file_folder/char_count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/ruby

require 'io/console'

if ARGV[0] == 'tilux'
require_relative '../../tools/catch_exception'
print `python3 -c "from tools.logos import Logo; Logo('Char Count');"`
end

# FileReader is responsible for reading text from files and counting characters.
class FileReader
# Reads text from the specified file.
#
# @param file_path [String] The path to the file.
# @return [String] The text read from the file.
def self.read_text_from_file(file_path)
File.open(file_path, 'r') do |file|
text = file.read
text.gsub!(/\r\n?/, "\n")
return text
end
end

# Counts occurrences of specified characters in the given text.
#
# @param text [String] The text in which characters will be counted.
# @param characters [Array<String>] The characters to be counted.
# @return [Hash<String, Integer>] A hash containing character counts.
def self.count_characters(text, characters)
character_counts = {}
characters.each do |char|
character_counts[char] = text.count(char)
end
character_counts
end
end

def count_characters_in_file(file_path, characters)
text = FileReader.read_text_from_file(file_path)
length = text.length
character_counts = FileReader.count_characters(text, characters)

puts "\n#{length} characters."
character_counts.each do |char, count|
puts "#{count} #{char}'s."
end
end

print 'Choose file: '
file = $stdin.gets.chomp.to_s
empty_input?(file) if ARGV[0] == 'tilux'
unless File.file?(file)
puts "File '#{file}' doesn't exist!"
exit
end

puts "\n(Multiple characters are supported e.g. a b c d)"
print 'Choose character(s) (single character only): '
characters_to_count = $stdin.gets.chomp.split(' ')
empty_input?(characters_to_count) if ARGV[0] == 'tilux'
if characters_to_count.any? { |char| char.length > 1 }
puts 'Only single characters are allowed!'
exit
end

count_characters_in_file(file, characters_to_count)
1 change: 1 addition & 0 deletions tools/tilux/command_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'-f': {
'-bf': -> { TiluxHelpers.sys("bash #{PATH}/sys/file_folder/bf.sh tilux") },
'-c': -> { TiluxHelpers.sys("bash #{PATH}/sys/file_folder/clean.sh tilux") },
'-char': -> { TiluxHelpers.sys("ruby #{PATH}/sys/file_folder/char_count.rb tilux") },
'-d': -> { TiluxHelpers.sys("python3 #{PATH}/sys/file_folder/duplicate_file.py tilux") },
'-del': -> { TiluxHelpers.sys("ruby #{PATH}/sys/file_folder/del.rb tilux") },
'-dir': -> { TiluxHelpers.sys("python3 #{PATH}/sys/file_folder/dir_size_calculator.py tilux") },
Expand Down
28 changes: 15 additions & 13 deletions tools/tilux/print_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,20 @@ def systemc_print

def systemf_print
puts '::FF::'
puts '-bf: bf (backup folder)'
puts '-c: clean (clean system of logs and trash)'
puts '-d: check for duplicate files'
puts '-del: del (delete file or directory)'
puts '-dir: directory size calculator'
puts '-e: exists (file or directory)'
puts '-ext: check file extensions inside a folder'
puts '-f: check file permissions'
puts '-fd: fd (number of files and folders)'
puts '-fl: file updated'
puts '-fs: file size'
puts '-k: search files by keyword'
puts "-l: last updated (file)\n"
puts '-bf: bf (backup folder)'
puts '-c: clean (clean system of logs and trash)'
puts '-char: amount of chars in a file'
puts '-d: check for duplicate files'
puts '-del: del (delete file or directory)'
puts '-dir: directory size calculator'
puts '-e: exists (file or directory)'
puts '-ext: check file extensions inside a folder'
puts '-f: check file permissions'
puts '-fd: fd (number of files and folders)'
puts '-fl: file updated'
puts '-fs: file size'
puts '-k: search files by keyword'
puts "-l: last updated (file)\n"
end

def systemi_print
Expand Down Expand Up @@ -130,6 +131,7 @@ def help_print
puts '4 -c -gtl: gnome terminal profile load'
puts '4 -f -bf: bf (backup folder)'
puts '4 -f -c: clean (clean system of logs and trash)'
puts '4 -f -char: amount of chars in a file'
puts '4 -f -d: check for duplicate files'
puts '4 -f -del: del (delete file or directory)'
puts '4 -f -dir: directory size calculator'
Expand Down

0 comments on commit 4b3bae2

Please sign in to comment.