quran-go

Read Qur'an right in the terminal.
git clone http://git.hanabi.in/repos/quran-go.git
Log | Files | Refs | README | LICENSE

translation-details.go (1095B)


      1 package fetch
      2 
      3 import (
      4 	"fmt"
      5 
      6 	t "git.hanabi.in/gitbox/quran-go/src/types"
      7 )
      8 
      9 // Fetch translation details based on translation ID.
     10 func TranslationDetails(translationID t.Trans) (transDetails t.Translations, err error) {
     11 
     12 	translationList, err := AllTranslations()
     13 	if err != nil {
     14 		return transDetails, err
     15 	}
     16 
     17 	idx := searchTranslation(translationList, translationID)
     18 	if idx > -1 {
     19 		transDetails = translationList[idx]
     20 	} else {
     21 		err = fmt.Errorf("%d is not a valid translation, please use `quran ls-translations' to see available translations.", translationID)
     22 	}
     23 
     24 	return transDetails, err
     25 
     26 }
     27 
     28 // Binary search on translation list against transID.  Returns index of found element, else -1.
     29 func searchTranslation(translationList []t.Translations, translationID t.Trans) (idx int) {
     30 	low, high := 0, len(translationList)-1
     31 	for low <= high {
     32 		mid := (low + high) >> 1
     33 		midElemID := t.Trans(translationList[mid].ID)
     34 
     35 		if midElemID == translationID {
     36 			return mid
     37 		} else {
     38 			if midElemID > translationID {
     39 				high = mid - 1
     40 			} else {
     41 				low = mid + 1
     42 			}
     43 		}
     44 	}
     45 	return -1
     46 }