Function egg_mode::text::characters_remaining [] [src]

pub fn characters_remaining(
    text: &str,
    http_url_len: i32,
    https_url_len: i32
) -> (usize, bool)

Returns how many characters would remain in a traditional 140-character tweet with the given text. Also returns an indicator of whether the given text is a valid length for a tweet.

This function exists as a sort of convenience method to allow clients to call one uniform method to show a remaining character count on a tweet compose box, and to conditionally enable a "submit" button.

For the http_url_len and https_url_len parameters, call service::config and use the short_url_len and short_url_len_https fields on the struct that's returned. If you want to perform these checks offline, twitter-text's sample code and tests assume 23 characters for both sizes. At the time of this writing (2016-11-28), those numbers were also being returned from the service itself.

If you're writing text for a direct message and want to know how many characters are available in that context, see service::config and the dm_text_character_limit on the struct returned by that function, then call character_count and subtract the result from the configuration value.

Examples

use egg_mode::text::characters_remaining;

 let (count, _) = characters_remaining("This is a test.", 23, 23);
 assert_eq!(count, 140 - 15);

 // URLs get replaced by a t.co URL of the given length
 let (count, _) = characters_remaining("test.com", 23, 23);
 assert_eq!(count, 140 - 23);

 // Multiple URLs get shortened individually
 let (count, _) =
     characters_remaining("Test https://test.com test https://test.com test.com test", 23, 23);
 assert_eq!(count, 140 - 86);